summaryrefslogtreecommitdiffstats
path: root/Lib/py_compile.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-09-29 15:57:42 (GMT)
committerGuido van Rossum <guido@python.org>1998-09-29 15:57:42 (GMT)
commitf984a6526711513e1af035d2fcee545c51ab51fe (patch)
treef52398a3475e5bf0b5815175ad3a8e0d3899a737 /Lib/py_compile.py
parentdc492e6738a97d3ff55fb98295660e0edf8a51fa (diff)
downloadcpython-f984a6526711513e1af035d2fcee545c51ab51fe.zip
cpython-f984a6526711513e1af035d2fcee545c51ab51fe.tar.gz
cpython-f984a6526711513e1af035d2fcee545c51ab51fe.tar.bz2
Fix suggested by Sjoerd (long ago!) to get a better error message when
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().
Diffstat (limited to 'Lib/py_compile.py')
-rw-r--r--Lib/py_compile.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/Lib/py_compile.py b/Lib/py_compile.py
index 98b3b21..a6d03d7 100644
--- a/Lib/py_compile.py
+++ b/Lib/py_compile.py
@@ -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')