[Twisted-Python] safe_str is not safe

Wolfgang Rohdewald wolfgang at rohdewald.de
Mon Feb 6 16:29:26 EST 2012


after patching CopyableFailure as written,

the stack trace now ends with the following lines. The correct
exception message is still not shown. It contains unicode chars
which reflect.py/safe_str cannot handle.

  File "/hdd/pub/src/gitgames/kajongg/src/scoringengine.py", line 170, in 
initRuleset
    raise Exception(m18n('ruleset "%1" not found', self.name))
exceptions.Exception: <Exception instance at 0x30e8b48 with str error:
 'Traceback (most recent call last):\n  File "/usr/local/lib/python2.7/dist-
packages/Twisted-11.0.0-py2.7-linux-x86_64.egg/twisted/python/reflect.py", line 
546, in _safeFormat\n    return formatter(o)\nUnicodeEncodeError: \'ascii\' 
codec can\'t encode character u\'\\u201e\' in position 10: ordinal not in 
range(128)\n'>
Unhandled error in Deferred:


after replacing safe_str by safe_repr in twisted/python/failure.py,

I finally get a meaningful printout:
  File "/hdd/pub/src/gitgames/kajongg/src/scoringengine.py", line 170, in 
initRuleset
    raise Exception(m18n('ruleset "%1" not found', self.name))
exceptions.Exception: Exception(u'Regelsatz 
\u201e55a8a17e29a1a087cc8df9cedb5590cd\u201c nicht gefunden',)
Unhandled error in Deferred:

of course a correct encoding would be even better...
my patch:

--- failure.org 2012-02-06 22:22:46.395065076 +0100
+++ failure.py  2012-02-06 22:22:51.259064816 +0100
@@ -472,7 +472,7 @@
         """Get a string of the exception which caused this Failure."""
         if isinstance(self.value, Failure):
             return self.value.getErrorMessage()
-        return reflect.safe_str(self.value)
+        return reflect.safe_repr(self.value)
 
     def getBriefTraceback(self):
         io = StringIO()
@@ -517,7 +517,7 @@
             w("%s: %s: %s\n" % (
                     hasFrames,
                     reflect.safe_str(self.type),
-                    reflect.safe_str(self.value)))
+                    reflect.safe_repr(self.value)))
         else:
             w( 'Traceback (most recent call last):\n')
 
@@ -542,7 +542,7 @@
                 w(self.type + "\n")
             else:
                 w("%s: %s\n" % (reflect.qual(self.type),
-                                reflect.safe_str(self.value)))
+                                reflect.safe_repr(self.value)))
         # chaining
         if isinstance(self.value, Failure):
             # TODO: indentation for chained failures?

-- 
Wolfgang



More information about the Twisted-Python mailing list