Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a29e4f6

Browse files
committed
Fix packaging byte-compilation to comply with PEP 3147 (#11254).
I want to replace custom byte-compiling function with calls to compileall before 3.3b1, but in the short term it’s good to have this fixed. Adapted from the distutils patch by Jeff Ramnani. I tested with -B, -O and -OO; test_util and test_mixin2to3 fail in -O mode because lib2to3 doesn’t support it.
1 parent 73b1e7d commit a29e4f6

5 files changed

Lines changed: 26 additions & 12 deletions

File tree

Doc/library/packaging.util.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ This module contains various helpers for the other modules.
121121
.. function:: byte_compile(py_files[, optimize=0, force=0, prefix=None, base_dir=None, verbose=1, dry_run=0, direct=None])
122122

123123
Byte-compile a collection of Python source files to either :file:`.pyc` or
124-
:file:`.pyo` files in the same directory. *py_files* is a list of files to
125-
compile; any files that don't end in :file:`.py` are silently skipped.
126-
*optimize* must be one of the following:
124+
:file:`.pyo` files in a :file:`__pycache__` subdirectory (see :pep:`3147`),
125+
or to the same directory when using the distutils2 backport on Python
126+
versions older than 3.2.
127+
128+
*py_files* is a list of files to compile; any files that don't end in
129+
:file:`.py` are silently skipped. *optimize* must be one of the following:
127130

128131
* ``0`` - don't optimize (generate :file:`.pyc`)
129132
* ``1`` - normal optimization (like ``python -O``)

Lib/packaging/tests/test_command_build_py.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import sys
5+
import imp
56

67
from packaging.command.build_py import build_py
78
from packaging.dist import Distribution
@@ -58,13 +59,15 @@ def test_package_data(self):
5859
self.assertEqual(len(cmd.get_outputs()), 3)
5960
pkgdest = os.path.join(destination, "pkg")
6061
files = os.listdir(pkgdest)
62+
pycache_dir = os.path.join(pkgdest, "__pycache__")
6163
self.assertIn("__init__.py", files)
6264
self.assertIn("README.txt", files)
63-
# XXX even with -O, distutils writes pyc, not pyo; bug?
6465
if sys.dont_write_bytecode:
65-
self.assertNotIn("__init__.pyc", files)
66+
self.assertFalse(os.path.exists(pycache_dir))
6667
else:
67-
self.assertIn("__init__.pyc", files)
68+
# XXX even with -O, packaging writes pyc, not pyo; bug?
69+
pyc_files = os.listdir(pycache_dir)
70+
self.assertIn("__init__.%s.pyc" % imp.get_tag(), pyc_files)
6871

6972
def test_empty_package_dir(self):
7073
# See SF 1668596/1720897.

Lib/packaging/tests/test_command_install_lib.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for packaging.command.install_data."""
2-
import sys
32
import os
3+
import sys
4+
import imp
45

56
from packaging.tests import unittest, support
67
from packaging.command.install_lib import install_lib
@@ -36,15 +37,18 @@ def test_finalize_options(self):
3637
@unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
3738
def test_byte_compile(self):
3839
pkg_dir, dist = self.create_dist()
40+
os.chdir(pkg_dir)
3941
cmd = install_lib(dist)
4042
cmd.compile = True
4143
cmd.optimize = 1
4244

4345
f = os.path.join(pkg_dir, 'foo.py')
4446
self.write_file(f, '# python file')
4547
cmd.byte_compile([f])
46-
self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyc')))
47-
self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyo')))
48+
pyc_file = imp.cache_from_source('foo.py')
49+
pyo_file = imp.cache_from_source('foo.py', debug_override=False)
50+
self.assertTrue(os.path.exists(pyc_file))
51+
self.assertTrue(os.path.exists(pyo_file))
4852

4953
def test_get_outputs(self):
5054
pkg_dir, dist = self.create_dist()

Lib/packaging/util.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
import csv
6+
import imp
67
import sys
78
import errno
89
import shutil
@@ -296,7 +297,7 @@ def strtobool(val):
296297
def byte_compile(py_files, optimize=0, force=False, prefix=None,
297298
base_dir=None, verbose=0, dry_run=False, direct=None):
298299
"""Byte-compile a collection of Python source files to either .pyc
299-
or .pyo files in the same directory.
300+
or .pyo files in a __pycache__ subdirectory.
300301
301302
'py_files' is a list of files to compile; any files that don't end in
302303
".py" are silently skipped. 'optimize' must be one of the following:
@@ -415,7 +416,10 @@ def byte_compile(py_files, optimize=0, force=False, prefix=None,
415416
# Terminology from the py_compile module:
416417
# cfile - byte-compiled file
417418
# dfile - purported source filename (same as 'file' by default)
418-
cfile = file + (__debug__ and "c" or "o")
419+
if optimize >= 0:
420+
cfile = imp.cache_from_source(file, debug_override=not optimize)
421+
else:
422+
cfile = imp.cache_from_source(file)
419423
dfile = file
420424
if prefix:
421425
if file[:len(prefix)] != prefix:

Misc/NEWS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ Library
304304
- Fix distutils.sysconfig.get_makefile_filename when Python was configured with
305305
different prefix and exec-prefix.
306306

307-
- Issue #11254: Teach distutils to compile .pyc and .pyo files in
307+
- Issue #11254: Teach distutils and packaging to compile .pyc and .pyo files in
308308
PEP 3147-compliant __pycache__ directories.
309309

310310
- Issue #7367: Fix pkgutil.walk_paths to skip directories whose

0 commit comments

Comments
 (0)