Back to index

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

List of all members.

Public Member Functions

def testModeStrings
def testStdin
def testBadModeArgument
def testSetBufferSize
def testTruncateOnWindows
def testIteration

Static Public Attributes

tuple open = staticmethod(pyio.open)

Detailed Description

Definition at line 328 of file test_file.py.


Member Function Documentation

Definition at line 161 of file test_file.py.

00161 
00162     def testBadModeArgument(self):
00163         # verify that we get a sensible error message for bad mode argument
00164         bad_mode = "qwerty"
00165         try:
00166             f = self.open(TESTFN, bad_mode)
00167         except ValueError as msg:
00168             if msg.args[0] != 0:
00169                 s = str(msg)
00170                 if TESTFN in s or bad_mode not in s:
00171                     self.fail("bad error message for invalid mode: %s" % s)
00172             # if msg.args[0] == 0, we're probably on Windows where there may be
00173             # no obvious way to discover why open() failed.
00174         else:
00175             f.close()
00176             self.fail("no error for invalid mode: %s" % bad_mode)

Here is the call graph for this function:

def test.test_file.OtherFileTests.testIteration (   self) [inherited]

Definition at line 224 of file test_file.py.

00224 
00225     def testIteration(self):
00226         # Test the complex interaction when mixing file-iteration and the
00227         # various read* methods.
00228         dataoffset = 16384
00229         filler = b"ham\n"
00230         assert not dataoffset % len(filler), \
00231             "dataoffset must be multiple of len(filler)"
00232         nchunks = dataoffset // len(filler)
00233         testlines = [
00234             b"spam, spam and eggs\n",
00235             b"eggs, spam, ham and spam\n",
00236             b"saussages, spam, spam and eggs\n",
00237             b"spam, ham, spam and eggs\n",
00238             b"spam, spam, spam, spam, spam, ham, spam\n",
00239             b"wonderful spaaaaaam.\n"
00240         ]
00241         methods = [("readline", ()), ("read", ()), ("readlines", ()),
00242                    ("readinto", (array("b", b" "*100),))]
00243 
00244         try:
00245             # Prepare the testfile
00246             bag = self.open(TESTFN, "wb")
00247             bag.write(filler * nchunks)
00248             bag.writelines(testlines)
00249             bag.close()
00250             # Test for appropriate errors mixing read* and iteration
00251             for methodname, args in methods:
00252                 f = self.open(TESTFN, 'rb')
00253                 if next(f) != filler:
00254                     self.fail, "Broken testfile"
00255                 meth = getattr(f, methodname)
00256                 meth(*args)  # This simply shouldn't fail
00257                 f.close()
00258 
00259             # Test to see if harmless (by accident) mixing of read* and
00260             # iteration still works. This depends on the size of the internal
00261             # iteration buffer (currently 8192,) but we can test it in a
00262             # flexible manner.  Each line in the bag o' ham is 4 bytes
00263             # ("h", "a", "m", "\n"), so 4096 lines of that should get us
00264             # exactly on the buffer boundary for any power-of-2 buffersize
00265             # between 4 and 16384 (inclusive).
00266             f = self.open(TESTFN, 'rb')
00267             for i in range(nchunks):
00268                 next(f)
00269             testline = testlines.pop(0)
00270             try:
00271                 line = f.readline()
00272             except ValueError:
00273                 self.fail("readline() after next() with supposedly empty "
00274                           "iteration-buffer failed anyway")
00275             if line != testline:
00276                 self.fail("readline() after next() with empty buffer "
00277                           "failed. Got %r, expected %r" % (line, testline))
00278             testline = testlines.pop(0)
00279             buf = array("b", b"\x00" * len(testline))
00280             try:
00281                 f.readinto(buf)
00282             except ValueError:
00283                 self.fail("readinto() after next() with supposedly empty "
00284                           "iteration-buffer failed anyway")
00285             line = buf.tobytes()
00286             if line != testline:
00287                 self.fail("readinto() after next() with empty buffer "
00288                           "failed. Got %r, expected %r" % (line, testline))
00289 
00290             testline = testlines.pop(0)
00291             try:
00292                 line = f.read(len(testline))
00293             except ValueError:
00294                 self.fail("read() after next() with supposedly empty "
00295                           "iteration-buffer failed anyway")
00296             if line != testline:
00297                 self.fail("read() after next() with empty buffer "
00298                           "failed. Got %r, expected %r" % (line, testline))
00299             try:
00300                 lines = f.readlines()
00301             except ValueError:
00302                 self.fail("readlines() after next() with supposedly empty "
00303                           "iteration-buffer failed anyway")
00304             if lines != testlines:
00305                 self.fail("readlines() after next() with empty buffer "
00306                           "failed. Got %r, expected %r" % (line, testline))
00307             f.close()
00308 
00309             # Reading after iteration hit EOF shouldn't hurt either
00310             f = self.open(TESTFN, 'rb')
00311             try:
00312                 for line in f:
00313                     pass
00314                 try:
00315                     f.readline()
00316                     f.readinto(buf)
00317                     f.read()
00318                     f.readlines()
00319                 except ValueError:
00320                     self.fail("read* failed after next() consumed file")
00321             finally:
00322                 f.close()
00323         finally:
00324             os.unlink(TESTFN)

