Commit 90be5fb8 authored by Nick Coghlan's avatar Nick Coghlan

Issue 10556: test_zipimport_support implicitly imports too many modules...

Issue 10556: test_zipimport_support implicitly imports too many modules (including _ssl) to safely clobber sys.modules after each test
parent 6655d113
...@@ -1199,6 +1199,12 @@ def modules_cleanup(oldmodules): ...@@ -1199,6 +1199,12 @@ def modules_cleanup(oldmodules):
if k.startswith('encodings.')] if k.startswith('encodings.')]
sys.modules.clear() sys.modules.clear()
sys.modules.update(encodings) sys.modules.update(encodings)
# XXX: This kind of problem can affect more than just encodings. In particular
# extension modules (such as _ssl) don't cope with releoding properly.
# Really, test modules should be cleaning out the test specific modules they
# know they added (ala test_runpy) rather than relying on this function (as
# test_importhooks and test_pkg do currently).
# Implicitly imported *real* modules should be left alone (see issue 10556).
sys.modules.update(oldmodules) sys.modules.update(oldmodules)
#======================================================================= #=======================================================================
......
...@@ -13,6 +13,7 @@ import doctest ...@@ -13,6 +13,7 @@ import doctest
import inspect import inspect
import linecache import linecache
import pdb import pdb
import unittest
from test.script_helper import (spawn_python, kill_python, assert_python_ok, from test.script_helper import (spawn_python, kill_python, assert_python_ok,
temp_dir, make_script, make_zip_script) temp_dir, make_script, make_zip_script)
...@@ -29,7 +30,6 @@ verbose = test.support.verbose ...@@ -29,7 +30,6 @@ verbose = test.support.verbose
# Retrieve some helpers from other test cases # Retrieve some helpers from other test cases
from test import test_doctest, sample_doctest from test import test_doctest, sample_doctest
from test.test_importhooks import ImportHooksBaseTestCase
def _run_object_doctest(obj, module): def _run_object_doctest(obj, module):
...@@ -59,17 +59,28 @@ def _run_object_doctest(obj, module): ...@@ -59,17 +59,28 @@ def _run_object_doctest(obj, module):
class ZipSupportTests(ImportHooksBaseTestCase): class ZipSupportTests(unittest.TestCase):
# We use the ImportHooksBaseTestCase to restore # This used to use the ImportHooksBaseTestCase to restore
# the state of the import related information # the state of the import related information
# in the sys module after each test # in the sys module after each test. However, that restores
# *too much* information and breaks for the invocation of
# of test_doctest. So we do our own thing and leave
# sys.modules alone.
# We also clear the linecache and zipimport cache # We also clear the linecache and zipimport cache
# just to avoid any bogus errors due to name reuse in the tests # just to avoid any bogus errors due to name reuse in the tests
def setUp(self): def setUp(self):
linecache.clearcache() linecache.clearcache()
zipimport._zip_directory_cache.clear() zipimport._zip_directory_cache.clear()
ImportHooksBaseTestCase.setUp(self) self.path = sys.path[:]
self.meta_path = sys.meta_path[:]
self.path_hooks = sys.path_hooks[:]
sys.path_importer_cache.clear()
def tearDown(self):
sys.path[:] = self.path
sys.meta_path[:] = self.meta_path
sys.path_hooks[:] = self.path_hooks
sys.path_importer_cache.clear()
def test_inspect_getsource_issue4223(self): def test_inspect_getsource_issue4223(self):
test_src = "def foo(): pass\n" test_src = "def foo(): pass\n"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment