]> granicus.if.org Git - python/commitdiff
Fix suggested by Sjoerd (long ago!) to get a better error message when
authorGuido van Rossum <guido@python.org>
Tue, 29 Sep 1998 15:57:42 +0000 (15:57 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 29 Sep 1998 15:57:42 +0000 (15:57 +0000)
there's a syntax error.  (In particular, display the correct
filename).  This changes the API: if there's a syntax error, the
function now returns normally after dumping the error to sys.stderr.
I changed Sjoerd's use of string.join(string.split(...)) with
string.replace().

Lib/py_compile.py

index 98b3b21f1a9e18cf2b760f42dffb9c11f8eac63c..a6d03d7b3eedf72a354d06358f2d6fee608a72e2 100644 (file)
@@ -51,7 +51,15 @@ def compile(file, cfile=None, dfile=None):
     f.close()
     if codestring and codestring[-1] != '\n':
         codestring = codestring + '\n'
-    codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
+    try:
+        codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
+    except SyntaxError, detail:
+        import traceback, sys, string
+        lines = traceback.format_exception_only(SyntaxError, detail)
+        for line in lines:
+            sys.stderr.write(string.replace(line, 'File "<string>"',
+                                            'File "%s"' % (dfile or file)))
+        return
     if not cfile:
         cfile = file + (__debug__ and 'c' or 'o')
     fc = open(cfile, 'wb')