Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_tempfile.test_TemporaryDirectory Class Reference
Inheritance diagram for test.test_tempfile.test_TemporaryDirectory:
Inheritance graph
[legend]
Collaboration diagram for test.test_tempfile.test_TemporaryDirectory:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def do_create
def test_mkdtemp_failure
def test_explicit_cleanup
def test_cleanup_with_symlink_to_a_directory
def test_del_on_collection
def test_del_on_shutdown
def test_warnings_on_cleanup
def test_multiple_close
def test_context_manager
def setUp
def tearDown
def failOnException
def nameCheck

Static Public Attributes

tuple str_check = re.compile(r"[a-zA-Z0-9_-]{6}$")

Detailed Description

Test TemporaryDirectory().

Definition at line 927 of file test_tempfile.py.


Member Function Documentation

def test.test_tempfile.test_TemporaryDirectory.do_create (   self,
  dir = None,
  pre = "",
  suf = "",
  recurse = 1 
)

Definition at line 930 of file test_tempfile.py.

00930 
00931     def do_create(self, dir=None, pre="", suf="", recurse=1):
00932         if dir is None:
00933             dir = tempfile.gettempdir()
00934         try:
00935             tmp = tempfile.TemporaryDirectory(dir=dir, prefix=pre, suffix=suf)
00936         except:
00937             self.failOnException("TemporaryDirectory")
00938         self.nameCheck(tmp.name, dir, pre, suf)
00939         # Create a subdirectory and some files
00940         if recurse:
00941             self.do_create(tmp.name, pre, suf, recurse-1)
00942         with open(os.path.join(tmp.name, "test.txt"), "wb") as f:
00943             f.write(b"Hello world!")
00944         return tmp

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_tempfile.TC.failOnException (   self,
  what,
  ei = None 
) [inherited]

Definition at line 47 of file test_tempfile.py.

00047 
00048     def failOnException(self, what, ei=None):
00049         if ei is None:
00050             ei = sys.exc_info()
00051         self.fail("%s raised %s: %s" % (what, ei[0], ei[1]))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_tempfile.TC.nameCheck (   self,
  name,
  dir,
  pre,
  suf 
) [inherited]

Definition at line 52 of file test_tempfile.py.

00052 
00053     def nameCheck(self, name, dir, pre, suf):
00054         (ndir, nbase) = os.path.split(name)
00055         npre  = nbase[:len(pre)]
00056         nsuf  = nbase[len(nbase)-len(suf):]
00057 
00058         # check for equality of the absolute paths!
00059         self.assertEqual(os.path.abspath(ndir), os.path.abspath(dir),
00060                          "file '%s' not in directory '%s'" % (name, dir))
00061         self.assertEqual(npre, pre,
00062                          "file '%s' does not begin with '%s'" % (nbase, pre))
00063         self.assertEqual(nsuf, suf,
00064                          "file '%s' does not end with '%s'" % (nbase, suf))
00065 
00066         nbase = nbase[len(pre):len(nbase)-len(suf)]
00067         self.assertTrue(self.str_check.match(nbase),
00068                      "random string '%s' does not match /^[a-zA-Z0-9_-]{6}$/"
00069                      % nbase)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_tempfile.TC.setUp (   self) [inherited]

Reimplemented in test.test_tempfile.test_mktemp, and test.test_tempfile.test__RandomNameSequence.

Definition at line 37 of file test_tempfile.py.

00037 
00038     def setUp(self):
00039         self._warnings_manager = support.check_warnings()
00040         self._warnings_manager.__enter__()
00041         warnings.filterwarnings("ignore", category=RuntimeWarning,
00042                                 message="mktemp", module=__name__)

Here is the caller graph for this function:

def test.test_tempfile.TC.tearDown (   self) [inherited]

Reimplemented in test.test_tempfile.test_mktemp.

Definition at line 43 of file test_tempfile.py.

00043 
00044     def tearDown(self):
00045         self._warnings_manager.__exit__(None, None, None)
00046 

Here is the caller graph for this function:

Definition at line 968 of file test_tempfile.py.

00968 
00969     def test_cleanup_with_symlink_to_a_directory(self):
00970         # cleanup() should not follow symlinks to directories (issue #12464)
00971         d1 = self.do_create()
00972         d2 = self.do_create()
00973 
00974         # Symlink d1/foo -> d2
00975         os.symlink(d2.name, os.path.join(d1.name, "foo"))
00976 
00977         # This call to cleanup() should not follow the "foo" symlink
00978         d1.cleanup()
00979 
00980         self.assertFalse(os.path.exists(d1.name),
00981                          "TemporaryDirectory %s exists after cleanup" % d1.name)
00982         self.assertTrue(os.path.exists(d2.name),
00983                         "Directory pointed to by a symlink was deleted")
00984         self.assertEqual(os.listdir(d2.name), ['test.txt'],
00985                          "Contents of the directory pointed to by a symlink "
00986                          "were deleted")
00987         d2.cleanup()

Here is the call graph for this function:

Definition at line 1066 of file test_tempfile.py.

