diff options
author | Georg Brandl <georg@python.org> | 2010-12-04 10:26:46 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-12-04 10:26:46 (GMT) |
commit | 8334fd9285a8e9f0864b0453ae738fe3f6893b21 (patch) | |
tree | f9341847b4647cd85b6fcd4e5fbece5cd15e1883 /Lib/py_compile.py | |
parent | 427d3149ebe5c4495e69a04be5464e5b8b446c9e (diff) | |
download | cpython-8334fd9285a8e9f0864b0453ae738fe3f6893b21.zip cpython-8334fd9285a8e9f0864b0453ae738fe3f6893b21.tar.gz cpython-8334fd9285a8e9f0864b0453ae738fe3f6893b21.tar.bz2 |
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Diffstat (limited to 'Lib/py_compile.py')
-rw-r--r-- | Lib/py_compile.py | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/Lib/py_compile.py b/Lib/py_compile.py index d241434..e0f98cb 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -72,7 +72,7 @@ def wr_long(f, x): (x >> 16) & 0xff, (x >> 24) & 0xff])) -def compile(file, cfile=None, dfile=None, doraise=False): +def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1): """Byte-compile one Python source file to Python bytecode. :param file: The source file name. @@ -86,6 +86,10 @@ def compile(file, cfile=None, dfile=None, doraise=False): 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. + :param optimize: The optimization level for the compiler. Valid values + are -1, 0, 1 and 2. A value of -1 means to use the optimization + level of the current interpreter, as given by -O command line options. + :return: Path to the resulting byte compiled file. Note that it isn't necessary to byte-compile Python modules for @@ -111,7 +115,8 @@ def compile(file, cfile=None, dfile=None, doraise=False): timestamp = int(os.stat(file).st_mtime) codestring = f.read() try: - codeobject = builtins.compile(codestring, dfile or file,'exec') + codeobject = builtins.compile(codestring, dfile or file, 'exec', + optimize=optimize) except Exception as err: py_exc = PyCompileError(err.__class__, err, dfile or file) if doraise: @@ -120,7 +125,10 @@ def compile(file, cfile=None, dfile=None, doraise=False): sys.stderr.write(py_exc.msg + '\n') return if cfile is None: - cfile = imp.cache_from_source(file) + if optimize >= 0: + cfile = imp.cache_from_source(file, debug_override=not optimize) + else: + cfile = imp.cache_from_source(file) try: os.makedirs(os.path.dirname(cfile)) except OSError as error: |