Skip to content

Commit 1781480

Browse files
miss-islingtonMariatta
authored andcommitted
bpo-31676: Fix test_imp.test_load_source() side effect (GH-3871) (GH-3988)
test_load_source() now replaces the current __name__ module with a temporary module to prevent side effects. (cherry picked from commit a505ecd)
1 parent 98e0f26 commit 1781480

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Lib/test/test_imp.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,13 @@ def test_multiple_calls_to_get_data(self):
315315
loader.get_data(imp.__file__) # Will need to create a newly opened file
316316

317317
def test_load_source(self):
318-
with self.assertRaisesRegex(ValueError, 'embedded null'):
319-
imp.load_source(__name__, __file__ + "\0")
318+
# Create a temporary module since load_source(name) modifies
319+
# sys.modules[name] attributes like __loader___
320+
modname = f"tmp{__name__}"
321+
mod = type(sys.modules[__name__])(modname)
322+
with support.swap_item(sys.modules, modname, mod):
323+
with self.assertRaisesRegex(ValueError, 'embedded null'):
324+
imp.load_source(modname, __file__ + "\0")
320325

321326
@support.cpython_only
322327
def test_issue31315(self):

0 commit comments

Comments
 (0)