1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
From: Stefano Rivera <stefanor@debian.org>
Date: Sat, 7 Oct 2017 09:38:57 +0200
Subject: PEP3147 distutils patches
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Backported from cpython.
Origin: cpython, http://hg.python.org/cpython/rev/27a36b05caed
Origin: cpython, http://hg.python.org/cpython/rev/651e84363001
Origin: cpython, http://hg.python.org/cpython/rev/ea926dff958f
Bug-cpython: http://bugs.python.org/issue11254
Author: Jeff Ramnani
Author: Éric Araujo <merwok@netwok.org>
Author: Stefano Rivera <stefanor@debian.org>
Last-Update: 2011-12-20
---
lib-python/2.7/distutils/command/build_py.py | 7 +-
lib-python/2.7/distutils/command/install_lib.py | 7 +-
lib-python/2.7/distutils/tests/test_build_py.py | 76 ++++++++++++++--------
lib-python/2.7/distutils/tests/test_install_lib.py | 60 ++++++++++-------
lib-python/2.7/distutils/util.py | 17 +++--
5 files changed, 109 insertions(+), 58 deletions(-)
diff --git a/lib-python/2.7/distutils/command/build_py.py b/lib-python/2.7/distutils/command/build_py.py
index c123c62..f4cc5c9 100644
--- a/lib-python/2.7/distutils/command/build_py.py
+++ b/lib-python/2.7/distutils/command/build_py.py
@@ -5,6 +5,7 @@ Implements the Distutils 'build_py' command."""
__revision__ = "$Id$"
import os
+import imp
import sys
from glob import glob
@@ -314,9 +315,11 @@ class build_py(Command):
outputs.append(filename)
if include_bytecode:
if self.compile:
- outputs.append(filename + "c")
+ outputs.append(imp.cache_from_source(filename,
+ debug_override=True))
if self.optimize > 0:
- outputs.append(filename + "o")
+ outputs.append(imp.cache_from_source(filename,
+ debug_override=False))
outputs += [
os.path.join(build_dir, filename)
diff --git a/lib-python/2.7/distutils/command/install_lib.py b/lib-python/2.7/distutils/command/install_lib.py
index 043e8b6..1f6141d 100644
--- a/lib-python/2.7/distutils/command/install_lib.py
+++ b/lib-python/2.7/distutils/command/install_lib.py
@@ -6,6 +6,7 @@ Implements the Distutils 'install_lib' command
__revision__ = "$Id$"
import os
+import imp
import sys
from distutils.core import Command
@@ -169,9 +170,11 @@ class install_lib(Command):
if ext != PYTHON_SOURCE_EXTENSION:
continue
if self.compile:
- bytecode_files.append(py_file + "c")
+ bytecode_files.append(imp.cache_from_source(
+ py_file, debug_override=True))
if self.optimize > 0:
- bytecode_files.append(py_file + "o")
+ bytecode_files.append(imp.cache_from_source(
+ py_file, debug_override=False))
return bytecode_files
diff --git a/lib-python/2.7/distutils/tests/test_build_py.py b/lib-python/2.7/distutils/tests/test_build_py.py
index c4498bc..7c12dbf 100644
--- a/lib-python/2.7/distutils/tests/test_build_py.py
+++ b/lib-python/2.7/distutils/tests/test_build_py.py
@@ -2,7 +2,7 @@
import os
import sys
-import StringIO
+import imp
import unittest
from distutils.command.build_py import build_py
@@ -59,17 +59,16 @@ class BuildPyTestCase(support.TempdirManager,
files = os.listdir(pkgdest)
self.assertIn("__init__.py", files)
self.assertIn("README.txt", files)
+ pycache_dir = os.path.join(pkgdest, "__pycache__")
+ pyc_files = os.listdir(pycache_dir)
# XXX even with -O, distutils writes pyc, not pyo; bug?
if sys.dont_write_bytecode:
- self.assertNotIn("__init__.pyc", files)
+ self.assertNotIn("__init__.%s.pyc" % imp.get_tag(), pyc_files)
else:
- self.assertIn("__init__.pyc", files)
+ self.assertIn("__init__.%s.pyc" % imp.get_tag(), pyc_files)
def test_empty_package_dir(self):
- # See SF 1668596/1720897.
- cwd = os.getcwd()
-
- # create the distribution files.
+ # See bugs #1668596/#1720897
sources = self.mkdtemp()
open(os.path.join(sources, "__init__.py"), "w").close()
@@ -78,26 +77,51 @@ class BuildPyTestCase(support.TempdirManager,
open(os.path.join(testdir, "testfile"), "w").close()
os.chdir(sources)
- old_stdout = sys.stdout
- sys.stdout = StringIO.StringIO()
+ dist = Distribution({"packages": ["pkg"],
+ "package_dir": {"pkg": ""},
+ "package_data": {"pkg": ["doc/*"]}})
+ # script_name need not exist, it just need to be initialized
+ dist.script_name = os.path.join(sources, "setup.py")
+ dist.script_args = ["build"]
+ dist.parse_command_line()
try:
- dist = Distribution({"packages": ["pkg"],
- "package_dir": {"pkg": ""},
- "package_data": {"pkg": ["doc/*"]}})
- # script_name need not exist, it just need to be initialized
- dist.script_name = os.path.join(sources, "setup.py")
- dist.script_args = ["build"]
- dist.parse_command_line()
-
- try:
- dist.run_commands()
- except DistutilsFileError:
- self.fail("failed package_data test when package_dir is ''")
- finally:
- # Restore state.
- os.chdir(cwd)
- sys.stdout = old_stdout
+ dist.run_commands()
+ except DistutilsFileError:
+ self.fail("failed package_data test when package_dir is ''")
+
+ @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
+ def test_byte_compile(self):
+ project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
+ os.chdir(project_dir)
+ self.write_file('boiledeggs.py', 'import antigravity')
+ cmd = build_py(dist)
+ cmd.compile = 1
+ cmd.build_lib = 'here'
+ cmd.finalize_options()
+ cmd.run()
+
+ found = os.listdir(cmd.build_lib)
+ self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
+ found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
+ self.assertEqual(found, ['boiledeggs.%s.pyc' % imp.get_tag()])
+
+ @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
+ def test_byte_compile_optimized(self):
+ project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
+ os.chdir(project_dir)
+ self.write_file('boiledeggs.py', 'import antigravity')
+ cmd = build_py(dist)
+ cmd.compile = 0
+ cmd.optimize = 1
+ cmd.build_lib = 'here'
+ cmd.finalize_options()
+ cmd.run()
+
+ found = os.listdir(cmd.build_lib)
+ self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
+ found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
+ self.assertEqual(sorted(found), ['boiledeggs.%s.pyo' % imp.get_tag()])
def test_dir_in_package_data(self):
"""
@@ -132,7 +156,7 @@ class BuildPyTestCase(support.TempdirManager,
def test_dont_write_bytecode(self):
# makes sure byte_compile is not used
- pkg_dir, dist = self.create_dist()
+ dist = self.create_dist()[1]
cmd = build_py(dist)
cmd.compile = 1
cmd.optimize = 1
diff --git a/lib-python/2.7/distutils/tests/test_install_lib.py b/lib-python/2.7/distutils/tests/test_install_lib.py
index 0defbd6..446bdba 100644
--- a/lib-python/2.7/distutils/tests/test_install_lib.py
+++ b/lib-python/2.7/distutils/tests/test_install_lib.py
@@ -1,6 +1,7 @@
"""Tests for distutils.command.install_data."""
import os
import sys
+import imp
import unittest
from distutils.command.install_lib import install_lib
@@ -15,7 +16,7 @@ class InstallLibTestCase(support.TempdirManager,
unittest.TestCase):
def test_finalize_options(self):
- pkg_dir, dist = self.create_dist()
+ dist = self.create_dist()[1]
cmd = install_lib(dist)
cmd.finalize_options()
@@ -44,49 +45,62 @@ class InstallLibTestCase(support.TempdirManager,
@unittest.skipIf(sys.dont_write_bytecode, 'byte-compile not enabled')
def test_byte_compile(self):
- pkg_dir = self._setup_byte_compile()
- if sys.flags.optimize < 1:
- self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyc')))
- else:
- self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyo')))
+ project_dir, dist = self.create_dist()
+ os.chdir(project_dir)
+ cmd = install_lib(dist)
+ cmd.compile = cmd.optimize = 1
+
+ f = os.path.join(project_dir, 'foo.py')
+ self.write_file(f, '# python file')
+ cmd.byte_compile([f])
+ pyc_file = imp.cache_from_source('foo.py', debug_override=True)
+ pyo_file = imp.cache_from_source('foo.py', debug_override=False)
+ self.assertTrue(os.path.exists(pyc_file))
+ self.assertTrue(os.path.exists(pyo_file))
def test_get_outputs(self):
- pkg_dir, dist = self.create_dist()
+ project_dir, dist = self.create_dist()
+ os.chdir(project_dir)
+ os.mkdir('spam')
cmd = install_lib(dist)
# setting up a dist environment
cmd.compile = cmd.optimize = 1
- cmd.install_dir = pkg_dir
- f = os.path.join(pkg_dir, 'foo.py')
- self.write_file(f, '# python file')
- cmd.distribution.py_modules = [pkg_dir]
+ cmd.install_dir = self.mkdtemp()
+ f = os.path.join(project_dir, 'spam', '__init__.py')
+ self.write_file(f, '# python package')
cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
- cmd.distribution.packages = [pkg_dir]
+ cmd.distribution.packages = ['spam']
cmd.distribution.script_name = 'setup.py'
- # get_output should return 4 elements
- self.assertGreaterEqual(len(cmd.get_outputs()), 2)
+ # get_outputs should return 4 elements: spam/__init__.py, .pyc and
+ # .pyo, foo.import-tag-abiflags.so / foo.pyd
+ outputs = cmd.get_outputs()
+ self.assertEqual(len(outputs), 4, outputs)
def test_get_inputs(self):
- pkg_dir, dist = self.create_dist()
+ project_dir, dist = self.create_dist()
+ os.chdir(project_dir)
+ os.mkdir('spam')
cmd = install_lib(dist)
# setting up a dist environment
cmd.compile = cmd.optimize = 1
- cmd.install_dir = pkg_dir
- f = os.path.join(pkg_dir, 'foo.py')
- self.write_file(f, '# python file')
- cmd.distribution.py_modules = [pkg_dir]
+ cmd.install_dir = self.mkdtemp()
+ f = os.path.join(project_dir, 'spam', '__init__.py')
+ self.write_file(f, '# python package')
cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
- cmd.distribution.packages = [pkg_dir]
+ cmd.distribution.packages = ['spam']
cmd.distribution.script_name = 'setup.py'
- # get_input should return 2 elements
- self.assertEqual(len(cmd.get_inputs()), 2)
+ # get_inputs should return 2 elements: spam/__init__.py and
+ # foo.import-tag-abiflags.so / foo.pyd
+ inputs = cmd.get_inputs()
+ self.assertEqual(len(inputs), 2, inputs)
def test_dont_write_bytecode(self):
# makes sure byte_compile is not used
- pkg_dir, dist = self.create_dist()
+ dist = self.create_dist()[1]
cmd = install_lib(dist)
cmd.compile = 1
cmd.optimize = 1
diff --git a/lib-python/2.7/distutils/util.py b/lib-python/2.7/distutils/util.py
index 2b4d784..5792df7 100644
--- a/lib-python/2.7/distutils/util.py
+++ b/lib-python/2.7/distutils/util.py
@@ -6,7 +6,11 @@ one of the other *util.py modules.
__revision__ = "$Id$"
-import sys, os, string, re
+import os
+import re
+import imp
+import sys
+import string
from distutils.errors import DistutilsPlatformError
from distutils.dep_util import newer
from distutils.spawn import spawn
@@ -331,9 +335,9 @@ def byte_compile (py_files,
verbose=1, dry_run=0,
direct=None):
"""Byte-compile a collection of Python source files to either .pyc
- or .pyo files in the same directory. 'py_files' is a list of files
- to compile; any files that don't end in ".py" are silently skipped.
- 'optimize' must be one of the following:
+ or .pyo files in a __pycache__ subdirectory. 'py_files' is a list
+ of files to compile; any files that don't end in ".py" are silently
+ skipped. 'optimize' must be one of the following:
0 - don't optimize (generate .pyc)
1 - normal optimization (like "python -O")
2 - extra optimization (like "python -OO")
@@ -445,7 +449,10 @@ byte_compile(files, optimize=%r, force=%r,
# Terminology from the py_compile module:
# cfile - byte-compiled file
# dfile - purported source filename (same as 'file' by default)
- cfile = file + (__debug__ and "c" or "o")
+ if optimize >= 0:
+ cfile = imp.cache_from_source(file, debug_override=not optimize)
+ else:
+ cfile = imp.cache_from_source(file)
dfile = file
if prefix:
if file[:len(prefix)] != prefix:
|