Back to index

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

List of all members.

Public Member Functions

def do_create
def test_basic
def test_del_on_close
def test_rewrite_small
def test_write_sequential
def test_writelines
def test_writelines_sequential
def test_sparse
def test_fileno
def test_multiple_close_before_rollover
def test_multiple_close_after_rollover
def test_bound_methods
def test_text_mode
def test_text_newline_and_encoding
def test_context_manager_before_rollover
def test_context_manager_during_rollover
def test_context_manager_after_rollover
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 SpooledTemporaryFile().

Definition at line 650 of file test_tempfile.py.


Member Function Documentation

def test.test_tempfile.test_SpooledTemporaryFile.do_create (   self,
  max_size = 0,
  dir = None,
  pre = "",
  suf = "" 
)

Definition at line 653 of file test_tempfile.py.

00653 
00654     def do_create(self, max_size=0, dir=None, pre="", suf=""):
00655         if dir is None:
00656             dir = tempfile.gettempdir()
00657         try:
00658             file = tempfile.SpooledTemporaryFile(max_size=max_size, dir=dir, prefix=pre, suffix=suf)
00659         except:
00660             self.failOnException("SpooledTemporaryFile")
00661 
00662         return file
00663 

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 664 of file test_tempfile.py.

00664 
00665     def test_basic(self):
00666         # SpooledTemporaryFile can create files
00667         f = self.do_create()
00668         self.assertFalse(f._rolled)
00669         f = self.do_create(max_size=100, pre="a", suf=".txt")
00670         self.assertFalse(f._rolled)

Here is the call graph for this function:

Definition at line 765 of file test_tempfile.py.

00765 
00766     def test_bound_methods(self):
00767         # It should be OK to steal a bound method from a SpooledTemporaryFile
00768         # and use it independently; when the file rolls over, those bound
00769         # methods should continue to function
00770         f = self.do_create(max_size=30)
00771         read = f.read
00772         write = f.write
00773         seek = f.seek
00774 
00775         write(b"a" * 35)
00776         write(b"b" * 35)
00777         seek(0, 0)
00778         self.assertEqual(read(70), b'a'*35 + b'b'*35)

Here is the call graph for this function:

Definition at line 835 of file test_tempfile.py.

00835 
00836     def test_context_manager_after_rollover(self):
00837         # A SpooledTemporaryFile can be used as a context manager
00838         f = tempfile.SpooledTemporaryFile(max_size=1)
00839         f.write(b'abc\n')
00840         f.flush()
00841         self.assertTrue(f._rolled)
00842         with f:
00843             self.assertFalse(f.closed)
00844         self.assertTrue(f.closed)
00845         def use_closed():
00846             with f:
00847                 pass
00848         self.assertRaises(ValueError, use_closed)
00849 
00850 
00851 test_classes.append(test_SpooledTemporaryFile)
00852 

Here is the call graph for this function:

Definition at line 810 of file test_tempfile.py.

00810 
00811     def test_context_manager_before_rollover(self):
00812         # A SpooledTemporaryFile can be used as a context manager
00813         with tempfile.SpooledTemporaryFile(max_size=1) as f:
00814             self.assertFalse(f._rolled)
00815             self.assertFalse(f.closed)
00816         self.assertTrue(f.closed)
00817         def use_closed():
00818             with f:
00819                 pass
00820         self.assertRaises(ValueError, use_closed)

Here is the call graph for this function:

Definition at line 821 of file test_tempfile.py.

00821 
00822     def test_context_manager_during_rollover(self):
00823         # A SpooledTemporaryFile can be used as a context manager
00824         with tempfile.SpooledTemporaryFile(max_size=1) as f:
00825             self.assertFalse(f._rolled)
00826             f.write(b'abc\n')
00827             f.flush()
00828             self.assertTrue(f._rolled)
00829             self.assertFalse(f.closed)
00830         self.assertTrue(f.closed)
00831         def use_closed():
00832             with f:
00833                 pass
00834         self.assertRaises(ValueError, use_closed)

Here is the call graph for this function:

Definition at line 671 of file test_tempfile.py.

00671 
00672     def test_del_on_close(self):
00673         # A SpooledTemporaryFile is deleted when closed
00674         dir = tempfile.mkdtemp()
00675         try:
00676             f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir)
00677             self.assertFalse(f._rolled)
00678             f.write(b'blat ' * 5)
00679             self.assertTrue(f._rolled)
00680             filename = f.name
00681             f.close()
00682             self.assertFalse(isinstance(filename, str) and os.path.exists(filename),
00683                         "SpooledTemporaryFile %s exists after close" % filename)
00684         finally:
00685             os.rmdir(dir)

Here is the call graph for this function:

Definition at line 734 of file test_tempfile.py.

