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

Skip to content

Commit 9ab358a

Browse files
committed
Issue #16826: Don't check for PYTHONCASEOK when using -E.
This commit fixes a regression that sneaked into Python 3.3 where importlib was not respecting -E when checking for the PYTHONCASEOK environment variable.
2 parents c6171e4 + d151da9 commit 9ab358a

5 files changed

Lines changed: 3211 additions & 3134 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ def _make_relax_case():
2929
if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
3030
def _relax_case():
3131
"""True if filenames must be checked case-insensitively."""
32-
return b'PYTHONCASEOK' in _os.environ
32+
if sys.flags.ignore_environment:
33+
return False
34+
else:
35+
return b'PYTHONCASEOK' in _os.environ
3336
else:
3437
def _relax_case():
3538
"""True if filenames must be checked case-insensitively."""

Lib/test/test_importlib/extension/test_case_sensitivity.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from importlib import machinery
77
from .. import util
88
from . import util as ext_util
9-
9+
import os
10+
import subprocess
1011

1112
@util.case_insensitive_tests
1213
class ExtensionModuleCaseSensitivityTest(unittest.TestCase):
@@ -30,14 +31,34 @@ def test_case_sensitive(self):
3031
self.assertIsNone(loader)
3132

3233
def test_case_insensitivity(self):
33-
with support.EnvironmentVarGuard() as env:
34-
env.set('PYTHONCASEOK', '1')
35-
if b'PYTHONCASEOK' not in _bootstrap._os.environ:
36-
self.skipTest('os.environ changes not reflected in '
37-
'_os.environ')
38-
loader = self.find_module()
39-
self.assertTrue(hasattr(loader, 'load_module'))
34+
find_snippet = """if True:
35+
from importlib import _bootstrap
36+
import sys
37+
finder = _bootstrap.FileFinder('{path}',
38+
(_bootstrap.ExtensionFileLoader,
39+
_bootstrap.EXTENSION_SUFFIXES))
40+
loader = finder.find_module('{bad_name}')
41+
print(str(hasattr(loader, 'load_module')))
42+
""".format(bad_name=ext_util.NAME.upper(), path=ext_util.PATH)
43+
44+
newenv = os.environ.copy()
45+
newenv["PYTHONCASEOK"] = "1"
46+
47+
def check_output(expected, extra_arg=None):
48+
args = [sys.executable]
49+
if extra_arg:
50+
args.append(extra_arg)
51+
args.extend(["-c", find_snippet])
52+
p = subprocess.Popen(args, stdout=subprocess.PIPE, env=newenv)
53+
actual = p.communicate()[0].decode().strip()
54+
self.assertEqual(expected, actual)
55+
self.assertEqual(p.wait(), 0)
56+
57+
# Test with PYTHONCASEOK=1.
58+
check_output("True")
4059

60+
# Test with PYTHONCASEOK=1 ignored because of -E.
61+
check_output("False", "-E")
4162

4263

4364

Lib/test/test_importlib/source/test_case_sensitivity.py

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
from test import support as test_support
1010
import unittest
11+
import subprocess
1112

1213

1314
@util.case_insensitive_tests
@@ -50,16 +51,62 @@ def test_sensitive(self):
5051
self.assertIsNone(insensitive)
5152

5253
def test_insensitive(self):
53-
with test_support.EnvironmentVarGuard() as env:
54-
env.set('PYTHONCASEOK', '1')
55-
if b'PYTHONCASEOK' not in _bootstrap._os.environ:
56-
self.skipTest('os.environ changes not reflected in '
57-
'_os.environ')
58-
sensitive, insensitive = self.sensitivity_test()
59-
self.assertTrue(hasattr(sensitive, 'load_module'))
60-
self.assertIn(self.name, sensitive.get_filename(self.name))
61-
self.assertTrue(hasattr(insensitive, 'load_module'))
62-
self.assertIn(self.name, insensitive.get_filename(self.name))
54+
sensitive_pkg = 'sensitive.{0}'.format(self.name)
55+
insensitive_pkg = 'insensitive.{0}'.format(self.name.lower())
56+
context = source_util.create_modules(insensitive_pkg, sensitive_pkg)
57+
with context as mapping:
58+
sensitive_path = os.path.join(mapping['.root'], 'sensitive')
59+
insensitive_path = os.path.join(mapping['.root'], 'insensitive')
60+
find_snippet = """if True:
61+
import sys
62+
from importlib import machinery
63+
64+
def find(path):
65+
f = machinery.FileFinder(path,
66+
(machinery.SourceFileLoader,
67+
machinery.SOURCE_SUFFIXES),
68+
(machinery.SourcelessFileLoader,
69+
machinery.BYTECODE_SUFFIXES))
70+
return f.find_module('{name}')
71+
72+
sensitive = find('{sensitive_path}')
73+
insensitive = find('{insensitive_path}')
74+
print(str(hasattr(sensitive, 'load_module')))
75+
if hasattr(sensitive, 'load_module'):
76+
print(sensitive.get_filename('{name}'))
77+
else:
78+
print('None')
79+
print(str(hasattr(insensitive, 'load_module')))
80+
if hasattr(insensitive, 'load_module'):
81+
print(insensitive.get_filename('{name}'))
82+
else:
83+
print('None')
84+
""".format(sensitive_path=sensitive_path,
85+
insensitive_path=insensitive_path,
86+
name=self.name)
87+
88+
newenv = os.environ.copy()
89+
newenv["PYTHONCASEOK"] = "1"
90+
91+
def check_output(expected, extra_arg=None):
92+
args = [sys.executable]
93+
if extra_arg:
94+
args.append(extra_arg)
95+
args.extend(["-c", find_snippet])
96+
p = subprocess.Popen(args, stdout=subprocess.PIPE,
97+
env=newenv)
98+
actual = p.communicate()[0].decode().split()
99+
self.assertEqual(expected[0], actual[0])
100+
self.assertIn(expected[1], actual[1])
101+
self.assertEqual(expected[2], actual[2])
102+
self.assertIn(expected[3], actual[3])
103+
self.assertEqual(p.wait(), 0)
104+
105+
# Test with PYTHONCASEOK=1.
106+
check_output(["True", self.name, "True", self.name])
107+
108+
# Test with PYTHONCASEOK=1 ignored because of -E.
109+
check_output(["True", self.name, "False", "None"], "-E")
63110

64111

65112
def test_main():

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Core and Builtins
5454
Library
5555
-------
5656

57+
- Issue #16826: Don't check for PYTHONCASEOK if interpreter started with -E.
58+
5759
- Issue #18756: os.urandom() now uses a lazily-opened persistent file
5860
descriptor, so as to avoid using many file descriptors when run in
5961
parallel from multiple threads.

0 commit comments

Comments
 (0)