def test_main_recursion_error(self): with temp_dir() as script_dir, temp_dir() as dummy_dir: mod_name = "__main__" source = ("import runpy\n" "runpy.run_path(%r)\n") % dummy_dir script_name = self._make_test_script(script_dir, mod_name, source) zip_name, fname = make_zip_script(script_dir, "test_zip", script_name) msg = "recursion depth exceeded" self.assertRaisesRegexp(RuntimeError, msg, run_path, zip_name)
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' with temp_dir() as d: 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 (ascii(out)) self.assertIn(expected.encode('utf-8'), out) zip_name, run_name = make_zip_script(d, "test_zip", script_name, '__main__.py') rc, out, err = assert_python_ok(zip_name) expected = pattern % (run_name, "__main__.Test") if verbose: print ("Expected line", expected) print ("Got stdout:") print (ascii(out)) self.assertIn(expected.encode('utf-8'), out)
def test_script_compiled(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'script') py_compile.compile(script_name, doraise=True) os.remove(script_name) pyc_file = support.make_legacy_pyc(script_name) self._check_script(pyc_file)
def test_directory_compiled(self): with temp_dir() as script_dir: mod_name = "__main__" script_name = self._make_test_script(script_dir, mod_name) compiled_name = compile_script(script_name) os.remove(script_name) self._check_script(script_dir, "<run_path>", compiled_name, script_dir, "")
def test_script_compiled(self): with temp_dir() as script_dir: mod_name = "script" script_name = self._make_test_script(script_dir, mod_name) compiled_name = compile_script(script_name) os.remove(script_name) self._check_script(compiled_name, "<run_path>", compiled_name, compiled_name, None)
def test_zipfile_compiled(self): with temp_dir() as script_dir: mod_name = "__main__" script_name = self._make_test_script(script_dir, mod_name) compiled_name = py_compile.compile(script_name, doraise=True) zip_name, fname = make_zip_script(script_dir, "test_zip", compiled_name) self._check_script(zip_name, "<run_path>", fname, zip_name, "")
def test_zipfile_error(self): with temp_dir() as script_dir: mod_name = 'not_main' script_name = self._make_test_script(script_dir, mod_name) zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) msg = "can't find '__main__' module in %r" % zip_name self._check_import_error(zip_name, msg)
def test_basic_script(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'script') package = '' if support.check_impl_detail(pypy=True) else None self._check_script(script_name, script_name, script_name, script_dir, package, importlib.machinery.SourceFileLoader)
def test_zipfile(self): source = self.main_in_children_source with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__', source=source) zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) self._check_script(zip_name)
def test_script_compiled(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, "script") py_compile.compile(script_name, doraise=True) os.remove(script_name) pyc_file = support.make_legacy_pyc(script_name) self._check_script(pyc_file, pyc_file, pyc_file, script_dir, None, importlib.machinery.SourcelessFileLoader)
def test_basic_script_no_suffix(self): with temp_dir() as script_dir: mod_name = 'script' script_name = self._make_test_script(script_dir, mod_name, omit_suffix=True) self._check_script(script_name, "<run_path>", script_name, script_name, expect_spec=False)
def test_zipfile(self): with temp_dir() as script_dir: mod_name = '__main__' script_name = self._make_test_script(script_dir, mod_name) zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) self._check_script(zip_name, "<run_path>", fname, zip_name, mod_name=mod_name, check_loader=False)
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' with temp_dir() as d: script_name = make_script(d, 'script', test_src) exit_code, data = run_python(script_name) expected = pattern % (script_name, "__main__.Test") if verbose: print "Expected line", expected print "Got stdout:" print data self.assertIn(expected, data) zip_name, run_name = make_zip_script(d, "test_zip", script_name, '__main__.py') exit_code, data = run_python(zip_name) expected = pattern % (run_name, "__main__.Test") if verbose: print "Expected line", expected print "Got stdout:" print data self.assertIn(expected, data)
def test_zipfile_compiled(self): with temp_dir() as script_dir: mod_name = '__main__' script_name = self._make_test_script(script_dir, mod_name) compiled_name = compile_script(script_name) zip_name, fname = make_zip_script(script_dir, 'test_zip', compiled_name) self._check_script(zip_name, "<run_path>", fname, zip_name, '')
def test_package_error(self): with temp_dir() as script_dir: pkg_dir = os.path.join(script_dir, "test_pkg") make_pkg(pkg_dir) msg = "'test_pkg' is a package and cannot " "be directly executed" launch_name = _make_launch_script(script_dir, "launch", "test_pkg") self._check_import_error(launch_name, msg)
def test_package(self): with temp_dir() as script_dir: pkg_dir = os.path.join(script_dir, "test_pkg") make_pkg(pkg_dir) script_name = _make_test_script(pkg_dir, "__main__") launch_name = _make_launch_script(script_dir, "launch", "test_pkg") self._check_script(launch_name, script_name, script_name, "test_pkg")
def test_module_in_package(self): with temp_dir() as script_dir: pkg_dir = os.path.join(script_dir, 'test_pkg') make_pkg(pkg_dir) script_name = _make_test_script(pkg_dir, 'script') launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script') self._check_script(launch_name, script_name, script_name, 'test_pkg')
def test_zipfile_compiled(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__') compiled_name = py_compile.compile(script_name, doraise=True) zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name) self._check_script(zip_name, run_name, zip_name, zip_name, '', zipimport.zipimporter)
def test_module_in_subpackage_in_zipfile(self): with temp_dir() as script_dir: zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2) launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name) self._check_script(launch_name, run_name, run_name, zip_name, 'test_pkg.test_pkg', zipimport.zipimporter)
def test_directory_compiled(self): with temp_dir() as script_dir: mod_name = "__main__" script_name = self._make_test_script(script_dir, mod_name) compiled_name = py_compile.compile(script_name, doraise=True) os.remove(script_name) legacy_pyc = make_legacy_pyc(script_name) self._check_script(script_dir, "<run_path>", legacy_pyc, script_dir, "")
def test_directory_compiled(self): if test.test_support.check_impl_detail(pypy=True): raise unittest.SkipTest("pypy won't load lone .pyc files") with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__') compiled_name = compile_script(script_name) os.remove(script_name) self._check_script(script_dir, compiled_name, script_dir, '')
def test_zipfile_compiled(self): source = self.main_in_children_source with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__', source=source) compiled_name = py_compile.compile(script_name, doraise=True) zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name) self._check_script(zip_name)
def test_script_compiled(self): with temp_dir() as script_dir: mod_name = 'script' script_name = self._make_test_script(script_dir, mod_name) compiled_name = py_compile.compile(script_name, doraise=True) os.remove(script_name) self._check_script(compiled_name, "<run_path>", compiled_name, compiled_name, None)
def test_package(self): source = self.main_in_children_source with temp_dir() as script_dir: pkg_dir = os.path.join(script_dir, 'test_pkg') make_pkg(pkg_dir) script_name = _make_test_script(pkg_dir, '__main__', source=source) launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') self._check_script(launch_name)
def test_package(self): with temp_dir() as script_dir: pkg_dir = os.path.join(script_dir, 'test_pkg') make_pkg(pkg_dir) script_name = _make_test_script(pkg_dir, '__main__') launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') self._check_script(launch_name, script_name, script_name, script_dir, 'test_pkg', importlib.machinery.SourceFileLoader)
def test_package_compiled(self): with temp_dir() as script_dir: pkg_dir = os.path.join(script_dir, "test_pkg") make_pkg(pkg_dir) script_name = _make_test_script(pkg_dir, "__main__") compiled_name = compile_script(script_name) os.remove(script_name) launch_name = _make_launch_script(script_dir, "launch", "test_pkg") self._check_script(launch_name, compiled_name, compiled_name, "test_pkg")
def test_module_in_package(self): with temp_dir() as script_dir: pkg_dir = os.path.join(script_dir, "test_pkg") make_pkg(pkg_dir) script_name = _make_test_script(pkg_dir, "script") launch_name = _make_launch_script(script_dir, "launch", "test_pkg.script") self._check_script( launch_name, script_name, script_name, script_dir, "test_pkg", importlib.machinery.SourceFileLoader )
def test_directory_compiled(self): source = self.main_in_children_source with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__', source=source) py_compile.compile(script_name, doraise=True) os.remove(script_name) pyc_file = support.make_legacy_pyc(script_name) self._check_script(script_dir)
def test_zipfile_compiled(self): with temp_dir() as script_dir: mod_name = '__main__' script_name = self._make_test_script(script_dir, mod_name) compiled_name = py_compile.compile(script_name, doraise=True) zip_name, fname = make_zip_script(script_dir, 'test_zip', compiled_name) self._check_script(zip_name, "<run_path>", fname, zip_name, mod_name=mod_name, check_loader=False)
def test_encoding(self): with temp_dir() as script_dir: filename = os.path.join(script_dir, 'script.py') with open(filename, 'w', encoding='latin1') as f: f.write(""" #coding:latin1 "non-ASCII: h\xe9" """) result = run_path(filename) self.assertEqual(result['__doc__'], "non-ASCII: h\xe9")
def test_directory_error(self): with temp_dir() as script_dir: mod_name = 'not_main' script_name = self._make_test_script(script_dir, mod_name) msg = "can't find '__main__' module in %r" % script_dir self._check_import_error(script_dir, msg)
def test_module_in_package_in_zipfile(self): with temp_dir() as script_dir: zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script') launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name) self._check_script(launch_name, run_name, run_name, zip_name, 'test_pkg', zipimport.zipimporter)
def test_basic_script_no_suffix(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'script', omit_suffix=True) self._check_script(script_name)
def test_zipfile(self): with temp_dir() as script_dir: mod_name = '__main__' script_name = self._make_test_script(script_dir, mod_name) zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) self._check_script(zip_name, "<run_path>", fname, zip_name, '')
def setup_test_pkg(self, *args): with temp_dir() as script_dir, \ test.test_support.change_cwd(script_dir): pkg_dir = os.path.join(script_dir, 'test_pkg') make_pkg(pkg_dir, *args) yield pkg_dir
def test_script_compiled(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'script') compiled_name = compile_script(script_name) os.remove(script_name) self._check_script(compiled_name, compiled_name, compiled_name, None)
def test_zipfile_error(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'not_main') zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) msg = "can't find '__main__' module in %r" % zip_name self._check_import_error(zip_name, msg)
def test_basic_script(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'script') self._check_script(script_name, script_name, script_name, script_dir, None, importlib.machinery.SourceFileLoader)
def test_directory(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__') self._check_script(script_dir, script_name, script_dir, script_dir, '', importlib.machinery.SourceFileLoader)
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( "from test import test_doctest", "import test_zipped_doctest as test_doctest") test_src = test_src.replace("test.test_doctest", "test_zipped_doctest") test_src = test_src.replace("test.sample_doctest", "sample_zipped_doctest") # The sample doctest files rewritten to include in the zipped version. sample_sources = {} for mod in [ sample_doctest, sample_doctest_no_doctests, sample_doctest_no_docstrings ]: src = inspect.getsource(mod) src = src.replace("test.test_doctest", "test_zipped_doctest") # 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 with temp_dir() as d: script_name = make_script(d, 'test_zipped_doctest', test_src) zip_name, run_name = make_zip_script(d, 'test_zip', script_name) z = zipfile.ZipFile(zip_name, 'a') for mod_name, src in sample_sources.items(): z.writestr(mod_name + ".py", src) z.close() if verbose: zip_file = zipfile.ZipFile(zip_name, 'r') print('Contents of %r:' % zip_name) zip_file.printdir() zip_file.close() os.remove(script_name) sys.path.insert(0, zip_name) import 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_testsource, test_zipped_doctest.test_trailing_space_in_test, test_zipped_doctest.test_DocTestSuite, test_zipped_doctest.test_DocTestFinder, ] # These 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_basic_script(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'script') self._check_script(script_name, script_name, script_name, None)
def test_directory_compiled(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__') compiled_name = compile_script(script_name) os.remove(script_name) self._check_script(script_dir, compiled_name, script_dir, '')
def test_directory(self): with temp_dir() as script_dir: mod_name = '__main__' script_name = self._make_test_script(script_dir, mod_name) self._check_script(script_dir, "<run_path>", script_name, script_dir)
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( "from test import test_doctest", "import test_zipped_doctest as test_doctest") test_src = test_src.replace("test.test_doctest", "test_zipped_doctest") test_src = test_src.replace("test.sample_doctest", "sample_zipped_doctest") sample_src = inspect.getsource(sample_doctest) sample_src = sample_src.replace("test.test_doctest", "test_zipped_doctest") with temp_dir() as d: script_name = make_script(d, 'test_zipped_doctest', test_src) 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) z.close() if verbose: zip_file = zipfile.ZipFile(zip_name, 'r') print 'Contents of %r:' % zip_name zip_file.printdir() zip_file.close() 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.old_test1, test_zipped_doctest.old_test2, test_zipped_doctest.old_test3, test_zipped_doctest.old_test4, 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, ] # Needed for test_DocTestParser and test_debug deprecations = [ # Ignore all warnings about the use of class Tester in this module. ("class Tester is deprecated", DeprecationWarning) ] if sys.py3kwarning: deprecations += [("backquote not supported", SyntaxWarning), ("execfile.. not supported", DeprecationWarning)] with test.test_support.check_warnings(*deprecations): for obj in known_good_tests: _run_object_doctest(obj, test_zipped_doctest)
def test_basic_script(self): with temp_dir() as script_dir: mod_name = 'script' script_name = self._make_test_script(script_dir, mod_name) self._check_script(script_name, "<run_path>", script_name, script_name)
def test_directory_error(self): with temp_dir() as script_dir: msg = "can't find '__main__' module in %r" % script_dir self._check_import_error(script_dir, msg)
def test_directory(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__') self._check_script(script_dir, script_name, script_dir, '')
def test_zipfile(self): with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__') zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) self._check_script(zip_name, run_name, zip_name, '')
def test_directory(self): source = self.main_in_children_source with temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__', source=source) self._check_script(script_dir)