def _inside_empty_temp_dir(): dir = tempfile.mkdtemp() try: with support.swap_attr(tempfile, 'tempdir', dir): yield finally: support.rmtree(dir)
def tearDown(self): try: os.chdir(self.cwd) if self.pythonexe != sys.executable: os.remove(self.pythonexe) test_support.rmtree(self.parent_dir) finally: BaseTestCase.tearDown(self)
def tearDown(self): sys.path[:] = self.sys_path if self.orig_module is not None: sys.modules[self.module_name] = self.orig_module else: unload(self.module_name) unlink(self.file_name) unlink(self.compiled_name) rmtree(self.dir_name)
def get_new_environment_path() : path=get_new_path("environment") import os try: os.makedirs(path,mode=0700) except os.error: test_support.rmtree(path) os.makedirs(path) return path
def get_new_environment_path() : path=get_new_path("environment") import os try: os.makedirs(path,mode=0o700) except os.error: test_support.rmtree(path) os.makedirs(path) return path
def check_script_output(self, src, expected): tmpd = tempfile.mkdtemp() try: fn = os.path.join(tmpd, 'test.py') with open(fn, 'wb') as fp: fp.write(src) rc, out, err = script_helper.assert_python_ok(fn) finally: rmtree(tmpd) self.assertEqual(out.rstrip(), expected)
def create_modules(*names): u"""Temporarily create each named module with an attribute (named 'attr') that contains the name passed into the context manager that caused the creation of the module. All files are created in a temporary directory returned by tempfile.mkdtemp(). This directory is inserted at the beginning of sys.path. When the context manager exits all created files (source and bytecode) are explicitly deleted. No magic is performed when creating packages! This means that if you create a module within a package you must also create the package's __init__ as well. """ source = u'attr = {0!r}' created_paths = [] mapping = {} state_manager = None uncache_manager = None try: temp_dir = tempfile.mkdtemp() mapping[u'.root'] = temp_dir import_names = set() for name in names: if not name.endswith(u'__init__'): import_name = name else: import_name = name[:-len(u'.__init__')] import_names.add(import_name) if import_name in sys.modules: del sys.modules[import_name] name_parts = name.split(u'.') file_path = temp_dir for directory in name_parts[:-1]: file_path = os.path.join(file_path, directory) if not os.path.exists(file_path): os.mkdir(file_path) created_paths.append(file_path) file_path = os.path.join(file_path, name_parts[-1] + u'.py') with open(file_path, u'w') as file: file.write(source.format(name)) created_paths.append(file_path) mapping[name] = file_path uncache_manager = util.uncache(*import_names) uncache_manager.__enter__() state_manager = util.import_state(path=[temp_dir]) state_manager.__enter__() yield mapping finally: if state_manager is not None: state_manager.__exit__(None, None, None) if uncache_manager is not None: uncache_manager.__exit__(None, None, None) support.rmtree(temp_dir)
def test_yet_more_evil_still_undecodable(self): # Issue #25388 src = b"#\x00\n#\xfd\n" tmpd = tempfile.mkdtemp() try: fn = os.path.join(tmpd, "bad.py") with open(fn, "wb") as fp: fp.write(src) rc, out, err = script_helper.assert_python_failure(fn) finally: test_support.rmtree(tmpd) self.assertIn(b"Non-ASCII", err)
def test_replace_parent_in_sys_modules(self): dir_name = os.path.abspath(TESTFN) os.mkdir(dir_name) try: pkg_dir = os.path.join(dir_name, 'sa') os.mkdir(pkg_dir) with open(os.path.join(pkg_dir, '__init__.py'), 'w') as init_file: init_file.write("import v1") with open(os.path.join(pkg_dir, 'v1.py'), 'w') as v1_file: v1_file.write("import sys;" "sys.modules['sa'] = sys.modules[__name__];" "import sa") sys.path.insert(0, dir_name) # a segfault means the test failed! import sa finally: rmtree(dir_name)
def test_yet_more_evil_still_undecodable(self): # Issue #25388 src = b"#\x00\n#\xfd\n" tmpd = tempfile.mkdtemp() try: fn = os.path.join(tmpd, "bad.py") with open(fn, "wb") as fp: fp.write(src) try: rc, out, err = script_helper.assert_python_failure(fn) except AssertionError: if check_impl_detail(pypy=True): # as long as we don't crash return raise finally: test_support.rmtree(tmpd) self.assertIn(b"Non-ASCII", err)
def test_readonly_files(self): dir = _fname os.mkdir(dir) try: fname = os.path.join(dir, 'db') f = dumbdbm.open(fname, 'n') self.assertEqual(list(f.keys()), []) for key in self._dict: f[key] = self._dict[key] f.close() os.chmod(fname + ".dir", stat.S_IRUSR) os.chmod(fname + ".dat", stat.S_IRUSR) os.chmod(dir, stat.S_IRUSR|stat.S_IXUSR) f = dumbdbm.open(fname, 'r') self.assertEqual(sorted(f.keys()), sorted(self._dict)) f.close() # don't write finally: test_support.rmtree(dir)
def test_readonly_files(self): dir = _fname os.mkdir(dir) try: fname = os.path.join(dir, 'db') f = dumbdbm.open(fname, 'n') self.assertEqual(list(f.keys()), []) for key in self._dict: f[key] = self._dict[key] f.close() os.chmod(fname + ".dir", stat.S_IRUSR) os.chmod(fname + ".dat", stat.S_IRUSR) os.chmod(dir, stat.S_IRUSR | stat.S_IXUSR) f = dumbdbm.open(fname, 'r') self.assertEqual(sorted(f.keys()), sorted(self._dict)) f.close() # don't write finally: test_support.rmtree(dir)
def test_security(self): # This test is incomplete since we are normally not run as root and # therefore can't test the file ownership being wrong. os.unlink(temp_filename) d = test_support.TESTFN try: os.mkdir(d) fn = os.path.join(d, '.netrc') with open(fn, 'wt') as f: f.write(TEST_NETRC) with test_support.EnvironmentVarGuard() as environ: environ.set('HOME', d) os.chmod(fn, 0600) self.netrc = netrc.netrc() self.test_case_1() os.chmod(fn, 0622) self.assertRaises(netrc.NetrcParseError, netrc.netrc) finally: test_support.rmtree(d)
def tearDown(self): rmtree(self.path) sys.path[:] = self.syspath
def tearDownClass(cls): td = cls.tmpdir if td and os.path.isdir(td): test_support.rmtree(td)
def tearDown(self): rmtree(self.topdir)
def tearDown(self): test_support.rmtree(self.tmpdir)
def remove_test_path_directory() : test_support.rmtree(get_new_path.prefix)
def tearDown(self): rmtree(TESTFN) unlink(TESTFN)
def newdirinpath(dir): os.mkdir(dir) sys.path.insert(0, dir) yield sys.path.pop(0) rmtree(dir)