Skip to content

Commit d151da9

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.
1 parent 9edb168 commit d151da9

5 files changed

Lines changed: 4298 additions & 4220 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ def _make_relax_case():
3333
if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
3434
def _relax_case():
3535
"""True if filenames must be checked case-insensitively."""
36-
return b'PYTHONCASEOK' in _os.environ
36+
if sys.flags.ignore_environment:
37+
return False
38+
else:
39+
return b'PYTHONCASEOK' in _os.environ
3740
else:
3841
def _relax_case():
3942
"""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
@@ -5,7 +5,8 @@
55
from importlib import _bootstrap
66
from .. import util
77
from . import util as ext_util
8-
8+
import os
9+
import subprocess
910

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

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

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

4162

4263

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
@@ -66,6 +66,8 @@ Core and Builtins
6666
Library
6767
-------
6868

69+
- Issue #16826: Don't check for PYTHONCASEOK if interpreter started with -E.
70+
6971
- Issue #18418: After fork(), reinit all threads states, not only active ones.
7072
Patch by A. Jesse Jiryu Davis.
7173

0 commit comments

Comments
 (0)