Skip to content

Commit

Permalink
CleanImport sys is not enough
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Jan 22, 2024
1 parent 0e1e3d8 commit 2486d00
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
@@ -804,30 +804,6 @@ def test_cwd_script_shadowing_stdlib(self):
):
collections.does_not_exist

def test_shadowing_stdlib_edge_cases(self):
with CleanImport('collections'):
import collections
collections.__spec__ = types.SimpleNamespace()
collections.__spec__.origin = os.path.join(os.getcwd(), 'collections.py')

with CleanImport('sys'):
import sys
sys.stdlib_module_names = None
with self.assertRaisesRegex(
AttributeError,
r"module 'collections' has no attribute 'does_not_exist'"
):
collections.does_not_exist

del sys.stdlib_module_names
with self.assertRaisesRegex(
AttributeError,
r"module 'collections' has no attribute 'does_not_exist'"
):
collections.does_not_exist

with CleanImport('collections'):
import collections
del collections.__spec__.origin
with self.assertRaisesRegex(
AttributeError,
@@ -841,6 +817,33 @@ def test_shadowing_stdlib_edge_cases(self):
):
collections.does_not_exist

def test_shadowing_stdlib_sys_edge_cases(self):
program = textwrap.dedent('''
import collections
import os
import types
collections.__spec__ = types.SimpleNamespace()
collections.__spec__.origin = os.path.join(os.getcwd(), 'collections.py')
import sys
sys.stdlib_module_names = None
try:
collections.does_not_exist
except AttributeError as e:
print(str(e))
del sys.stdlib_module_names
try:
collections.does_not_exist
except AttributeError as e:
print(str(e))
''')
popen = script_helper.spawn_python('-c', program)
stdout, stderr = popen.communicate()
self.assertEqual(stdout.splitlines(), [
b"module 'collections' has no attribute 'does_not_exist'",
b"module 'collections' has no attribute 'does_not_exist'",
])


@skip_if_dont_write_bytecode
class FilePermissionTests(unittest.TestCase):

0 comments on commit 2486d00

Please sign in to comment.