def test_consistent_sys_path_for_direct_execution(self): # This test case ensures that the following all give the same # sys.path configuration: # # ./python -s script_dir/__main__.py # ./python -s script_dir # ./python -I script_dir script = textwrap.dedent("""\ import sys for entry in sys.path: print(entry) """) # Always show full path diffs on errors self.maxDiff = None with support.temp_dir() as work_dir, support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__', script) # Reference output comes from directly executing __main__.py # We omit PYTHONPATH and user site to align with isolated mode p = spawn_python("-Es", script_name, cwd=work_dir) out_by_name = kill_python(p).decode().splitlines() self.assertEqual(out_by_name[0], script_dir) self.assertNotIn(work_dir, out_by_name) # Directory execution should give the same output p = spawn_python("-Es", script_dir, cwd=work_dir) out_by_dir = kill_python(p).decode().splitlines() self.assertEqual(out_by_dir, out_by_name) # As should directory execution in isolated mode p = spawn_python("-I", script_dir, cwd=work_dir) out_by_dir_isolated = kill_python(p).decode().splitlines() self.assertEqual(out_by_dir_isolated, out_by_dir, out_by_name)
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.assertRaisesRegex(RecursionError, msg, run_path, zip_name)
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_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, expect_spec=False)
def test_script_compiled(self): with support.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_package_error(self): with support.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") self._check_import_error(["-m", "test_pkg"], msg, cwd=script_dir)
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_ismount(self): self.assertTrue(ntpath.ismount("c:\\")) self.assertTrue(ntpath.ismount("C:\\")) self.assertTrue(ntpath.ismount("c:/")) self.assertTrue(ntpath.ismount("C:/")) self.assertTrue(ntpath.ismount("\\\\.\\c:\\")) self.assertTrue(ntpath.ismount("\\\\.\\C:\\")) self.assertTrue(ntpath.ismount(b"c:\\")) self.assertTrue(ntpath.ismount(b"C:\\")) self.assertTrue(ntpath.ismount(b"c:/")) self.assertTrue(ntpath.ismount(b"C:/")) self.assertTrue(ntpath.ismount(b"\\\\.\\c:\\")) self.assertTrue(ntpath.ismount(b"\\\\.\\C:\\")) with support.temp_dir() as d: self.assertFalse(ntpath.ismount(d)) if sys.platform == "win32": # # Make sure the current folder isn't the root folder # (or any other volume root). The drive-relative # locations below cannot then refer to mount points # drive, path = ntpath.splitdrive(sys.executable) with support.change_cwd(os.path.dirname(sys.executable)): self.assertFalse(ntpath.ismount(drive.lower())) self.assertFalse(ntpath.ismount(drive.upper())) self.assertTrue(ntpath.ismount("\\\\localhost\\c$")) self.assertTrue(ntpath.ismount("\\\\localhost\\c$\\")) self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$")) self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\"))
def test_zipfile_compiled(self): with support.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_history_size(self): history_size = 10 with temp_dir() as test_dir: inputrc = os.path.join(test_dir, "inputrc") with open(inputrc, "wb") as f: f.write(b"set history-size %d\n" % history_size) history_file = os.path.join(test_dir, "history") with open(history_file, "wb") as f: # history_size * 2 items crashes readline data = b"".join(b"item %d\n" % i for i in range(history_size * 2)) f.write(data) script = """ import os import readline history_file = os.environ["HISTORY_FILE"] readline.read_history_file(history_file) input() readline.write_history_file(history_file) """ env = dict(os.environ) env["INPUTRC"] = inputrc env["HISTORY_FILE"] = history_file run_pty(script, input=b"last input\r", env=env) with open(history_file, "rb") as f: lines = f.readlines() self.assertEqual(len(lines), history_size) self.assertEqual(lines[-1].strip(), b"last input")
def test_script_compiled(self): with support.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_zipfile(self): source = self.main_in_children_source with support.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_package_error(self): with support.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_module_in_subpackage_in_zipfile(self): with support.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_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_module_in_subpackage_in_zipfile(self): with support.temp_dir() as script_dir: zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2) self._check_script(["-m", "test_pkg.test_pkg.script"], run_name, run_name, script_dir, 'test_pkg.test_pkg', zipimport.zipimporter, PYTHONPATH=zip_name, cwd=script_dir)
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_zipfile_compiled(self): source = self.main_in_children_source with support.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_module_in_package(self): with support.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, 'check_sibling') launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.check_sibling') self._check_script(launch_name)
def test_syntaxerror_unindented_caret_position(self): script = "1 + 1 = 2\n" with support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'script', script) exitcode, stdout, stderr = assert_python_failure(script_name) text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read() # Confirm that the caret is located under the first 1 character self.assertIn("\n 1 + 1 = 2\n ^", text)
def test_package(self): with support.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_directory_compiled(self): source = self.main_in_children_source with support.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_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) if not sys.dont_write_bytecode: legacy_pyc = make_legacy_pyc(script_name) self._check_script(script_dir, "<run_path>", legacy_pyc, script_dir, mod_name=mod_name)
def test_zipfile_compiled_unchecked_hash(self): with support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__') compiled_name = py_compile.compile( script_name, doraise=True, invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH) 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_package(self): with support.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') self._check_script(["-m", "test_pkg.script"], script_name, script_name, script_dir, 'test_pkg', importlib.machinery.SourceFileLoader, cwd=script_dir)
def test_module_in_package(self): with support.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_zipfile_compiled_timestamp(self): with support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__') compiled_name = py_compile.compile( script_name, doraise=True, invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP) 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_package(self): source = self.main_in_children_source with support.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_change_cwd(self): original_cwd = os.getcwd() with support.temp_dir() as temp_path: with support.change_cwd(temp_path) as new_cwd: self.assertEqual(new_cwd, temp_path) self.assertEqual(os.getcwd(), new_cwd) self.assertEqual(os.getcwd(), original_cwd)
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 s = "non-ASCII: h\xe9" """) result = run_path(filename) self.assertEqual(result['s'], "non-ASCII: h\xe9")
def test_consistent_sys_path_for_module_execution(self): # This test case ensures that the following both give the same # sys.path configuration: # ./python -sm script_pkg.__main__ # ./python -sm script_pkg # # And that this fails as unable to find the package: # ./python -Im script_pkg script = textwrap.dedent("""\ import sys for entry in sys.path: print(entry) """) # Always show full path diffs on errors self.maxDiff = None with support.temp_dir() as work_dir: script_dir = os.path.join(work_dir, "script_pkg") os.mkdir(script_dir) script_name = _make_test_script(script_dir, '__main__', script) # Reference output comes from `-m script_pkg.__main__` # We omit PYTHONPATH and user site to better align with the # direct execution test cases p = spawn_python("-sm", "script_pkg.__main__", cwd=work_dir) out_by_module = kill_python(p).decode().splitlines() self.assertEqual(out_by_module[0], work_dir) self.assertNotIn(script_dir, out_by_module) # Package execution should give the same output p = spawn_python("-sm", "script_pkg", cwd=work_dir) out_by_package = kill_python(p).decode().splitlines() self.assertEqual(out_by_package, out_by_module) # Isolated mode should fail with an import error exitcode, stdout, stderr = assert_python_failure("-Im", "script_pkg", cwd=work_dir) traceback_lines = stderr.decode().splitlines() self.assertIn("No module named script_pkg", traceback_lines[-1])
def test_external(self): # bpo-42398: Test that the destination file is left unchanged if the # content does not change. Moreover, check also that the file # modification time does not change in this case. source = support.findfile('clinic.test') with open(source, 'r', encoding='utf-8') as f: orig_contents = f.read() with support.temp_dir() as tmp_dir: testfile = os.path.join(tmp_dir, 'clinic.test.c') with open(testfile, 'w', encoding='utf-8') as f: f.write(orig_contents) old_mtime_ns = os.stat(testfile).st_mtime_ns clinic.parse_file(testfile) with open(testfile, 'r', encoding='utf-8') as f: new_contents = f.read() new_mtime_ns = os.stat(testfile).st_mtime_ns self.assertEqual(new_contents, orig_contents) # Don't change the file modification time # if the content does not change self.assertEqual(new_mtime_ns, old_mtime_ns)
def test_find_executable(self): with test_support.temp_dir() as tmp_dir: # use TESTFN to get a pseudo-unique filename program_noeext = test_support.TESTFN # Give the temporary program an ".exe" suffix for all. # It's needed on Windows and not harmful on other platforms. program = program_noeext + ".exe" filename = os.path.join(tmp_dir, program) with open(filename, "wb"): pass os.chmod(filename, stat.S_IXUSR) # test path parameter rv = find_executable(program, path=tmp_dir) self.assertEqual(rv, filename) if sys.platform == 'win32': # test without ".exe" extension rv = find_executable(program_noeext, path=tmp_dir) self.assertEqual(rv, filename) # test find in the current directory with test_support.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) # test non-existent program dont_exist_program = "dontexist_" + program rv = find_executable(dont_exist_program, path=tmp_dir) self.assertIsNone(rv) # PATH='': no match, except in the current directory with test_support.EnvironmentVarGuard() as env: env['PATH'] = '' with unittest.mock.patch('distutils.spawn.os.confstr', return_value=tmp_dir, create=True), \ unittest.mock.patch('distutils.spawn.os.defpath', tmp_dir): rv = find_executable(program) self.assertIsNone(rv) # look in current directory with test_support.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) # PATH=':': explicitly looks in the current directory with test_support.EnvironmentVarGuard() as env: env['PATH'] = os.pathsep with unittest.mock.patch('distutils.spawn.os.confstr', return_value='', create=True), \ unittest.mock.patch('distutils.spawn.os.defpath', ''): rv = find_executable(program) self.assertIsNone(rv) # look in current directory with test_support.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) # missing PATH: test os.confstr("CS_PATH") and os.defpath with test_support.EnvironmentVarGuard() as env: env.pop('PATH', None) # without confstr with unittest.mock.patch('distutils.spawn.os.confstr', side_effect=ValueError, create=True), \ unittest.mock.patch('distutils.spawn.os.defpath', tmp_dir): rv = find_executable(program) self.assertEqual(rv, filename) # with confstr with unittest.mock.patch('distutils.spawn.os.confstr', return_value=tmp_dir, create=True), \ unittest.mock.patch('distutils.spawn.os.defpath', ''): rv = find_executable(program) self.assertEqual(rv, filename)
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, expect_spec=False)
def test_directory(self): source = self.main_in_children_source with support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__', source=source) self._check_script(script_dir)
def tmp_path(*args, **kwargs): with temp_dir() as tmp_fn: yield pathlib.Path(tmp_fn)
self.assertIsNone(mimetypes.read_mime_types("non-existent")) with os_helper.temp_dir() as directory: data = "x-application/x-unittest pyunit\n" file = pathlib.Path(directory, "sample.mimetype") file.write_text(data) mime_dict = mimetypes.read_mime_types(file) eq(mime_dict[".pyunit"], "x-application/x-unittest") # bpo-41048: read_mime_types should read the rule file with 'utf-8' encoding. # Not with locale encoding. _bootlocale has been imported because io.open(...) # uses it. <<<<<<< HEAD with os_helper.temp_dir() as directory: ======= with support.temp_dir() as directory: >>>>>>> 3.9 data = "application/no-mans-land Fran\u00E7ais" file = pathlib.Path(directory, "sample.mimetype") file.write_text(data, encoding='utf-8') import _bootlocale with support.swap_attr(_bootlocale, 'getpreferredencoding', lambda do_setlocale=True: 'ASCII'): mime_dict = mimetypes.read_mime_types(file) eq(mime_dict[".Français"], "application/no-mans-land") def test_non_standard_types(self): eq = self.assertEqual # First try strict eq(self.db.guess_type('foo.xul', strict=True), (None, None)) eq(self.db.guess_extension('image/jpg', strict=True), None) # And then non-strict
def test_directory(self): with support.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_zipfile(self): with support.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, zip_name, '', zipimport.zipimporter)
def test_module_in_package_in_zipfile(self): with support.temp_dir() as script_dir: zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script') self._check_script(["-m", "test_pkg.script"], run_name, run_name, script_dir, 'test_pkg', zipimport.zipimporter, PYTHONPATH=zip_name, cwd=script_dir)
def test_temp_dir__path_none(self): """Test passing no path.""" with support.temp_dir() as temp_path: self.assertTrue(os.path.isdir(temp_path)) self.assertFalse(os.path.isdir(temp_path))
def test_basic_script(self): with support.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: mod_name = '__main__' script_name = self._make_test_script(script_dir, mod_name) self._check_script(script_dir, "<run_path>", script_name, script_dir, mod_name=mod_name)
def test_directory_error(self): with support.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_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_zipfile_error(self): with support.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 support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'script') self._check_script(script_name)
def setup_test_pkg(self, *args): with support.temp_dir() as script_dir, \ support.change_cwd(path=script_dir): pkg_dir = os.path.join(script_dir, 'test_pkg') make_pkg(pkg_dir, *args) yield pkg_dir
def test_basic_script_no_suffix(self): with support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'script', omit_suffix=True) self._check_script(script_name)
def call_temp_dir(path): with support.temp_dir(path) as temp_path: raise Exception("should not get here")
def test_module_in_package_in_zipfile(self): with support.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)