diff options
author | Fred Drake <fdrake@acm.org> | 2002-08-21 20:23:22 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2002-08-21 20:23:22 (GMT) |
commit | a96f1a3c08fbe9452c24bb163b4480b5c67d1e8d (patch) | |
tree | a0f4c402f20447904feb01e00d453c8ef76146b1 /Lib/py_compile.py | |
parent | 5b09eeea89b2f16cb458410551283d4fd2229460 (diff) | |
download | cpython-a96f1a3c08fbe9452c24bb163b4480b5c67d1e8d.zip cpython-a96f1a3c08fbe9452c24bb163b4480b5c67d1e8d.tar.gz cpython-a96f1a3c08fbe9452c24bb163b4480b5c67d1e8d.tar.bz2 |
Refactor: Remove some code that was obsoleted when this module was
changed to use universal newlines.
Remove all imports from the compile() function; these are
now done at the top of the module ("Python normal form"),
and define a helper based on the platform instead of
testing the platform in the compile() function.
Diffstat (limited to 'Lib/py_compile.py')
-rw-r--r-- | Lib/py_compile.py | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/Lib/py_compile.py b/Lib/py_compile.py index d10c5cb..4a3a8a0 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -3,11 +3,26 @@ This module has intimate knowledge of the format of .pyc files. """ +import __builtin__ import imp +import marshal +import os +import sys +import traceback + MAGIC = imp.get_magic() __all__ = ["compile"] +# Define an internal helper according to the platform +if os.name == "mac": + import macfs + def set_creator_type(file): + macfs.FSSpec(file).SetCreatorType('Pyth', 'PYC ') +else: + def set_creator_type(file): + pass + def wr_long(f, x): """Internal; write a 32-bit int to a file in little-endian order.""" f.write(chr( x & 0xff)) @@ -43,29 +58,22 @@ def compile(file, cfile=None, dfile=None): directories). """ - import os, marshal, __builtin__ f = open(file, 'U') try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() - # If parsing from a string, line breaks are \n (see parsetok.c:tok_nextc) - # Replace will return original string if pattern is not found, so - # we don't need to check whether it is found first. - codestring = codestring.replace("\r\n","\n") - codestring = codestring.replace("\r","\n") f.close() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: codeobject = __builtin__.compile(codestring, dfile or file, 'exec') except SyntaxError, detail: - import traceback, sys lines = traceback.format_exception_only(SyntaxError, detail) for line in lines: sys.stderr.write(line.replace('File "<string>"', - 'File "%s"' % (dfile or file))) + 'File "%s"' % (dfile or file))) return if cfile is None: cfile = file + (__debug__ and 'c' or 'o') @@ -77,6 +85,4 @@ def compile(file, cfile=None, dfile=None): fc.seek(0, 0) fc.write(MAGIC) fc.close() - if os.name == 'mac': - import macfs - macfs.FSSpec(cfile).SetCreatorType('Pyth', 'PYC ') + set_creator_type(cfile) |