[Python-checkins] python/dist/src/Lib compileall.py,1.12,1.13 py_compile.py,1.23,1.24 zipfile.py,1.27,1.28
loewis@users.sourceforge.net
loewis@users.sourceforge.net
Wed, 15 Jan 2003 03:51:08 -0800
Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv4597/Lib
Modified Files:
compileall.py py_compile.py zipfile.py
Log Message:
Patch #661719: Expose compilation errors as exceptions on request.
Index: compileall.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/compileall.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** compileall.py 1 Jun 2002 19:51:15 -0000 1.12
--- compileall.py 15 Jan 2003 11:51:06 -0000 1.13
***************
*** 63,76 ****
print 'Compiling', fullname, '...'
try:
! ok = py_compile.compile(fullname, None, dfile)
except KeyboardInterrupt:
raise KeyboardInterrupt
! except:
! # XXX py_compile catches SyntaxErrors
! if type(sys.exc_type) == type(''):
! exc_type_name = sys.exc_type
! else: exc_type_name = sys.exc_type.__name__
! print 'Sorry:', exc_type_name + ':',
! print sys.exc_value
success = 0
else:
--- 63,71 ----
print 'Compiling', fullname, '...'
try:
! ok = py_compile.compile(fullname, None, dfile, True)
except KeyboardInterrupt:
raise KeyboardInterrupt
! except py_compile.PyCompileError,err:
! print err.msg
success = 0
else:
Index: py_compile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/py_compile.py,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** py_compile.py 21 Aug 2002 20:56:21 -0000 1.23
--- py_compile.py 15 Jan 2003 11:51:06 -0000 1.24
***************
*** 13,17 ****
MAGIC = imp.get_magic()
! __all__ = ["compile", "main"]
# Define an internal helper according to the platform
--- 13,64 ----
MAGIC = imp.get_magic()
! __all__ = ["compile", "main", "PyCompileError"]
!
!
! class PyCompileError(Exception):
! """Exception raised when an error occurs while attempting to
! compile the file.
!
! To raise this exception, use
!
! raise PyCompileError(exc_type,exc_value,file[,msg])
!
! where
!
! exc_type: exception type to be used in error message
! type name can be accesses as class variable
! 'exc_type_name'
!
! exc_value: exception value to be used in error message
! can be accesses as class variable 'exc_value'
!
! file: name of file being compiled to be used in error message
! can be accesses as class variable 'file'
!
! msg: string message to be written as error message
! If no value is given, a default exception message will be given,
! consistent with 'standard' py_compile output.
! message (or default) can be accesses as class variable 'msg'
!
! """
!
! def __init__(self, exc_type, exc_value, file, msg=''):
! exc_type_name = exc_type.__name__
! if exc_type is SyntaxError:
! tbtext = ''.join(traceback.format_exception_only(exc_type, exc_value))
! errmsg = tbtext.replace('File "<string>"', 'File "%s"' % file)
! else:
! errmsg = "Sorry: %s: %s" % (exc_type_name,exc_value)
!
! Exception.__init__(self,msg or errmsg,exc_type_name,exc_value,file)
!
! self.exc_type_name = exc_type_name
! self.exc_value = exc_value
! self.file = file
! self.msg = msg or errmsg
!
! def __str__(self):
! return self.msg
!
# Define an internal helper according to the platform
***************
*** 31,45 ****
f.write(chr((x >> 24) & 0xff))
! def compile(file, cfile=None, dfile=None):
"""Byte-compile one Python source file to Python bytecode.
Arguments:
! file: source filename
! cfile: target filename; defaults to source with 'c' or 'o' appended
! ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo)
! dfile: purported filename; defaults to source (this is the filename
! that will show up in error messages)
!
Note that it isn't necessary to byte-compile Python modules for
execution efficiency -- Python itself byte-compiles a module when
--- 78,99 ----
f.write(chr((x >> 24) & 0xff))
! def compile(file, cfile=None, dfile=None, doraise=False):
"""Byte-compile one Python source file to Python bytecode.
Arguments:
! file: source filename
! cfile: target filename; defaults to source with 'c' or 'o' appended
! ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo)
! dfile: purported filename; defaults to source (this is the filename
! that will show up in error messages)
! doraise: flag indicating whether or not an exception should be
! raised when a compile error is found. If an exception
! occurs and this flag is set to False, a string
! indicating the nature of the exception will be printed,
! and the function will return to the caller. If an
! exception occurs and this flag is set to True, a
! PyCompileError exception will be raised.
!
Note that it isn't necessary to byte-compile Python modules for
execution efficiency -- Python itself byte-compiles a module when
***************
*** 69,79 ****
codestring = codestring + '\n'
try:
! codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
! except SyntaxError, detail:
! lines = traceback.format_exception_only(SyntaxError, detail)
! for line in lines:
! sys.stderr.write(line.replace('File "<string>"',
! 'File "%s"' % (dfile or file)))
! return
if cfile is None:
cfile = file + (__debug__ and 'c' or 'o')
--- 123,134 ----
codestring = codestring + '\n'
try:
! codeobject = __builtin__.compile(codestring, dfile or file,'exec')
! except Exception,err:
! py_exc = PyCompileError(err.__class__,err.args,dfile or file)
! if doraise:
! raise py_exc
! else:
! sys.stderr.write(py_exc.msg)
! return
if cfile is None:
cfile = file + (__debug__ and 'c' or 'o')
***************
*** 101,106 ****
args = sys.argv[1:]
for filename in args:
! compile(filename)
!
if __name__ == "__main__":
main()
--- 156,164 ----
args = sys.argv[1:]
for filename in args:
! try:
! compile(filename, doraise=True)
! except PyCompileError,err:
! sys.stderr.write(err.msg)
!
if __name__ == "__main__":
main()
Index: zipfile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/zipfile.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** zipfile.py 12 Dec 2002 12:23:32 -0000 1.27
--- zipfile.py 15 Jan 2003 11:51:06 -0000 1.28
***************
*** 605,609 ****
if self.debug:
print "Compiling", file_py
! py_compile.compile(file_py, file_pyc)
fname = file_pyc
else:
--- 605,612 ----
if self.debug:
print "Compiling", file_py
! try:
! py_compile.compile(file_py, file_pyc, None, True)
! except py_compile.PyCompileError,err:
! print err.msg
fname = file_pyc
else: