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

Skip to content

Commit e640521

Browse files
committed
Merge follow-up for #11254 and other changes from 3.2
2 parents 8808015 + c465b2f commit e640521

10 files changed

Lines changed: 134 additions & 89 deletions

File tree

Doc/using/cmdline.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ These environment variables influence Python's behavior.
455455
.. envvar:: PYTHONDONTWRITEBYTECODE
456456

457457
If this is set, Python won't try to write ``.pyc`` or ``.pyo`` files on the
458-
import of source modules.
458+
import of source modules. This is equivalent to specifying the :option:`-B`
459+
option.
459460

460461

461462
.. envvar:: PYTHONIOENCODING

Lib/_pyio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ class TextIOWrapper(TextIOBase):
14551455
enabled. With this enabled, on input, the lines endings '\n', '\r',
14561456
or '\r\n' are translated to '\n' before being returned to the
14571457
caller. Conversely, on output, '\n' is translated to the system
1458-
default line seperator, os.linesep. If newline is any other of its
1458+
default line separator, os.linesep. If newline is any other of its
14591459
legal values, that newline becomes the newline when the file is read
14601460
and it is returned untranslated. On output, '\n' is converted to the
14611461
newline.

Lib/distutils/command/build_py.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
Implements the Distutils 'build_py' command."""
44

5-
import sys, os
5+
import os
6+
import imp
67
import sys
78
from glob import glob
89

@@ -311,9 +312,11 @@ def get_outputs(self, include_bytecode=1):
311312
outputs.append(filename)
312313
if include_bytecode:
313314
if self.compile:
314-
outputs.append(filename + "c")
315+
outputs.append(imp.cache_from_source(filename,
316+
debug_override=True))
315317
if self.optimize > 0:
316-
outputs.append(filename + "o")
318+
outputs.append(imp.cache_from_source(filename,
319+
debug_override=False))
317320

318321
outputs += [
319322
os.path.join(build_dir, filename)

Lib/distutils/command/install_lib.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
(install all Python modules)."""
55

66
import os
7+
import imp
78
import sys
89

910
from distutils.core import Command
@@ -164,9 +165,11 @@ def _bytecode_filenames(self, py_filenames):
164165
if ext != PYTHON_SOURCE_EXTENSION:
165166
continue
166167
if self.compile:
167-
bytecode_files.append(py_file + "c")
168+
bytecode_files.append(imp.cache_from_source(
169+
py_file, debug_override=True))
168170
if self.optimize > 0:
169-
bytecode_files.append(py_file + "o")
171+
bytecode_files.append(imp.cache_from_source(
172+
py_file, debug_override=False))
170173

171174
return bytecode_files
172175

Lib/distutils/tests/test_bdist_dumb.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Tests for distutils.command.bdist_dumb."""
22

3-
import unittest
4-
import sys
53
import os
4+
import imp
5+
import sys
6+
import zipfile
7+
import unittest
68
from test.support import run_unittest
79

810
from distutils.core import Distribution
@@ -72,15 +74,24 @@ def test_simple_built(self):
7274

7375
# see what we have
7476
dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
75-
base = "%s.%s" % (dist.get_fullname(), cmd.plat_name)
77+
base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name)
7678
if os.name == 'os2':
7779
base = base.replace(':', '-')
7880

79-
wanted = ['%s.zip' % base]
80-
self.assertEqual(dist_created, wanted)
81+
self.assertEqual(dist_created, [base])
8182

8283
# now let's check what we have in the zip file
83-
# XXX to be done
84+
fp = zipfile.ZipFile(os.path.join('dist', base))
85+
try:
86+
contents = fp.namelist()
87+
finally:
88+
fp.close()
89+
90+
contents = sorted(os.path.basename(fn) for fn in contents)
91+
wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2],
92+
'foo.%s.pyc' % imp.get_tag(),
93+
'foo.py']
94+
self.assertEqual(contents, sorted(wanted))
8495

8596
def test_suite():
8697
return unittest.makeSuite(BuildDumbTestCase)

Lib/distutils/tests/test_build_py.py

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

33
import os
44
import sys
5-
import io
65
import imp
76
import unittest
87

@@ -54,7 +53,6 @@ def test_package_data(self):
5453
# This makes sure the list of outputs includes byte-compiled
5554
# files for Python modules but not for package data files
5655
# (there shouldn't *be* byte-code files for those!).
57-
#
5856
self.assertEqual(len(cmd.get_outputs()), 3)
5957
pkgdest = os.path.join(destination, "pkg")
6058
files = os.listdir(pkgdest)
@@ -64,15 +62,11 @@ def test_package_data(self):
6462
if sys.dont_write_bytecode:
6563
self.assertFalse(os.path.exists(pycache_dir))
6664
else:
67-
# XXX even with -O, distutils writes pyc, not pyo; bug?
6865
pyc_files = os.listdir(pycache_dir)
6966
self.assertIn("__init__.%s.pyc" % imp.get_tag(), pyc_files)
7067

7168
def test_empty_package_dir(self):
72-
# See SF 1668596/1720897.
73-
cwd = os.getcwd()
74-
75-
# create the distribution files.
69+
# See bugs #1668596/#1720897
7670
sources = self.mkdtemp()
7771
open(os.path.join(sources, "__init__.py"), "w").close()
7872

@@ -81,30 +75,55 @@ def test_empty_package_dir(self):
8175
open(os.path.join(testdir, "testfile"), "w").close()
8276

8377
os.chdir(sources)
84-
old_stdout = sys.stdout
85-
sys.stdout = io.StringIO()
78+
dist = Distribution({"packages": ["pkg"],
79+
"package_dir": {"pkg": ""},
80+
"package_data": {"pkg": ["doc/*"]}})
81+
# script_name need not exist, it just need to be initialized
82+
dist.script_name = os.path.join(sources, "setup.py")
83+
dist.script_args = ["build"]
84+
dist.parse_command_line()
8685

8786
try:
88-
dist = Distribution({"packages": ["pkg"],
89-
"package_dir": {"pkg": ""},
90-
"package_data": {"pkg": ["doc/*"]}})
91-
# script_name need not exist, it just need to be initialized
92-
dist.script_name = os.path.join(sources, "setup.py")
93-
dist.script_args = ["build"]
94-
dist.parse_command_line()
95-
96-
try:
97-
dist.run_commands()
98-
except DistutilsFileError:
99-
self.fail("failed package_data test when package_dir is ''")
100-
finally:
101-
# Restore state.
102-
os.chdir(cwd)
103-
sys.stdout = old_stdout
87+
dist.run_commands()
88+
except DistutilsFileError:
89+
self.fail("failed package_data test when package_dir is ''")
90+
91+
@unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
92+
def test_byte_compile(self):
93+
project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
94+
os.chdir(project_dir)
95+
self.write_file('boiledeggs.py', 'import antigravity')
96+
cmd = build_py(dist)
97+
cmd.compile = 1
98+
cmd.build_lib = 'here'
99+
cmd.finalize_options()
100+
cmd.run()
101+
102+
found = os.listdir(cmd.build_lib)
103+
self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
104+
found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
105+
self.assertEqual(found, ['boiledeggs.%s.pyc' % imp.get_tag()])
106+
107+
@unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
108+
def test_byte_compile_optimized(self):
109+
project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
110+
os.chdir(project_dir)
111+
self.write_file('boiledeggs.py', 'import antigravity')
112+
cmd = build_py(dist)
113+
cmd.compile = 0
114+
cmd.optimize = 1
115+
cmd.build_lib = 'here'
116+
cmd.finalize_options()
117+
cmd.run()
118+
119+
found = os.listdir(cmd.build_lib)
120+
self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
121+
found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
122+
self.assertEqual(sorted(found), ['boiledeggs.%s.pyo' % imp.get_tag()])
104123

105124
def test_dont_write_bytecode(self):
106125
# makes sure byte_compile is not used
107-
pkg_dir, dist = self.create_dist()
126+
dist = self.create_dist()[1]
108127
cmd = build_py(dist)
109128
cmd.compile = 1
110129
cmd.optimize = 1
@@ -118,6 +137,7 @@ def test_dont_write_bytecode(self):
118137

119138
self.assertIn('byte-compiling is disabled', self.logs[0][1])
120139

140+
121141
def test_suite():
122142
return unittest.makeSuite(BuildPyTestCase)
123143

Lib/distutils/tests/test_install.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for distutils.command.install."""
22

33
import os
4+
import imp
45
import sys
56
import unittest
67
import site
@@ -67,10 +68,7 @@ def check_path(got, expected):
6768
check_path(cmd.install_data, destination)
6869

6970
def test_user_site(self):
70-
# site.USER_SITE was introduced in 2.6
71-
if sys.version < '2.6':
72-
return
73-
71+
# test install with --user
7472
# preparing the environment for the test
7573
self.old_user_base = site.USER_BASE
7674
self.old_user_site = site.USER_SITE
@@ -87,34 +85,32 @@ def _expanduser(path):
8785
self.old_expand = os.path.expanduser
8886
os.path.expanduser = _expanduser
8987