Here is the call graph for this function:

Definition at line 140 of file test_file.py.

00140 
00141     def testModeStrings(self):
00142         # check invalid mode strings
00143         for mode in ("", "aU", "wU+"):
00144             try:
00145                 f = self.open(TESTFN, mode)
00146             except ValueError:
00147                 pass
00148             else:
00149                 f.close()
00150                 self.fail('%r is an invalid file mode' % mode)

Here is the call graph for this function:

Definition at line 177 of file test_file.py.

00177 
00178     def testSetBufferSize(self):
00179         # make sure that explicitly setting the buffer size doesn't cause
00180         # misbehaviour especially with repeated close() calls
00181         for s in (-1, 0, 1, 512):
00182             try:
00183                 f = self.open(TESTFN, 'wb', s)
00184                 f.write(str(s).encode("ascii"))
00185                 f.close()
00186                 f.close()
00187                 f = self.open(TESTFN, 'rb', s)
00188                 d = int(f.read().decode("ascii"))
00189                 f.close()
00190                 f.close()
00191             except IOError as msg:
00192                 self.fail('error setting buffer size %d: %s' % (s, str(msg)))
00193             self.assertEqual(d, s)

Here is the call graph for this function:

def test.test_file.OtherFileTests.testStdin (   self) [inherited]

Definition at line 151 of file test_file.py.

00151 
00152     def testStdin(self):
00153         # This causes the interpreter to exit on OSF1 v5.1.
00154         if sys.platform != 'osf1V5':
00155             self.assertRaises((IOError, ValueError), sys.stdin.seek, -1)
00156         else:
00157             print((
00158                 '  Skipping sys.stdin.seek(-1), it may crash the interpreter.'
00159                 ' Test manually.'), file=sys.__stdout__)
00160         self.assertRaises((IOError, ValueError), sys.stdin.truncate)

Here is the call graph for this function:

Definition at line 194 of file test_file.py.

00194 
00195     def testTruncateOnWindows(self):
00196         # SF bug <http://www.python.org/sf/801631>
00197         # "file.truncate fault on windows"
00198 
00199         os.unlink(TESTFN)
00200         f = self.open(TESTFN, 'wb')
00201 
00202         try:
00203             f.write(b'12345678901')   # 11 bytes
00204             f.close()
00205 
00206             f = self.open(TESTFN,'rb+')
00207             data = f.read(5)
00208             if data != b'12345':
00209                 self.fail("Read on file opened for update failed %r" % data)
00210             if f.tell() != 5:
00211                 self.fail("File pos after read wrong %d" % f.tell())
00212 
00213             f.truncate()
00214             if f.tell() != 5:
00215                 self.fail("File pos after ftruncate wrong %d" % f.tell())
00216 
00217             f.close()
00218             size = os.path.getsize(TESTFN)
00219             if size != 5:
00220                 self.fail("File size after ftruncate wrong %d" % size)
00221         finally:
00222             f.close()
00223             os.unlink(TESTFN)

Here is the call graph for this function:


Member Data Documentation

Definition at line 329 of file test_file.py.


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