mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
This commit is contained in:
parent
83f1ef681f
commit
9039b83c53
2 changed files with 24 additions and 5 deletions
|
@ -43,7 +43,14 @@ class ReloadTests(unittest.TestCase):
|
||||||
reload()."""
|
reload()."""
|
||||||
|
|
||||||
def test_source(self):
|
def test_source(self):
|
||||||
with test_support.CleanImport('os'):
|
# XXX (ncoghlan): It would be nice to use test_support.CleanImport
|
||||||
|
# here, but that breaks because the os module registers some
|
||||||
|
# handlers in copy_reg on import. Since CleanImport doesn't
|
||||||
|
# revert that registration, the module is left in a broken
|
||||||
|
# state after reversion. Reinitialising the module contents
|
||||||
|
# and just reverting os.environ to its previous state is an OK
|
||||||
|
# workaround
|
||||||
|
with test_support.EnvironmentVarGuard():
|
||||||
import os
|
import os
|
||||||
imp.reload(os)
|
imp.reload(os)
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import py_compile
|
||||||
import warnings
|
import warnings
|
||||||
import marshal
|
import marshal
|
||||||
from test.test_support import (unlink, TESTFN, unload, run_unittest,
|
from test.test_support import (unlink, TESTFN, unload, run_unittest,
|
||||||
check_warnings, TestFailed, CleanImport)
|
check_warnings, TestFailed, EnvironmentVarGuard)
|
||||||
|
|
||||||
|
|
||||||
def remove_files(name):
|
def remove_files(name):
|
||||||
|
@ -121,10 +121,22 @@ class ImportTest(unittest.TestCase):
|
||||||
|
|
||||||
def testImpModule(self):
|
def testImpModule(self):
|
||||||
# Verify that the imp module can correctly load and find .py files
|
# Verify that the imp module can correctly load and find .py files
|
||||||
import imp
|
import imp, os
|
||||||
with CleanImport("os"):
|
# XXX (ncoghlan): It would be nice to use test_support.CleanImport
|
||||||
|
# here, but that breaks because the os module registers some
|
||||||
|
# handlers in copy_reg on import. Since CleanImport doesn't
|
||||||
|
# revert that registration, the module is left in a broken
|
||||||
|
# state after reversion. Reinitialising the module contents
|
||||||
|
# and just reverting os.environ to its previous state is an OK
|
||||||
|
# workaround
|
||||||
|
orig_path = os.path
|
||||||
|
orig_getenv = os.getenv
|
||||||
|
with EnvironmentVarGuard():
|
||||||
x = imp.find_module("os")
|
x = imp.find_module("os")
|
||||||
os = imp.load_module("os", *x)
|
new_os = imp.load_module("os", *x)
|
||||||
|
self.assertIs(os, new_os)
|
||||||
|
self.assertIs(orig_path, new_os.path)
|
||||||
|
self.assertIsNot(orig_getenv, new_os.getenv)
|
||||||
|
|
||||||
def test_module_with_large_stack(self, module='longlist'):
|
def test_module_with_large_stack(self, module='longlist'):
|
||||||
# create module w/list of 65000 elements to test bug #561858
|
# create module w/list of 65000 elements to test bug #561858
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue