diff Lib/packaging/command/install_lib.py @ 73323:dad02a080bbc

Improve byte-compilation in packaging to be independent of -O or -B. The code I fixed to comply with PEP 3147 still had one bug: When run under python -O, some paths for pyc files would be pyo, because I called imp.cache_from_source without explicit debug_override argument in some places, and under -O that would return .pyo (this is well explained in the imp docs). Now all code (util.byte_compile, build_py, install_lib) can create .pyo files according to options given by users, without interference from the calling Python’s own optimize mode. On a related topic, I also removed the code that prevented byte compilation under python -B. The rationale is that packaging gives control over the creation of pyc files to the user with its own explicit option, and the behavior should not be changed if the calling Python happens to run with -B for whatever reason. I will argue that this is a bug fix and ask to be allowed to backport this change to distutils. Finally, I moved one nugget of information about the --compile and --optimize options from the source into the doc. It clears up a misunderstanding that I (and maybe other people) had.
author Éric Araujo <merwok@netwok.org>
date Thu, 03 Nov 2011 05:08:28 +0100
parents 1cc4d822123b
children c10946a17420
line wrap: on
line diff
--- a/Lib/packaging/command/install_lib.py	Thu Nov 03 00:20:03 2011 +0100
+++ b/Lib/packaging/command/install_lib.py	Thu Nov 03 05:08:28 2011 +0100
@@ -2,7 +2,6 @@
 
 import os
 import imp
-import sys
 import logging
 
 from packaging import logger
@@ -11,25 +10,18 @@
 
 
 # Extension for Python source files.
+# XXX dead code?  most of the codebase checks for literal '.py'
 if hasattr(os, 'extsep'):
     PYTHON_SOURCE_EXTENSION = os.extsep + "py"
 else:
     PYTHON_SOURCE_EXTENSION = ".py"
 
+
 class install_lib(Command):
 
     description = "install all modules (extensions and pure Python)"
 
-    # The byte-compilation options are a tad confusing.  Here are the
-    # possible scenarios:
-    #   1) no compilation at all (--no-compile --no-optimize)
-    #   2) compile .pyc only (--compile --no-optimize; default)
-    #   3) compile .pyc and "level 1" .pyo (--compile --optimize)
-    #   4) compile "level 1" .pyo only (--no-compile --optimize)
-    #   5) compile .pyc and "level 2" .pyo (--compile --optimize-more)
-    #   6) compile "level 2" .pyo only (--no-compile --optimize-more)
-    #
-    # The UI for this is two option, 'compile' and 'optimize'.
+    # The options for controlling byte compilations are two independent sets:
     # 'compile' is strictly boolean, and only decides whether to
     # generate .pyc files.  'optimize' is three-way (0, 1, or 2), and
     # decides both whether to generate .pyo files and what level of
@@ -37,7 +29,7 @@
 
     user_options = [
         ('install-dir=', 'd', "directory to install to"),
-        ('build-dir=','b', "build directory (where to install from)"),
+        ('build-dir=', 'b', "build directory (where to install from)"),
         ('force', 'f', "force installation (overwrite existing files)"),
         ('compile', 'c', "compile .py to .pyc [default]"),
         ('no-compile', None, "don't compile .py files"),
@@ -48,7 +40,8 @@
         ]
 
     boolean_options = ['force', 'compile', 'skip-build']
-    negative_opt = {'no-compile' : 'compile'}
+
+    negative_opt = {'no-compile': 'compile'}
 
     def initialize_options(self):
         # let the 'install_dist' command dictate our installation directory
@@ -66,7 +59,8 @@
         self.set_undefined_options('install_dist',
                                    ('build_lib', 'build_dir'),
                                    ('install_lib', 'install_dir'),
-                                   'force', 'compile', 'optimize', 'skip_build')
+                                   'force', 'compile', 'optimize',
+                                   'skip_build')
 
         if self.compile is None:
             self.compile = True
@@ -115,14 +109,6 @@
         return outfiles
 
     def byte_compile(self, files):
-        if sys.dont_write_bytecode:
-            # XXX do we want this?  because a Python runs without bytecode
-            # doesn't mean that the *dists should not contain bytecode
-            #--or does it?
-            logger.warning('%s: byte-compiling is disabled, skipping.',
-                           self.get_command_name())
-            return
-
         from packaging.util import byte_compile  # FIXME use compileall
 
         # Get the "--root" directory supplied to the "install_dist" command,
@@ -138,13 +124,11 @@
         if self.compile:
             byte_compile(files, optimize=0,
                          force=self.force, prefix=install_root,
-                         dry_run=self.dry_run)
+                         verbose=verbose, dry_run=self.dry_run)
         if self.optimize > 0:
             byte_compile(files, optimize=self.optimize,
                          force=self.force, prefix=install_root,
-                         verbose=verbose,
-                         dry_run=self.dry_run)
-
+                         verbose=verbose, dry_run=self.dry_run)
 
     # -- Utility methods -----------------------------------------------
 
@@ -173,14 +157,14 @@
             if ext != PYTHON_SOURCE_EXTENSION:
                 continue
             if self.compile:
-                bytecode_files.append(imp.cache_from_source(py_file))
+                bytecode_files.append(imp.cache_from_source(
+                    py_file, debug_override=True))
             if self.optimize > 0:
                 bytecode_files.append(imp.cache_from_source(
                     py_file, debug_override=False))
 
         return bytecode_files
 
-
     # -- External interface --------------------------------------------
     # (called by outsiders)