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_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)
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 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): 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_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_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_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_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_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_directory_compiled(self): with support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, '__main__') py_compile.compile(script_name, doraise=True) os.remove(script_name) pyc_file = support.make_legacy_pyc(script_name) self._check_script(script_dir, pyc_file, script_dir, script_dir, '', importlib.machinery.SourcelessFileLoader)
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_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_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_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_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_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_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_syshook_no_logdir_text_format(self): # Issue 12890: we were emitting the <p> tag in text mode. with temp_dir() as tracedir: rc, out, err = assert_python_failure( '-c', ('import cgitb; cgitb.enable(format="text", logdir=%s); ' 'raise ValueError("Hello World")') % repr(tracedir)) out = out.decode(sys.getfilesystemencoding()) self.assertIn("ValueError", out) self.assertIn("Hello World", out) self.assertNotIn('<p>', out) self.assertNotIn('</p>', out)
def test_package_compiled(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) compiled_name = py_compile.compile(script_name, doraise=True) os.remove(script_name) pyc_file = support.make_legacy_pyc(script_name) launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') self._check_script(launch_name)
def test_syshook_no_logdir_default_format(self): with temp_dir() as tracedir: rc, out, err = assert_python_failure( '-c', ('import cgitb; cgitb.enable(logdir=%s); ' 'raise ValueError("Hello World")') % repr(tracedir)) out = out.decode(sys.getfilesystemencoding()) self.assertIn("ValueError", out) self.assertIn("Hello World", out) # By default we emit HTML markup. self.assertIn('<p>', out) self.assertIn('</p>', out)
def test_package_compiled(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__') compiled_name = py_compile.compile(script_name, doraise=True) os.remove(script_name) pyc_file = support.make_legacy_pyc(script_name) launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') self._check_script(launch_name, pyc_file, pyc_file, script_dir, 'test_pkg', importlib.machinery.SourcelessFileLoader)
def test_readonly_files(self): with support.temp_dir() as dir: fname = os.path.join(dir, 'db') with dumbdbm.open(fname, 'n') as f: self.assertEqual(list(f.keys()), []) for key in self._dict: f[key] = self._dict[key] os.chmod(fname + ".dir", stat.S_IRUSR) os.chmod(fname + ".dat", stat.S_IRUSR) os.chmod(dir, stat.S_IRUSR | stat.S_IXUSR) with dumbdbm.open(fname, 'r') as f: self.assertEqual(sorted(f.keys()), sorted(self._dict)) f.close() # don't write
def test_dash_m_bad_pyc(self): with support.temp_dir() as script_dir, \ support.change_cwd(path=script_dir): os.mkdir('test_pkg') # Create invalid *.pyc as empty file with open('test_pkg/__init__.pyc', 'wb'): pass err = self.check_dash_m_failure('test_pkg') self.assertRegex( err, br'Error while finding module specification.*' br'ImportError.*bad magic number') self.assertNotIn(b'is a package', err) self.assertNotIn(b'Traceback', err)
def test_change_cwd__non_existent_dir(self): """Test passing a non-existent directory.""" original_cwd = os.getcwd() def call_change_cwd(path): with support.change_cwd(path) as new_cwd: raise Exception("should not get here") with support.temp_dir() as parent_dir: non_existent_dir = os.path.join(parent_dir, 'does_not_exist') self.assertRaises(FileNotFoundError, call_change_cwd, non_existent_dir) self.assertEqual(os.getcwd(), original_cwd)
def test_change_cwd__non_existent_dir__quiet_true(self): """Test passing a non-existent directory with quiet=True.""" original_cwd = os.getcwd() with support.temp_dir() as parent_dir: bad_dir = os.path.join(parent_dir, 'does_not_exist') with support.check_warnings() as recorder: with support.change_cwd(bad_dir, quiet=True) as new_cwd: self.assertEqual(new_cwd, original_cwd) self.assertEqual(os.getcwd(), new_cwd) warnings = [str(w.message) for w in recorder.warnings] expected = ['tests may fail, unable to change CWD to: ' + bad_dir] self.assertEqual(warnings, expected)
def test_temp_dir(self): """Test that temp_dir() creates and destroys its directory.""" parent_dir = tempfile.mkdtemp() parent_dir = os.path.realpath(parent_dir) try: path = os.path.join(parent_dir, 'temp') self.assertFalse(os.path.isdir(path)) with support.temp_dir(path) as temp_path: self.assertEqual(temp_path, path) self.assertTrue(os.path.isdir(path)) self.assertFalse(os.path.isdir(path)) finally: support.rmtree(parent_dir)
def test_issue8202_dash_m_file_ignored(self): # Make sure a "-m" file in the current directory # does not alter the value of sys.path[0] with support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'other') with support.change_cwd(path=script_dir): with open("-m", "w") as f: f.write("data") rc, out, err = assert_python_ok('-m', 'other', *example_args, __isolated=False) self._check_output(script_name, rc, out, script_name, script_name, '', '', importlib.machinery.SourceFileLoader)
def test_issue8202_dash_c_file_ignored(self): # Make sure a "-c" file in the current directory # does not alter the value of sys.path[0] with support.temp_dir() as script_dir: with support.change_cwd(path=script_dir): with open("-c", "w") as f: f.write("data") rc, out, err = assert_python_ok( '-c', 'import sys; print("sys.path[0]==%r" % sys.path[0])', __isolated=False) if verbose > 1: print(repr(out)) expected = "sys.path[0]==%r" % '' self.assertIn(expected.encode('utf-8'), out)
def test_ipython_workaround(self): # Some versions of the IPython launch script are missing the # __name__ = "__main__" guard, and multiprocessing has long had # a workaround for that case # See https://github.com/ipython/ipython/issues/4698 source = test_source_main_skipped_in_children with support.temp_dir() as script_dir: script_name = _make_test_script(script_dir, 'ipython', source=source) self._check_script(script_name) script_no_suffix = _make_test_script(script_dir, 'ipython', source=source, omit_suffix=True) self._check_script(script_no_suffix)