def test_tmpdir12(self): """test manual removal of the tmpdir""" tmpdir = TemporaryDirectory(dir=self._tmpdir) path = tmpdir.path self.assertTrue(os.path.exists(path)) os.rmdir(path) self.assertFalse(os.path.exists(path)) # as long as the directory is gone, we are fine tmpdir.rmdir()
def test_tmpdir2(self): """simple temporary directory (remove with rmdir)""" tmpdir = TemporaryDirectory(dir=self._tmpdir) path = tmpdir.path self.assertIsNotNone(path) self.assertTrue(os.path.isdir(path)) tmpdir.rmdir() self.assertIsNone(tmpdir.path) self.assertFalse(os.path.isdir(path))
def test_tmpdir1(self): """simple temporary directory""" tmpdir = TemporaryDirectory(dir=self._tmpdir) path = tmpdir.path self.assertIsNotNone(path) self.assertTrue(os.path.isdir(path)) self.assertEqual(str(tmpdir), path) # a simple assignment to path is not supposed to work, but _path # can still be modified (_never_ do that) self.assertRaises(AttributeError, setattr, tmpdir, 'path', path) tmpdir.rmtree() self.assertIsNone(tmpdir.path) self.assertFalse(os.path.isdir(path))
def test_tmpdir6(self): """test context manager""" path = None with TemporaryDirectory(dir=self._tmpdir) as tmpdir: path = tmpdir.path self.assertTrue(os.path.isdir(path)) self.assertIsNone(tmpdir.path) self.assertFalse(os.path.isdir(path))
def test_tmpdir4(self): """test __del__ method""" tmpdir = TemporaryDirectory(dir=self._tmpdir) path = tmpdir.path self.assertIsNotNone(path) self.assertTrue(os.path.isdir(path)) del tmpdir self.assertFalse(os.path.isdir(path))
def test_tmpdir10(self): """do not remove tmpdir""" tmpdir = TemporaryDirectory(dir=self._tmpdir, delete=False) path = tmpdir.path self.assertTrue(os.path.isdir(path)) del tmpdir self.assertTrue(os.path.isdir(path)) os.rmdir(path)
def test_tmpdir7(self): """test context manager (double cleanup has no effect)""" path = None with TemporaryDirectory(dir=self._tmpdir) as tmpdir: path = tmpdir.path self.assertTrue(os.path.isdir(path)) self.assertIsNone(tmpdir.path) self.assertFalse(os.path.isdir(path)) # also performs a rmtree del tmpdir
def test_tmpdir8(self): """test context manager (delete=False)""" path = None with TemporaryDirectory(dir=self._tmpdir, delete=False) as tmpdir: path = tmpdir.path self.assertTrue(os.path.isdir(path)) self.assertTrue(os.path.isdir(path)) self.assertIsNotNone(tmpdir.path) del tmpdir self.assertTrue(os.path.isdir(path)) os.rmdir(path)
def test_tmpdir5(self): """test advanced __del__ semantics""" tmpdir = TemporaryDirectory(dir=self._tmpdir) ref = tmpdir path = tmpdir.path self.assertIsNotNone(path) self.assertTrue(os.path.isdir(path)) # ref still references the TemporaryDirectory object del tmpdir self.assertTrue(os.path.isdir(path)) del ref self.assertFalse(os.path.isdir(path))
def test_tmpdir11(self): """test remove nonempty dir with rmdir=True""" tmpdir = TemporaryDirectory(dir=self._tmpdir, rmdir=True) path = tmpdir.path self.assertTrue(os.path.isdir(path)) fname = os.path.join(path, 'foo') with open(fname, 'w'): pass self.assertTrue(os.path.exists(fname)) self.assertRaises(OSError, tmpdir.__del__) self.assertTrue(os.path.exists(fname)) os.unlink(fname) del tmpdir self.assertFalse(os.path.isdir(path))
def test_tmpdir3(self): """multiple remove""" tmpdir = TemporaryDirectory(dir=self._tmpdir) path = tmpdir.path self.assertIsNotNone(path) self.assertTrue(os.path.isdir(path)) tmpdir.rmtree() self.assertFalse(os.path.isdir(path)) # a call to path does not recreate the tmpdir self.assertIsNone(tmpdir.path) self.assertFalse(os.path.isdir(path)) # recreate directory os.mkdir(path) self.assertTrue(os.path.isdir(path)) # subsequent rmdir/rmtree calls have no affect anymore tmpdir.rmdir() tmpdir.rmtree() self.assertTrue(os.path.isdir(path)) os.rmdir(path)
def test_tmpdir13(self): """test __del__ method with module unloading""" # avoid an unraised exception: # Exception AttributeError: "'NoneType' object has no attribute 'path'" # in <bound method TemporaryDirectory.__del__ of # <osc2.util.io.TemporaryDirectory object at 0x7f8abc8d7910>> ignored # # This exception was "raised" in the TemporaryDirectory.__del__ method, # if the "osc2.util.io" was already "unloaded" (the __del__ method # calls the _cleanup method, which tried to call the # os.path.isdir function). # What happened is the following: # If the "osc2.util.io" module is passed to _PyModule_Clear # (Objects/moduleobject.c), almost all entries in the module dict are # set to None. That is, the entry for the key "os" points to None. # Now, suppose that there is still a TemporaryDirectory instance # around, whose __del__ method is eventually called. During this call, # "os" resolves to None and therefore the subsequent lookup for the # name "path" leads to the AttributeError. # Instead of raising this exception, it is just printed to stderr, # because it occurred in a __del__ method (see slot_tp_del # (Objects/typeobject.c) and PyErr_WriteUnraisable (Python/errors.c)). # avoid fiddling with sys.modules in the "main" process pid = os.fork() if pid == 0: ret = os.EX_SOFTWARE try: sio = StringIO() sys.stderr = sio tmpdir = TemporaryDirectory(dir=self._tmpdir) tmpdir.path # clean tmpdir manually, because del tmpdir could # raise an exception os.rmdir(tmpdir.path) del sys.modules['osc2.util.io'] del sys.modules['osc2.util'] del sys.modules['osc2'] del tmpdir self.assertEqual(sio.getvalue(), '') ret = os.EX_OK finally: os._exit(ret) _, status = os.waitpid(pid, 0) self.assertEqual(status, os.WEXITSTATUS(os.EX_OK))
def test_tmpdir9(self): """test __enter__ (tmpdir was already deleted)""" tmpdir = TemporaryDirectory(dir=self._tmpdir) tmpdir.rmtree() self.assertIsNone(tmpdir.path) self.assertRaises(ValueError, tmpdir.__enter__)