90-
try:
91-
# this is the actual test
92-
self._test_user_site()
93-
finally:
88+
def cleanup():
9489
site.USER_BASE = self.old_user_base
9590
site.USER_SITE = self.old_user_site
9691
install_module.USER_BASE = self.old_user_base
9792
install_module.USER_SITE = self.old_user_site
9893
os.path.expanduser = self.old_expand
9994

100-
def _test_user_site(self):
95+
self.addCleanup(cleanup)
96+
10197
for key in ('nt_user', 'unix_user', 'os2_home'):
102-
self.assertTrue(key in INSTALL_SCHEMES)
98+
self.assertIn(key, INSTALL_SCHEMES)
10399

104100
dist = Distribution({'name': 'xx'})
105101
cmd = install(dist)
106102

107103
# making sure the user option is there
108104
options = [name for name, short, lable in
109105
cmd.user_options]
110-
self.assertTrue('user' in options)
106+
self.assertIn('user', options)
111107

112108
# setting a value
113109
cmd.user = 1
114110

115111
# user base and site shouldn't be created yet
116-
self.assertTrue(not os.path.exists(self.user_base))
117-
self.assertTrue(not os.path.exists(self.user_site))
112+
self.assertFalse(os.path.exists(self.user_base))
113+
self.assertFalse(os.path.exists(self.user_site))
118114

119115
# let's run finalize
120116
cmd.ensure_finalized()
@@ -123,8 +119,8 @@ def _test_user_site(self):
123119
self.assertTrue(os.path.exists(self.user_base))
124120
self.assertTrue(os.path.exists(self.user_site))
125121

126-
self.assertTrue('userbase' in cmd.config_vars)
127-
self.assertTrue('usersite' in cmd.config_vars)
122+
self.assertIn('userbase', cmd.config_vars)
123+
self.assertIn('usersite', cmd.config_vars)
128124

129125
def test_handle_extra_path(self):
130126
dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
@@ -177,15 +173,16 @@ def test_finalize_options(self):
177173

178174
def test_record(self):
179175
install_dir = self.mkdtemp()
180-
project_dir, dist = self.create_dist(scripts=['hello'])
181-
self.addCleanup(os.chdir, os.getcwd())
176+
project_dir, dist = self.create_dist(py_modules=['hello'],
177+
scripts=['sayhi'])
182178
os.chdir(project_dir)
183-
self.write_file('hello', "print('o hai')")
179+
self.write_file('hello.py', "def main(): print('o hai')")
180+
self.write_file('sayhi', 'from hello import main; main()')
184181

185182
cmd = install(dist)
186183
dist.command_obj['install'] = cmd
187184
cmd.root = install_dir
188-
cmd.record = os.path.join(project_dir, 'RECORD')
185+
cmd.record = os.path.join(project_dir, 'filelist')
189186
cmd.ensure_finalized()
190187
cmd.run()
191188

@@ -196,15 +193,14 @@ def test_record(self):
196193
f.close()
197194

198195
found = [os.path.basename(line) for line in content.splitlines()]
199-
expected = ['hello',
196+
expected = ['hello.py', 'hello.%s.pyc' % imp.get_tag(), 'sayhi',
200197
'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
201198
self.assertEqual(found, expected)
202199

203200
def test_record_extensions(self):
204201
install_dir = self.mkdtemp()
205202
project_dir, dist = self.create_dist(ext_modules=[
206203
Extension('xx', ['xxmodule.c'])])
207-
self.addCleanup(os.chdir, os.getcwd())
208204
os.chdir(project_dir)
209205
support.copy_xxmodule_c(project_dir)
210206

@@ -216,7 +212,7 @@ def test_record_extensions(self):
216212
dist.command_obj['install'] = cmd
217213
dist.command_obj['build_ext'] = buildextcmd
218214
cmd.root = install_dir
219-
cmd.record = os.path.join(project_dir, 'RECORD')
215+
cmd.record = os.path.join(project_dir, 'filelist')
220216
cmd.ensure_finalized()
221217
cmd.run()
222218

@@ -242,6 +238,7 @@ def test_debug_mode(self):
242238
install_module.DEBUG = False
243239
self.assertTrue(len(self.logs) > old_logs_len)
244240

241+
245242
def test_suite():
246243
return unittest.makeSuite(InstallTestCase)
247244

0 commit comments

Comments
 (0)