01066 
01067     def test_context_manager(self):
01068         # Can be used as a context manager
01069         d = self.do_create()
01070         with d as name:
01071             self.assertTrue(os.path.exists(name))
01072             self.assertEqual(name, d.name)
01073         self.assertFalse(os.path.exists(name))
01074 
01075 
01076 test_classes.append(test_TemporaryDirectory)

Here is the call graph for this function:

Definition at line 989 of file test_tempfile.py.

00989 
00990     def test_del_on_collection(self):
00991         # A TemporaryDirectory is deleted when garbage collected
00992         dir = tempfile.mkdtemp()
00993         try:
00994             d = self.do_create(dir=dir)
00995             name = d.name
00996             del d # Rely on refcounting to invoke __del__
00997             self.assertFalse(os.path.exists(name),
00998                         "TemporaryDirectory %s exists after __del__" % name)
00999         finally:
01000             os.rmdir(dir)

Here is the call graph for this function:

Definition at line 1002 of file test_tempfile.py.

01002 
01003     def test_del_on_shutdown(self):
01004         # A TemporaryDirectory may be cleaned up during shutdown
01005         # Make sure it works with the relevant modules nulled out
01006         with self.do_create() as dir:
01007             d = self.do_create(dir=dir)
01008             # Mimic the nulling out of modules that
01009             # occurs during system shutdown
01010             modules = [os, os.path]
01011             if has_stat:
01012                 modules.append(stat)
01013             # Currently broken, so suppress the warning
01014             # that is otherwise emitted on stdout
01015             with support.captured_stderr() as err:
01016                 with NulledModules(*modules):
01017                     d.cleanup()
01018             # Currently broken, so stop spurious exception by
01019             # indicating the object has already been closed
01020             d._closed = True
01021             # And this assert will fail, as expected by the
01022             # unittest decorator...
01023             self.assertFalse(os.path.exists(d.name),
01024                         "TemporaryDirectory %s exists after cleanup" % d.name)

Here is the call graph for this function:

Definition at line 954 of file test_tempfile.py.

00954 
00955     def test_explicit_cleanup(self):
00956         # A TemporaryDirectory is deleted when cleaned up
00957         dir = tempfile.mkdtemp()
00958         try:
00959             d = self.do_create(dir=dir)
00960             self.assertTrue(os.path.exists(d.name),
00961                             "TemporaryDirectory %s does not exist" % d.name)
00962             d.cleanup()
00963             self.assertFalse(os.path.exists(d.name),
00964                         "TemporaryDirectory %s exists after cleanup" % d.name)
00965         finally:
00966             os.rmdir(dir)

Here is the call graph for this function:

Definition at line 945 of file test_tempfile.py.

00945 
00946     def test_mkdtemp_failure(self):
00947         # Check no additional exception if mkdtemp fails
00948         # Previously would raise AttributeError instead
00949         # (noted as part of Issue #10188)
00950         with tempfile.TemporaryDirectory() as nonexistent:
00951             pass
00952         with self.assertRaises(os.error):
00953             tempfile.TemporaryDirectory(dir=nonexistent)

Here is the call graph for this function:

Definition at line 1056 of file test_tempfile.py.

01056 
01057     def test_multiple_close(self):
01058         # Can be cleaned-up many times without error
01059         d = self.do_create()
01060         d.cleanup()
01061         try:
01062             d.cleanup()
01063             d.cleanup()
01064         except:
01065             self.failOnException("cleanup")

Here is the call graph for this function:

Definition at line 1025 of file test_tempfile.py.

01025 
01026     def test_warnings_on_cleanup(self):
01027         # Two kinds of warning on shutdown
01028         #   Issue 10888: may write to stderr if modules are nulled out
01029         #   ResourceWarning will be triggered by __del__
01030         with self.do_create() as dir:
01031             if os.sep != '\\':
01032                 # Embed a backslash in order to make sure string escaping
01033                 # in the displayed error message is dealt with correctly
01034                 suffix = '\\check_backslash_handling'
01035             else:
01036                 suffix = ''
01037             d = self.do_create(dir=dir, suf=suffix)
01038 
01039             #Check for the Issue 10888 message
01040             modules = [os, os.path]
01041             if has_stat:
01042                 modules.append(stat)
01043             with support.captured_stderr() as err:
01044                 with NulledModules(*modules):
01045                     d.cleanup()
01046             message = err.getvalue().replace('\\\\', '\\')
01047             self.assertIn("while cleaning up",  message)
01048             self.assertIn(d.name,  message)
01049 
01050             # Check for the resource warning
01051             with support.check_warnings(('Implicitly', ResourceWarning), quiet=False):
01052                 warnings.filterwarnings("always", category=ResourceWarning)
01053                 d.__del__()
01054             self.assertFalse(os.path.exists(d.name),
01055                         "TemporaryDirectory %s exists after __del__" % d.name)

Here is the call graph for this function:


Member Data Documentation

tuple test.test_tempfile.TC.str_check = re.compile(r"[a-zA-Z0-9_-]{6}$") [static, inherited]

Definition at line 35 of file test_tempfile.py.


The documentation for this class was generated from the following file: