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

Skip to content

Commit 47a4521

Browse files
committed
Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Patch by Jeff Ramnani. Tested with -B, -O and -OO.
1 parent db95c7a commit 47a4521

6 files changed

Lines changed: 34 additions & 12 deletions

File tree

Doc/distutils/apiref.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,9 @@ other utility module.
12041204
.. function:: byte_compile(py_files[, optimize=0, force=0, prefix=None, base_dir=None, verbose=1, dry_run=0, direct=None])
12051205

12061206
Byte-compile a collection of Python source files to either :file:`.pyc` or
1207-
:file:`.pyo` files in the same directory. *py_files* is a list of files to
1208-
compile; any files that don't end in :file:`.py` are silently skipped.
1209-
*optimize* must be one of the following:
1207+
:file:`.pyo` files in a :file:`__pycache__` subdirectory (see :pep:`3147`).
1208+
*py_files* is a list of files to compile; any files that don't end in
1209+
:file:`.py` are silently skipped. *optimize* must be one of the following:
12101210

12111211
* ``0`` - don't optimize (generate :file:`.pyc`)
12121212
* ``1`` - normal optimization (like ``python -O``)
@@ -1231,6 +1231,11 @@ other utility module.
12311231
is used by the script generated in indirect mode; unless you know what you're
12321232
doing, leave it set to ``None``.
12331233

1234+
.. versionchanged:: 3.2.3
1235+
Create ``.pyc`` or ``.pyo`` files with an :func:`import magic tag
1236+
<imp.get_tag>` in their name, in a :file:`__pycache__` subdirectory
1237+
instead of files without tag in the current directory.
1238+
12341239

12351240
.. function:: rfc822_escape(header)
12361241

Lib/distutils/tests/test_build_py.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55
import io
6+
import imp
67
import unittest
78

89
from distutils.command.build_py import build_py
@@ -57,13 +58,15 @@ def test_package_data(self):
5758
self.assertEqual(len(cmd.get_outputs()), 3)
5859
pkgdest = os.path.join(destination, "pkg")
5960
files = os.listdir(pkgdest)
61+
pycache_dir = os.path.join(pkgdest, "__pycache__")
6062
self.assertIn("__init__.py", files)
6163
self.assertIn("README.txt", files)
62-
# XXX even with -O, distutils writes pyc, not pyo; bug?
6364
if sys.dont_write_bytecode:
64-
self.assertNotIn("__init__.pyc", files)
65+
self.assertFalse(os.path.exists(pycache_dir))
6566
else:
66-
self.assertIn("__init__.pyc", files)
67+
# XXX even with -O, distutils writes pyc, not pyo; bug?
68+
pyc_files = os.listdir(pycache_dir)
69+
self.assertIn("__init__.%s.pyc" % imp.get_tag(), pyc_files)
6770

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

Lib/distutils/tests/test_install_lib.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for distutils.command.install_data."""
22
import sys
33
import os
4+
import imp
45
import unittest
56

67
from distutils.command.install_lib import install_lib
@@ -32,18 +33,20 @@ def test_finalize_options(self):
3233
cmd.finalize_options()
3334
self.assertEqual(cmd.optimize, 2)
3435

35-
@unittest.skipUnless(not sys.dont_write_bytecode,
36-
'byte-compile not supported')
36+
@unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
3737
def test_byte_compile(self):
3838
pkg_dir, dist = self.create_dist()
39+
os.chdir(pkg_dir)
3940
cmd = install_lib(dist)
4041
cmd.compile = cmd.optimize = 1
4142

4243
f = os.path.join(pkg_dir, 'foo.py')
4344
self.write_file(f, '# python file')
4445
cmd.byte_compile([f])
45-
self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyc')))
46-
self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyo')))
46+
pyc_file = imp.cache_from_source('foo.py')
47+
pyo_file = imp.cache_from_source('foo.py', debug_override=False)
48+
self.assertTrue(os.path.exists(pyc_file))
49+
self.assertTrue(os.path.exists(pyo_file))
4750

4851
def test_get_outputs(self):
4952
pkg_dir, dist = self.create_dist()

Lib/distutils/util.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
one of the other *util.py modules.
55
"""
66

7-
import sys, os, string, re
7+
import os
8+
import re
9+
import imp
10+
import sys
11+
import string
812
from distutils.errors import DistutilsPlatformError
913
from distutils.dep_util import newer
1014
from distutils.spawn import spawn
@@ -529,7 +533,10 @@ def byte_compile (py_files,
529533
# Terminology from the py_compile module:
530534
# cfile - byte-compiled file
531535
# dfile - purported source filename (same as 'file' by default)
532-
cfile = file + (__debug__ and "c" or "o")
536+
if optimize >= 0:
537+
cfile = imp.cache_from_source(file, debug_override=not optimize)
538+
else:
539+
cfile = imp.cache_from_source(file)
533540
dfile = file
534541
if prefix:
535542
if file[:len(prefix)] != prefix:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ Pierre Quentel
722722
Brian Quinlan
723723
Anders Qvist
724724
Burton Radons
725+
Jeff Ramnani
725726
Brodie Rao
726727
Antti Rasinen
727728
Sridhar Ratnakumar

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Core and Builtins
4343
Library
4444
-------
4545

46+
- Issue #11254: Teach distutils to compile .pyc and .pyo files in
47+
PEP 3147-compliant __pycache__ directories.
48+
4649
- Issue #7367: Fix pkgutil.walk_paths to skip directories whose
4750
contents cannot be read.
4851

0 commit comments

Comments
 (0)