2008-12-14 07:50:48 -04:00
|
|
|
# This test module covers support in various parts of the standard library
|
|
|
|
# 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 test.support
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
import textwrap
|
|
|
|
import zipfile
|
|
|
|
import zipimport
|
|
|
|
import doctest
|
|
|
|
import inspect
|
|
|
|
import linecache
|
2011-01-11 06:05:20 -04:00
|
|
|
import unittest
|
2020-06-30 10:46:06 -03:00
|
|
|
from test.support import os_helper
|
2015-05-06 00:33:17 -03:00
|
|
|
from test.support.script_helper import (spawn_python, kill_python, assert_python_ok,
|
|
|
|
make_script, make_zip_script)
|
2008-12-14 07:50:48 -04:00
|
|
|
|
|
|
|
verbose = test.support.verbose
|
|
|
|
|
|
|
|
# Library modules covered by this test set
|
|
|
|
# pdb (Issue 4201)
|
|
|
|
# inspect (Issue 4223)
|
|
|
|
# doctest (Issue 4197)
|
|
|
|
|
|
|
|
# Other test modules with zipimport related tests
|
|
|
|
# test_zipimport (of course!)
|
|
|
|
# test_cmd_line_script (covers the zipimport support in runpy)
|
|
|
|
|
|
|
|
# Retrieve some helpers from other test cases
|
2024-01-18 11:58:11 -04:00
|
|
|
from test.test_doctest import (test_doctest,
|
|
|
|
sample_doctest, sample_doctest_no_doctests,
|
2024-03-28 16:59:12 -03:00
|
|
|
sample_doctest_no_docstrings, sample_doctest_skip)
|
2008-12-14 07:50:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _run_object_doctest(obj, module):
|
2011-06-29 13:11:36 -03:00
|
|
|
finder = doctest.DocTestFinder(verbose=verbose, recurse=False)
|
|
|
|
runner = doctest.DocTestRunner(verbose=verbose)
|
|
|
|
# Use the object's fully qualified name if it has one
|
|
|
|
# Otherwise, use the module's name
|
2008-12-14 07:50:48 -04:00
|
|
|
try:
|
2014-07-22 09:00:37 -03:00
|
|
|
name = "%s.%s" % (obj.__module__, obj.__qualname__)
|
2011-06-29 13:11:36 -03:00
|
|
|
except AttributeError:
|
|
|
|
name = module.__name__
|
|
|
|
for example in finder.find(obj, name, module):
|
|
|
|
runner.run(example)
|
|
|
|
f, t = runner.failures, runner.tries
|
|
|
|
if f:
|
|
|
|
raise test.support.TestFailed("%d of %d doctests failed" % (f, t))
|
2008-12-14 07:50:48 -04:00
|
|
|
if verbose:
|
|
|
|
print ('doctest (%s) ... %d tests with zero failures' % (module.__name__, t))
|
|
|
|
return f, t
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-11 06:05:20 -04:00
|
|
|
class ZipSupportTests(unittest.TestCase):
|
|
|
|
# This used to use the ImportHooksBaseTestCase to restore
|
2008-12-14 07:50:48 -04:00
|
|
|
# the state of the import related information
|
2011-01-11 06:05:20 -04:00
|
|
|
# in the sys module after each test. However, that restores
|
2014-12-01 12:28:43 -04:00
|
|
|
# *too much* information and breaks for the invocation
|
2011-01-11 06:05:20 -04:00
|
|
|
# of test_doctest. So we do our own thing and leave
|
|
|
|
# sys.modules alone.
|
2008-12-14 07:50:48 -04:00
|
|
|
# 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()
|
2011-01-11 06:05:20 -04:00
|
|
|
self.path = sys.path[:]
|
|
|
|
self.meta_path = sys.meta_path[:]
|
|
|
|
self.path_hooks = sys.path_hooks[:]
|
|
|
|
sys.path_importer_cache.clear()
|
2008-12-14 07:50:48 -04:00
|
|
|
|
2011-01-11 06:05:20 -04:00
|
|
|
def tearDown(self):
|
|
|
|
sys.path[:] = self.path
|
|
|
|
sys.meta_path[:] = self.meta_path
|
|
|
|
sys.path_hooks[:] = self.path_hooks
|
|
|
|
sys.path_importer_cache.clear()
|
2008-12-14 07:50:48 -04:00
|
|
|
|
|
|
|
def test_inspect_getsource_issue4223(self):
|
|
|
|
test_src = "def foo(): pass\n"
|
2020-06-30 10:46:06 -03:00
|
|
|
with os_helper.temp_dir() as d:
|
2009-11-16 02:49:25 -04:00
|
|
|
init_name = make_script(d, '__init__', test_src)
|
2008-12-14 07:50:48 -04:00
|
|
|
name_in_zip = os.path.join('zip_pkg',
|
|
|
|
os.path.basename(init_name))
|
2009-11-16 02:49:25 -04:00
|
|
|
zip_name, run_name = make_zip_script(d, 'test_zip',
|
2008-12-14 07:50:48 -04:00
|
|
|
init_name, name_in_zip)
|
|
|
|
os.remove(init_name)
|
|
|
|
sys.path.insert(0, zip_name)
|
|
|
|
import zip_pkg
|
2011-02-07 09:43:07 -04:00
|
|
|
try:
|
|
|
|
self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
|
|
|
|
finally:
|
|
|
|
del sys.modules["zip_pkg"]
|
2008-12-14 07:50:48 -04:00
|
|
|
|
|
|
|
def test_doctest_issue4197(self):
|
|
|
|
# To avoid having to keep two copies of the doctest module's
|
|
|
|
# unit tests in sync, this test works by taking the source of
|
|
|
|
# test_doctest itself, rewriting it a bit to cope with a new
|
|
|
|
# location, and then throwing it in a zip file to make sure
|
|
|
|
# everything still works correctly
|
|
|
|
test_src = inspect.getsource(test_doctest)
|
|
|
|
test_src = test_src.replace(
|
2024-01-18 11:58:11 -04:00
|
|
|
"from test.test_doctest import test_doctest",
|
2008-12-14 07:50:48 -04:00
|
|
|
"import test_zipped_doctest as test_doctest")
|
2024-01-18 11:58:11 -04:00
|
|
|
test_src = test_src.replace("test.test_doctest.test_doctest",
|
2008-12-14 07:50:48 -04:00
|
|
|
"test_zipped_doctest")
|
2024-01-18 11:58:11 -04:00
|
|
|
test_src = test_src.replace("test.test_doctest.sample_doctest",
|
2008-12-14 07:50:48 -04:00
|
|
|
"sample_zipped_doctest")
|
2012-09-10 11:15:58 -03:00
|
|
|
# The sample doctest files rewritten to include in the zipped version.
|
|
|
|
sample_sources = {}
|
|
|
|
for mod in [sample_doctest, sample_doctest_no_doctests,
|
2024-03-28 16:59:12 -03:00
|
|
|
sample_doctest_no_docstrings, sample_doctest_skip]:
|
2012-09-10 11:15:58 -03:00
|
|
|
src = inspect.getsource(mod)
|
2024-01-18 11:58:11 -04:00
|
|
|
src = src.replace("test.test_doctest.test_doctest", "test_zipped_doctest")
|
2012-09-10 11:15:58 -03:00
|
|
|
# Rewrite the module name so that, for example,
|
|
|
|
# "test.sample_doctest" becomes "sample_zipped_doctest".
|
|
|
|
mod_name = mod.__name__.split(".")[-1]
|
|
|
|
mod_name = mod_name.replace("sample_", "sample_zipped_")
|
|
|
|
sample_sources[mod_name] = src
|
|
|
|
|
2020-06-30 10:46:06 -03:00
|
|
|
with os_helper.temp_dir() as d:
|
2009-11-16 02:49:25 -04:00
|
|
|
script_name = make_script(d, 'test_zipped_doctest',
|
2008-12-14 07:50:48 -04:00
|
|
|
test_src)
|
2009-11-16 02:49:25 -04:00
|
|
|
zip_name, run_name = make_zip_script(d, 'test_zip',
|
2008-12-14 07:50:48 -04:00
|
|
|
script_name)
|
2019-03-05 04:06:26 -04:00
|
|
|
with zipfile.ZipFile(zip_name, 'a') as z:
|
|
|
|
for mod_name, src in sample_sources.items():
|
|
|
|
z.writestr(mod_name + ".py", src)
|
2008-12-14 07:50:48 -04:00
|
|
|
if verbose:
|
2019-03-05 04:06:26 -04:00
|
|
|
with zipfile.ZipFile(zip_name, 'r') as zip_file:
|
|
|
|
print ('Contents of %r:' % zip_name)
|
|
|
|
zip_file.printdir()
|
2008-12-14 07:50:48 -04:00
|
|
|
os.remove(script_name)
|
|
|
|
sys.path.insert(0, zip_name)
|
|
|
|
import test_zipped_doctest
|
2011-02-07 09:43:07 -04:00
|
|
|
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_testsource,
|
|
|
|
test_zipped_doctest.test_trailing_space_in_test,
|
|
|
|
test_zipped_doctest.test_DocTestSuite,
|
|
|
|
test_zipped_doctest.test_DocTestFinder,
|
|
|
|
]
|
2011-02-21 15:29:56 -04:00
|
|
|
# These tests are the ones which need access
|
2011-02-07 09:43:07 -04:00
|
|
|
# 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,
|
|
|
|
]
|
2011-02-21 15:29:56 -04:00
|
|
|
|
2011-02-07 09:43:07 -04:00
|
|
|
for obj in known_good_tests:
|
|
|
|
_run_object_doctest(obj, test_zipped_doctest)
|
|
|
|
finally:
|
|
|
|
del sys.modules["test_zipped_doctest"]
|
2008-12-14 07:50:48 -04:00
|
|
|
|
2008-12-15 08:01:34 -04:00
|
|
|
def test_doctest_main_issue4197(self):
|
|
|
|
test_src = textwrap.dedent("""\
|
|
|
|
class Test:
|
|
|
|
">>> 'line 2'"
|
|
|
|
pass
|
|
|
|
|
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
|
|
""")
|
|
|
|
pattern = 'File "%s", line 2, in %s'
|
2020-06-30 10:46:06 -03:00
|
|
|
with os_helper.temp_dir() as d:
|
2009-11-16 02:49:25 -04:00
|
|
|
script_name = make_script(d, 'script', test_src)
|
2010-10-08 15:05:42 -03:00
|
|
|
rc, out, err = assert_python_ok(script_name)
|
2008-12-15 08:01:34 -04:00
|
|
|
expected = pattern % (script_name, "__main__.Test")
|
|
|
|
if verbose:
|
|
|
|
print ("Expected line", expected)
|
|
|
|
print ("Got stdout:")
|
2010-10-20 18:48:35 -03:00
|
|
|
print (ascii(out))
|
2010-10-08 15:05:42 -03:00
|
|
|
self.assertIn(expected.encode('utf-8'), out)
|
2009-11-16 02:49:25 -04:00
|
|
|
zip_name, run_name = make_zip_script(d, "test_zip",
|
2008-12-15 08:01:34 -04:00
|
|
|
script_name, '__main__.py')
|
2010-10-08 15:05:42 -03:00
|
|
|
rc, out, err = assert_python_ok(zip_name)
|
2008-12-15 08:01:34 -04:00
|
|
|
expected = pattern % (run_name, "__main__.Test")
|
|
|
|
if verbose:
|
|
|
|
print ("Expected line", expected)
|
|
|
|
print ("Got stdout:")
|
2010-10-20 18:48:35 -03:00
|
|
|
print (ascii(out))
|
2010-10-08 15:05:42 -03:00
|
|
|
self.assertIn(expected.encode('utf-8'), out)
|
2008-12-15 08:01:34 -04:00
|
|
|
|
2008-12-14 07:50:48 -04:00
|
|
|
def test_pdb_issue4201(self):
|
|
|
|
test_src = textwrap.dedent("""\
|
|
|
|
def f():
|
|
|
|
pass
|
|
|
|
|
|
|
|
import pdb
|
2010-12-04 13:11:36 -04:00
|
|
|
pdb.Pdb(nosigint=True).runcall(f)
|
2008-12-14 07:50:48 -04:00
|
|
|
""")
|
2020-06-30 10:46:06 -03:00
|
|
|
with os_helper.temp_dir() as d:
|
2009-11-16 02:49:25 -04:00
|
|
|
script_name = make_script(d, 'script', test_src)
|
|
|
|
p = spawn_python(script_name)
|
2008-12-14 07:50:48 -04:00
|
|
|
p.stdin.write(b'l\n')
|
2009-11-16 02:49:25 -04:00
|
|
|
data = kill_python(p)
|
2013-10-25 14:38:16 -03:00
|
|
|
# bdb/pdb applies normcase to its filename before displaying
|
|
|
|
self.assertIn(os.path.normcase(script_name.encode('utf-8')), data)
|
2009-11-16 02:49:25 -04:00
|
|
|
zip_name, run_name = make_zip_script(d, "test_zip",
|
2008-12-14 07:50:48 -04:00
|
|
|
script_name, '__main__.py')
|
2009-11-16 02:49:25 -04:00
|
|
|
p = spawn_python(zip_name)
|
2008-12-14 07:50:48 -04:00
|
|
|
p.stdin.write(b'l\n')
|
2009-11-16 02:49:25 -04:00
|
|
|
data = kill_python(p)
|
2013-10-25 14:38:16 -03:00
|
|
|
# bdb/pdb applies normcase to its filename before displaying
|
|
|
|
self.assertIn(os.path.normcase(run_name.encode('utf-8')), data)
|
2008-12-14 07:50:48 -04:00
|
|
|
|
|
|
|
|
2015-04-13 17:00:43 -03:00
|
|
|
def tearDownModule():
|
2008-12-14 07:50:48 -04:00
|
|
|
test.support.reap_children()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-04-13 17:00:43 -03:00
|
|
|
unittest.main()
|