summaryrefslogtreecommitdiff
path: root/Lib/test/test_zipimport_support.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_zipimport_support.py')
-rw-r--r--Lib/test/test_zipimport_support.py173
1 files changed, 93 insertions, 80 deletions
diff --git a/Lib/test/test_zipimport_support.py b/Lib/test/test_zipimport_support.py
index 94a65fc7b2..610dfa0d0a 100644
--- a/Lib/test/test_zipimport_support.py
+++ b/Lib/test/test_zipimport_support.py
@@ -2,7 +2,6 @@
# for working with modules located inside zipfiles
# The tests are centralised in this fashion to make it easy to drop them
# if a platform doesn't support zipimport
-import unittest
import test.support
import os
import os.path
@@ -14,6 +13,9 @@ import doctest
import inspect
import linecache
import pdb
+import unittest
+from test.script_helper import (spawn_python, kill_python, assert_python_ok,
+ temp_dir, make_script, make_zip_script)
verbose = test.support.verbose
@@ -28,12 +30,6 @@ verbose = test.support.verbose
# Retrieve some helpers from other test cases
from test import test_doctest, sample_doctest
-from test.test_importhooks import ImportHooksBaseTestCase
-from test.test_cmd_line_script import temp_dir, _run_python, \
- _spawn_python, _kill_python, \
- _make_test_script, \
- _compile_test_script, \
- _make_test_zip, _make_test_pkg
def _run_object_doctest(obj, module):
@@ -63,30 +59,44 @@ def _run_object_doctest(obj, module):
-class ZipSupportTests(ImportHooksBaseTestCase):
- # We use the ImportHooksBaseTestCase to restore
+class ZipSupportTests(unittest.TestCase):
+ # This used to use the ImportHooksBaseTestCase to restore
# 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
# just to avoid any bogus errors due to name reuse in the tests
def setUp(self):
linecache.clearcache()
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):
test_src = "def foo(): pass\n"
with temp_dir() as d:
- init_name = _make_test_script(d, '__init__', test_src)
+ init_name = make_script(d, '__init__', test_src)
name_in_zip = os.path.join('zip_pkg',
os.path.basename(init_name))
- zip_name, run_name = _make_test_zip(d, 'test_zip',
+ zip_name, run_name = make_zip_script(d, 'test_zip',
init_name, name_in_zip)
os.remove(init_name)
sys.path.insert(0, zip_name)
import zip_pkg
- self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
+ try:
+ self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
+ finally:
+ del sys.modules["zip_pkg"]
def test_doctest_issue4197(self):
# To avoid having to keep two copies of the doctest module's
@@ -106,9 +116,9 @@ class ZipSupportTests(ImportHooksBaseTestCase):
sample_src = sample_src.replace("test.test_doctest",
"test_zipped_doctest")
with temp_dir() as d:
- script_name = _make_test_script(d, 'test_zipped_doctest',
+ script_name = make_script(d, 'test_zipped_doctest',
test_src)
- zip_name, run_name = _make_test_zip(d, 'test_zip',
+ zip_name, run_name = make_zip_script(d, 'test_zip',
script_name)
z = zipfile.ZipFile(zip_name, 'a')
z.writestr("sample_zipped_doctest.py", sample_src)
@@ -121,53 +131,56 @@ class ZipSupportTests(ImportHooksBaseTestCase):
os.remove(script_name)
sys.path.insert(0, zip_name)
import test_zipped_doctest
- # Some of the doc tests depend on the colocated text files
- # which aren't available to the zipped version (the doctest
- # module currently requires real filenames for non-embedded
- # tests). So we're forced to be selective about which tests
- # to run.
- # doctest could really use some APIs which take a text
- # string or a file object instead of a filename...
- known_good_tests = [
- test_zipped_doctest.SampleClass,
- test_zipped_doctest.SampleClass.NestedClass,
- test_zipped_doctest.SampleClass.NestedClass.__init__,
- test_zipped_doctest.SampleClass.__init__,
- test_zipped_doctest.SampleClass.a_classmethod,
- test_zipped_doctest.SampleClass.a_property,
- test_zipped_doctest.SampleClass.a_staticmethod,
- test_zipped_doctest.SampleClass.double,
- test_zipped_doctest.SampleClass.get,
- test_zipped_doctest.SampleNewStyleClass,
- test_zipped_doctest.SampleNewStyleClass.__init__,
- test_zipped_doctest.SampleNewStyleClass.double,
- test_zipped_doctest.SampleNewStyleClass.get,
- test_zipped_doctest.sample_func,
- test_zipped_doctest.test_DocTest,
- test_zipped_doctest.test_DocTestParser,
- test_zipped_doctest.test_DocTestRunner.basics,
- test_zipped_doctest.test_DocTestRunner.exceptions,
- test_zipped_doctest.test_DocTestRunner.option_directives,
- test_zipped_doctest.test_DocTestRunner.optionflags,
- test_zipped_doctest.test_DocTestRunner.verbose_flag,
- test_zipped_doctest.test_Example,
- test_zipped_doctest.test_debug,
- test_zipped_doctest.test_pdb_set_trace,
- test_zipped_doctest.test_pdb_set_trace_nested,
- test_zipped_doctest.test_testsource,
- test_zipped_doctest.test_trailing_space_in_test,
- test_zipped_doctest.test_DocTestSuite,
- test_zipped_doctest.test_DocTestFinder,
- ]
- # These remaining tests are the ones which need access
- # to the data files, so we don't run them
- fail_due_to_missing_data_files = [
- test_zipped_doctest.test_DocFileSuite,
- test_zipped_doctest.test_testfile,
- test_zipped_doctest.test_unittest_reportflags,
- ]
- for obj in known_good_tests:
- _run_object_doctest(obj, test_zipped_doctest)
+ try:
+ # Some of the doc tests depend on the colocated text files
+ # which aren't available to the zipped version (the doctest
+ # module currently requires real filenames for non-embedded
+ # tests). So we're forced to be selective about which tests
+ # to run.
+ # doctest could really use some APIs which take a text
+ # string or a file object instead of a filename...
+ known_good_tests = [
+ test_zipped_doctest.SampleClass,
+ test_zipped_doctest.SampleClass.NestedClass,
+ test_zipped_doctest.SampleClass.NestedClass.__init__,
+ test_zipped_doctest.SampleClass.__init__,
+ test_zipped_doctest.SampleClass.a_classmethod,
+ test_zipped_doctest.SampleClass.a_property,
+ test_zipped_doctest.SampleClass.a_staticmethod,
+ test_zipped_doctest.SampleClass.double,
+ test_zipped_doctest.SampleClass.get,
+ test_zipped_doctest.SampleNewStyleClass,
+ test_zipped_doctest.SampleNewStyleClass.__init__,
+ test_zipped_doctest.SampleNewStyleClass.double,
+ test_zipped_doctest.SampleNewStyleClass.get,
+ test_zipped_doctest.sample_func,
+ test_zipped_doctest.test_DocTest,
+ test_zipped_doctest.test_DocTestParser,
+ test_zipped_doctest.test_DocTestRunner.basics,
+ test_zipped_doctest.test_DocTestRunner.exceptions,
+ test_zipped_doctest.test_DocTestRunner.option_directives,
+ test_zipped_doctest.test_DocTestRunner.optionflags,
+ test_zipped_doctest.test_DocTestRunner.verbose_flag,
+ test_zipped_doctest.test_Example,
+ test_zipped_doctest.test_debug,
+ test_zipped_doctest.test_pdb_set_trace,
+ test_zipped_doctest.test_pdb_set_trace_nested,
+ test_zipped_doctest.test_testsource,
+ test_zipped_doctest.test_trailing_space_in_test,
+ test_zipped_doctest.test_DocTestSuite,
+ test_zipped_doctest.test_DocTestFinder,
+ ]
+ # These remaining tests are the ones which need access
+ # to the data files, so we don't run them
+ fail_due_to_missing_data_files = [
+ test_zipped_doctest.test_DocFileSuite,
+ test_zipped_doctest.test_testfile,
+ test_zipped_doctest.test_unittest_reportflags,
+ ]
+ for obj in known_good_tests:
+ _run_object_doctest(obj, test_zipped_doctest)
+ finally:
+ del sys.modules["test_zipped_doctest"]
def test_doctest_main_issue4197(self):
test_src = textwrap.dedent("""\
@@ -180,23 +193,23 @@ class ZipSupportTests(ImportHooksBaseTestCase):
""")
pattern = 'File "%s", line 2, in %s'
with temp_dir() as d:
- script_name = _make_test_script(d, 'script', test_src)
- exit_code, data = _run_python(script_name)
+ script_name = make_script(d, 'script', test_src)
+ rc, out, err = assert_python_ok(script_name)
expected = pattern % (script_name, "__main__.Test")
if verbose:
print ("Expected line", expected)
print ("Got stdout:")
- print (data)
- self.assertTrue(expected in data)
- zip_name, run_name = _make_test_zip(d, "test_zip",
+ print (ascii(out))
+ self.assertIn(expected.encode('utf-8'), out)
+ zip_name, run_name = make_zip_script(d, "test_zip",
script_name, '__main__.py')
- exit_code, data = _run_python(zip_name)
+ rc, out, err = assert_python_ok(zip_name)
expected = pattern % (run_name, "__main__.Test")
if verbose:
print ("Expected line", expected)
print ("Got stdout:")
- print (data)
- self.assertTrue(expected in data)
+ print (ascii(out))
+ self.assertIn(expected.encode('utf-8'), out)
def test_pdb_issue4201(self):
test_src = textwrap.dedent("""\
@@ -204,20 +217,20 @@ class ZipSupportTests(ImportHooksBaseTestCase):
pass
import pdb
- pdb.runcall(f)
+ pdb.Pdb(nosigint=True).runcall(f)
""")
with temp_dir() as d:
- script_name = _make_test_script(d, 'script', test_src)
- p = _spawn_python(script_name)
+ script_name = make_script(d, 'script', test_src)
+ p = spawn_python(script_name)
p.stdin.write(b'l\n')
- data = _kill_python(p).decode()
- self.assertTrue(script_name in data)
- zip_name, run_name = _make_test_zip(d, "test_zip",
+ data = kill_python(p)
+ self.assertIn(script_name.encode('utf-8'), data)
+ zip_name, run_name = make_zip_script(d, "test_zip",
script_name, '__main__.py')
- p = _spawn_python(zip_name)
+ p = spawn_python(zip_name)
p.stdin.write(b'l\n')
- data = _kill_python(p).decode()
- self.assertTrue(run_name in data)
+ data = kill_python(p)
+ self.assertIn(run_name.encode('utf-8'), data)
def test_main():