00734 
00735     def test_fileno(self):
00736         # A SpooledTemporaryFile should roll over to a real file on fileno()
00737         f = self.do_create(max_size=30)
00738         self.assertFalse(f._rolled)
00739         self.assertTrue(f.fileno() > 0)
00740         self.assertTrue(f._rolled)

Here is the call graph for this function:

Definition at line 753 of file test_tempfile.py.

00753 
00754     def test_multiple_close_after_rollover(self):
00755         # A SpooledTemporaryFile can be closed many times without error
00756         f = tempfile.SpooledTemporaryFile(max_size=1)
00757         f.write(b'abc\n')
00758         self.assertTrue(f._rolled)
00759         f.close()
00760         try:
00761             f.close()
00762             f.close()
00763         except:
00764             self.failOnException("close")

Here is the call graph for this function:

Definition at line 741 of file test_tempfile.py.

00741 
00742     def test_multiple_close_before_rollover(self):
00743         # A SpooledTemporaryFile can be closed many times without error
00744         f = tempfile.SpooledTemporaryFile()
00745         f.write(b'abc\n')
00746         self.assertFalse(f._rolled)
00747         f.close()
00748         try:
00749             f.close()
00750             f.close()
00751         except:
00752             self.failOnException("close")

Here is the call graph for this function:

Definition at line 686 of file test_tempfile.py.

00686 
00687     def test_rewrite_small(self):
00688         # A SpooledTemporaryFile can be written to multiple within the max_size
00689         f = self.do_create(max_size=30)
00690         self.assertFalse(f._rolled)
00691         for i in range(5):
00692             f.seek(0, 0)
00693             f.write(b'x' * 20)
00694         self.assertFalse(f._rolled)

Here is the call graph for this function:

Definition at line 724 of file test_tempfile.py.

00724 
00725     def test_sparse(self):
00726         # A SpooledTemporaryFile that is written late in the file will extend
00727         # when that occurs
00728         f = self.do_create(max_size=30)
00729         self.assertFalse(f._rolled)
00730         f.seek(100, 0)
00731         self.assertFalse(f._rolled)
00732         f.write(b'x')
00733         self.assertTrue(f._rolled)

Here is the call graph for this function:

Definition at line 779 of file test_tempfile.py.

00779 
00780     def test_text_mode(self):
00781         # Creating a SpooledTemporaryFile with a text mode should produce
00782         # a file object reading and writing (Unicode) text strings.
00783         f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10)
00784         f.write("abc\n")
00785         f.seek(0)
00786         self.assertEqual(f.read(), "abc\n")
00787         f.write("def\n")
00788         f.seek(0)
00789         self.assertEqual(f.read(), "abc\ndef\n")
00790         f.write("xyzzy\n")
00791         f.seek(0)
00792         self.assertEqual(f.read(), "abc\ndef\nxyzzy\n")
00793         # Check that Ctrl+Z doesn't truncate the file
00794         f.write("foo\x1abar\n")
00795         f.seek(0)
00796         self.assertEqual(f.read(), "abc\ndef\nxyzzy\nfoo\x1abar\n")

Here is the call graph for this function:

Definition at line 797 of file test_tempfile.py.

00797 
00798     def test_text_newline_and_encoding(self):
00799         f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10,
00800                                           newline='', encoding='utf-8')
00801         f.write("\u039B\r\n")
00802         f.seek(0)
00803         self.assertEqual(f.read(), "\u039B\r\n")
00804         self.assertFalse(f._rolled)
00805 
00806         f.write("\u039B" * 20 + "\r\n")
00807         f.seek(0)
00808         self.assertEqual(f.read(), "\u039B\r\n" + ("\u039B" * 20) + "\r\n")
00809         self.assertTrue(f._rolled)

Here is the call graph for this function:

Definition at line 695 of file test_tempfile.py.

00695 
00696     def test_write_sequential(self):
00697         # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
00698         # over afterward
00699         f = self.do_create(max_size=30)
00700         self.assertFalse(f._rolled)
00701         f.write(b'x' * 20)
00702         self.assertFalse(f._rolled)
00703         f.write(b'x' * 10)
00704         self.assertFalse(f._rolled)
00705         f.write(b'x')
00706         self.assertTrue(f._rolled)

Here is the call graph for this function:

Definition at line 707 of file test_tempfile.py.

00707 
00708     def test_writelines(self):
00709         # Verify writelines with a SpooledTemporaryFile
00710         f = self.do_create()
00711         f.writelines((b'x', b'y', b'z'))
00712         f.seek(0)
00713         buf = f.read()
00714         self.assertEqual(buf, b'xyz')

Here is the call graph for this function:

Definition at line 715 of file test_tempfile.py.

00715 
00716     def test_writelines_sequential(self):
00717         # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
00718         # over afterward
00719         f = self.do_create(max_size=35)
00720         f.writelines((b'x' * 20, b'x' * 10, b'x' * 5))
00721         self.assertFalse(f._rolled)
00722         f.write(b'x')
00723         self.assertTrue(f._rolled)

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: