mirror of
https://github.com/python/cpython.git
synced 2025-09-18 22:50:26 +00:00
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.
This commit is contained in:
parent
5b09eeea89
commit
a96f1a3c08
1 changed files with 17 additions and 11 deletions
|
@ -3,11 +3,26 @@
|
||||||
This module has intimate knowledge of the format of .pyc files.
|
This module has intimate knowledge of the format of .pyc files.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import __builtin__
|
||||||
import imp
|
import imp
|
||||||
|
import marshal
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
|
||||||
MAGIC = imp.get_magic()
|
MAGIC = imp.get_magic()
|
||||||
|
|
||||||
__all__ = ["compile"]
|
__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):
|
def wr_long(f, x):
|
||||||
"""Internal; write a 32-bit int to a file in little-endian order."""
|
"""Internal; write a 32-bit int to a file in little-endian order."""
|
||||||
f.write(chr( x & 0xff))
|
f.write(chr( x & 0xff))
|
||||||
|
@ -43,25 +58,18 @@ def compile(file, cfile=None, dfile=None):
|
||||||
directories).
|
directories).
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import os, marshal, __builtin__
|
|
||||||
f = open(file, 'U')
|
f = open(file, 'U')
|
||||||
try:
|
try:
|
||||||
timestamp = long(os.fstat(f.fileno()).st_mtime)
|
timestamp = long(os.fstat(f.fileno()).st_mtime)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
timestamp = long(os.stat(file).st_mtime)
|
timestamp = long(os.stat(file).st_mtime)
|
||||||
codestring = f.read()
|
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()
|
f.close()
|
||||||
if codestring and codestring[-1] != '\n':
|
if codestring and codestring[-1] != '\n':
|
||||||
codestring = codestring + '\n'
|
codestring = codestring + '\n'
|
||||||
try:
|
try:
|
||||||
codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
|
codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
|
||||||
except SyntaxError, detail:
|
except SyntaxError, detail:
|
||||||
import traceback, sys
|
|
||||||
lines = traceback.format_exception_only(SyntaxError, detail)
|
lines = traceback.format_exception_only(SyntaxError, detail)
|
||||||
for line in lines:
|
for line in lines:
|
||||||
sys.stderr.write(line.replace('File "<string>"',
|
sys.stderr.write(line.replace('File "<string>"',
|
||||||
|
@ -77,6 +85,4 @@ def compile(file, cfile=None, dfile=None):
|
||||||
fc.seek(0, 0)
|
fc.seek(0, 0)
|
||||||
fc.write(MAGIC)
|
fc.write(MAGIC)
|
||||||
fc.close()
|
fc.close()
|
||||||
if os.name == 'mac':
|
set_creator_type(cfile)
|
||||||
import macfs
|
|
||||||
macfs.FSSpec(cfile).SetCreatorType('Pyth', 'PYC ')
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue