]> granicus.if.org Git - python/commitdiff
Issue #7071: byte-compilation in Distutils now looks at sys.dont_write_bytecode
authorTarek Ziadé <ziade.tarek@gmail.com>
Sat, 24 Oct 2009 15:10:37 +0000 (15:10 +0000)
committerTarek Ziadé <ziade.tarek@gmail.com>
Sat, 24 Oct 2009 15:10:37 +0000 (15:10 +0000)
Lib/distutils/command/build_py.py
Lib/distutils/command/install_lib.py
Lib/distutils/errors.py
Lib/distutils/tests/test_build_py.py
Lib/distutils/tests/test_install_lib.py
Lib/distutils/tests/test_util.py
Lib/distutils/util.py
Misc/NEWS

index 5d249f3ddce46f20fb76197caea541bfc8816793..9b9f551830015486ecefcb3b2cafae92d43e96f2 100644 (file)
@@ -5,6 +5,7 @@ Implements the Distutils 'build_py' command."""
 __revision__ = "$Id$"
 
 import os
+import sys
 from glob import glob
 
 from distutils.core import Command
@@ -372,6 +373,10 @@ class build_py(Command):
                 self.build_module(module, module_file, package)
 
     def byte_compile(self, files):
+        if sys.dont_write_bytecode:
+            self.warn('byte-compile not supported on this platform, skipping.')
+            return
+
         from distutils.util import byte_compile
         prefix = self.build_lib
         if prefix[-1] != os.sep:
index 411db79cc2482103a5cea8708841a55d6a7244db..7b4b45be79f025630d12d455d9f0c450bba378eb 100644 (file)
@@ -6,6 +6,8 @@ Implements the Distutils 'install_lib' command
 __revision__ = "$Id$"
 
 import os
+import sys
+
 from distutils.core import Command
 from distutils.errors import DistutilsOptionError
 
@@ -118,6 +120,10 @@ class install_lib(Command):
         return outfiles
 
     def byte_compile(self, files):
+        if sys.dont_write_bytecode:
+            self.warn('byte-compile not supported on this platform, skipping.')
+            return
+
         from distutils.util import byte_compile
 
         # Get the "--root" directory supplied to the "install" command,
index 963d83377c08f23ec39e05b43eacca0f5a329225..acecacccb5641e92f4fcef936e212fee0ec6d0ca 100644 (file)
@@ -74,6 +74,8 @@ class DistutilsInternalError (DistutilsError):
 class DistutilsTemplateError (DistutilsError):
     """Syntax error in a file list template."""
 
+class DistutilsByteCompileError(DistutilsError):
+    """Byte compile error."""
 
 # Exception classes used by the CCompiler implementation classes
 class CCompilerError (Exception):
index c815d8185ebdbe9b84973af5b2c675562d3cb94a..e8c7ca921c0e49f4e8f6a48d1431ebf55a45c61b 100644 (file)
@@ -89,6 +89,22 @@ class BuildPyTestCase(support.TempdirManager,
             os.chdir(cwd)
             sys.stdout = sys.__stdout__
 
+    def test_dont_write_bytecode(self):
+        # makes sure byte_compile is not used
+        pkg_dir, dist = self.create_dist()
+        cmd = build_py(dist)
+        cmd.compile = 1
+        cmd.optimize = 1
+
+        old_dont_write_bytecode = sys.dont_write_bytecode
+        sys.dont_write_bytecode = True
+        try:
+            cmd.byte_compile([])
+        finally:
+            sys.dont_write_bytecode = old_dont_write_bytecode
+
+        self.assertTrue('byte-compile not supported ' in self.logs[0][1])
+
 def test_suite():
     return unittest.makeSuite(BuildPyTestCase)
 
index b2185b8442e230df4c7978a73fc5896aff915c9d..fab66d15ae82f5488b8045f29e5992512506b2fb 100644 (file)
@@ -31,6 +31,8 @@ class InstallLibTestCase(support.TempdirManager,
         cmd.finalize_options()
         self.assertEquals(cmd.optimize, 2)
 
+    @unittest.skipUnless(not sys.dont_write_bytecode,
+                         'byte-compile not supported')
     def test_byte_compile(self):
         pkg_dir, dist = self.create_dist()
         cmd = install_lib(dist)
@@ -76,6 +78,21 @@ class InstallLibTestCase(support.TempdirManager,
         # get_input should return 2 elements
         self.assertEquals(len(cmd.get_inputs()), 2)
 
+    def test_dont_write_bytecode(self):
+        # makes sure byte_compile is not used
+        pkg_dir, dist = self.create_dist()
+        cmd = install_lib(dist)
+        cmd.compile = 1
+        cmd.optimize = 1
+
+        old_dont_write_bytecode = sys.dont_write_bytecode
+        sys.dont_write_bytecode = True
+        try:
+            cmd.byte_compile([])
+        finally:
+            sys.dont_write_bytecode = old_dont_write_bytecode
+
+        self.assertTrue('byte-compile not supported ' in self.logs[0][1])
 
 def test_suite():
     return unittest.makeSuite(InstallLibTestCase)
index 4099c950ad4f31ade7783bb3fbc2d6f48c1153d8..4da1e651ed52b3bf7b3d4d2ecc8256b48c24cad2 100644 (file)
@@ -1,7 +1,4 @@
 """Tests for distutils.util."""
-# not covered yet:
-#    - byte_compile
-#
 import os
 import sys
 import unittest
@@ -9,11 +6,12 @@ from copy import copy
 from StringIO import StringIO
 import subprocess
 
-from distutils.errors import DistutilsPlatformError
+from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
 from distutils.util import (get_platform, convert_path, change_root,
                             check_environ, split_quoted, strtobool,
                             rfc822_escape, get_compiler_versions,
-                            _find_exe_version, _MAC_OS_X_LD_VERSION)
+                            _find_exe_version, _MAC_OS_X_LD_VERSION,
+                            byte_compile)
 from distutils import util
 from distutils.sysconfig import get_config_vars
 from distutils import sysconfig
@@ -349,6 +347,16 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
         res = get_compiler_versions()
         self.assertEquals(res[2], None)
 
+    def test_dont_write_bytecode(self):
+        # makes sure byte_compile raise a DistutilsError
+        # if sys.dont_write_bytecode is True
+        old_dont_write_bytecode = sys.dont_write_bytecode
+        sys.dont_write_bytecode = True
+        try:
+            self.assertRaises(DistutilsByteCompileError, byte_compile, [])
+        finally:
+            sys.dont_write_bytecode = False
+
 def test_suite():
     return unittest.makeSuite(UtilTestCase)
 
index fe6851cb8715bb314a5095ef96b67a9ded259a48..a87ef44a272ae60cff4ffc72adb14e531ba5580b 100644 (file)
@@ -13,6 +13,7 @@ from distutils.dep_util import newer
 from distutils.spawn import spawn, find_executable
 from distutils import log
 from distutils.version import LooseVersion
+from distutils.errors import DistutilsByteCompileError
 
 def get_platform():
     """Return a string that identifies the current platform.
@@ -445,6 +446,10 @@ def byte_compile(py_files, optimize=0, force=0, prefix=None, base_dir=None,
     generated in indirect mode; unless you know what you're doing, leave
     it set to None.
     """
+    # nothing is done if sys.dont_write_bytecode is True
+    if sys.dont_write_bytecode:
+        raise DistutilsByteCompileError('byte-compiling not supported.')
+
     # First, if the caller didn't force us into direct or indirect mode,
     # figure out which mode we should be in.  We take a conservative
     # approach: choose direct mode *only* if the current interpreter is
index 052ec4843d8f622ba96234ac129541aeeebf7acd..e26cd72204ea7fcb189a422c140fdbae0912595a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -418,6 +418,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7071: byte-compilation in Distutils is now done with respect to
+  sys.dont_write_bytecode.
+
 - Issue #7066: archive_util.make_archive now restores the cwd if an error is 
   raised. Initial patch by Ezio Melotti.