THRIFT-471. python: Generated exceptions should implement __str__
authorDavid Reiss <dreiss@apache.org>
Wed, 29 Apr 2009 23:35:01 +0000 (23:35 +0000)
committerDavid Reiss <dreiss@apache.org>
Wed, 29 Apr 2009 23:35:01 +0000 (23:35 +0000)
This makes Python stack dumps properly show the exception content.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@770002 13f79535-47bb-0310-9956-ffa450edef68

compiler/cpp/src/generate/t_py_generator.cc

index cd7f760..343c982 100644 (file)
@@ -637,6 +637,16 @@ void t_py_generator::generate_py_struct_definition(ofstream& out,
   generate_py_struct_reader(out, tstruct);
   generate_py_struct_writer(out, tstruct);
 
+  // For exceptions only, generate a __str__ method. This is
+  // because when raised exceptions are printed to the console, __repr__
+  // isn't used. See python bug #5882
+  if (is_exception) {
+    out <<
+      indent() << "def __str__(self):" << endl <<
+      indent() << "  return repr(self)" << endl <<
+      endl;
+  }
+
   // Printing utilities so that on the command line thrift
   // structs look pretty like dictionaries
   out <<