[pypy-svn] r11633 - pypy/dist/pypy/tool

tismer at codespeak.net tismer at codespeak.net
Fri Apr 29 18:23:00 CEST 2005


Author: tismer
Date: Fri Apr 29 18:23:00 2005
New Revision: 11633

Modified:
   pypy/dist/pypy/tool/_enum_exceptions.py
Log:
added an explicit implementation of SyntaxError__str__

Modified: pypy/dist/pypy/tool/_enum_exceptions.py
==============================================================================
--- pypy/dist/pypy/tool/_enum_exceptions.py	(original)
+++ pypy/dist/pypy/tool/_enum_exceptions.py	Fri Apr 29 18:23:00 2005
@@ -77,7 +77,6 @@
                 print >> f, "    # please implement %s.%s (%r)" % (name, attname, meth)
             else:
                 try:
-                    print >> f, "    # auto-generated code, please check carefully!"
                     for line in func(exc):
                         print >> f, "    " + line
                 except ValueError, e:
@@ -106,6 +105,7 @@
     except: x = 42
     use_default = x is None
     # looks fine so far.
+    yield "# auto-generated code, please check carefully!"
     yield "def __getitem__(self, idx):"
     if use_default:
         yield "    if not hasattr(self, 'args'):"
@@ -260,6 +260,7 @@
         groupassign[key].append( (order, assignment) )
     cases = groupassign.items()
     cases.sort()
+    yield "# auto-generated code, please check carefully!"
     yield "def __init__(self, *args):"
     if len(cases) > 1 or len(cases[0][0]) != maxprobe:
         yield "    argc = len(args)"
@@ -285,6 +286,13 @@
             yield indent * "    " + line
 
 def tryGenerate__str__(exc, maxprobe=20):
+    if exc in known__str__:
+        import inspect
+        src = inspect.getsource(known__str__[exc])
+        for line in src.split("\n"):
+            yield line
+        return
+    
     minargs, maxargs, working = findAllArgs(exc, maxprobe)
     # checking the default case (well, there are two)
     simple = False
@@ -304,6 +312,7 @@
             break
     else:
         simple = arg1_methods and min(arg1_methods) == max(arg1_methods)
+    yield "# auto-generated code, please check carefully!"
     if simple:
         yield "def __str__(self):"
         yield "    args = self.args"
@@ -329,6 +338,28 @@
     yield "    ])"
     yield "    return res"
 
+known__str__ = {}
+
+# SyntaxError
+def __str__(self):
+    if type(self.msg) is not str:
+        return self.msg
+
+    have_filename = type(self.filename) is str
+    have_lineno = type(self.lineno) is int
+    if have_filename or have_lineno:
+        import os
+        fname = os.path.basename(self.filename or "???")
+        if have_filename and have_lineno:
+            buffer = "%s (%s, line %ld)" % (self.msg, fname, self.lineno)
+        elif have_filename:
+            buffer ="%s (%s)" % (self.msg, fname)
+        elif have_lineno:
+            buffer = "%s (line %ld)" % (self.msg, self.lineno)
+    return buffer
+
+known__str__[SyntaxError] = __str__
+
 if __name__ == "__main__":
     import pypy, os
     prefix = os.path.dirname(pypy.__file__)



More information about the Pypy-commit mailing list