Back to index

python3.2  3.2.2
Public Member Functions
test.test_fileio.OtherFileTests Class Reference

List of all members.

Public Member Functions

def testAbles
def testModeStrings
def testUnicodeOpen
def testBytesOpen
def testInvalidFd
def testBadModeArgument
def testTruncate
def testTruncateOnWindows
def testAppend
def testInvalidInit
def testWarnings

Detailed Description

Definition at line 231 of file test_fileio.py.


Member Function Documentation

Definition at line 233 of file test_fileio.py.

00233 
00234     def testAbles(self):
00235         try:
00236             f = _FileIO(TESTFN, "w")
00237             self.assertEqual(f.readable(), False)
00238             self.assertEqual(f.writable(), True)
00239             self.assertEqual(f.seekable(), True)
00240             f.close()
00241 
00242             f = _FileIO(TESTFN, "r")
00243             self.assertEqual(f.readable(), True)
00244             self.assertEqual(f.writable(), False)
00245             self.assertEqual(f.seekable(), True)
00246             f.close()
00247 
00248             f = _FileIO(TESTFN, "a+")
00249             self.assertEqual(f.readable(), True)
00250             self.assertEqual(f.writable(), True)
00251             self.assertEqual(f.seekable(), True)
00252             self.assertEqual(f.isatty(), False)
00253             f.close()
00254 
00255             if sys.platform != "win32":
00256                 try:
00257                     f = _FileIO("/dev/tty", "a")
00258                 except EnvironmentError:
00259                     # When run in a cron job there just aren't any
00260                     # ttys, so skip the test.  This also handles other
00261                     # OS'es that don't support /dev/tty.
00262                     pass
00263                 else:
00264                     self.assertEqual(f.readable(), False)
00265                     self.assertEqual(f.writable(), True)
00266                     if sys.platform != "darwin" and \
00267                        'bsd' not in sys.platform and \
00268                        not sys.platform.startswith('sunos'):
00269                         # Somehow /dev/tty appears seekable on some BSDs
00270                         self.assertEqual(f.seekable(), False)
00271                     self.assertEqual(f.isatty(), True)
00272                     f.close()
00273         finally:
00274             os.unlink(TESTFN)

Here is the call graph for this function:

Definition at line 372 of file test_fileio.py.

00372 
00373     def testAppend(self):
00374         try:
00375             f = open(TESTFN, 'wb')
00376             f.write(b'spam')
00377             f.close()
00378             f = open(TESTFN, 'ab')
00379             f.write(b'eggs')
00380             f.close()
00381             f = open(TESTFN, 'rb')
00382             d = f.read()
00383             f.close()
00384             self.assertEqual(d, b'spameggs')
00385         finally:
00386             try:
00387                 os.unlink(TESTFN)
00388             except:
00389                 pass

Here is the call graph for this function:

Definition at line 315 of file test_fileio.py.

00315 
00316     def testBadModeArgument(self):
00317         # verify that we get a sensible error message for bad mode argument
00318         bad_mode = "qwerty"
00319         try:
00320             f = _FileIO(TESTFN, bad_mode)
00321         except ValueError as msg:
00322             if msg.args[0] != 0:
00323                 s = str(msg)
00324                 if TESTFN in s or bad_mode not in s:
00325                     self.fail("bad error message for invalid mode: %s" % s)
00326             # if msg.args[0] == 0, we're probably on Windows where there may be
00327             # no obvious way to discover why open() failed.
00328         else:
00329             f.close()
00330             self.fail("no error for invalid mode: %s" % bad_mode)

Here is the call graph for this function:

Definition at line 292 of file test_fileio.py.

00292 
00293     def testBytesOpen(self):
00294         # Opening a bytes filename
00295         try:
00296             fn = TESTFN.encode("ascii")
00297         except UnicodeEncodeError:
00298             # Skip test
00299             return
00300         f = _FileIO(fn, "w")
00301         try:
00302             f.write(b"abc")
00303             f.close()
00304             with open(TESTFN, "rb") as f:
00305                 self.assertEqual(f.read(), b"abc")
00306         finally:
00307             os.unlink(TESTFN)

Here is the call graph for this function:

Definition at line 308 of file test_fileio.py.

00308 
00309     def testInvalidFd(self):
00310         self.assertRaises(ValueError, _FileIO, -10)
00311         self.assertRaises(OSError, _FileIO, make_bad_fd())
00312         if sys.platform == 'win32':
00313             import msvcrt
00314             self.assertRaises(IOError, msvcrt.get_osfhandle, make_bad_fd())

Here is the call graph for this function:

Definition at line 390 of file test_fileio.py.

00390 
00391     def testInvalidInit(self):
00392         self.assertRaises(TypeError, _FileIO, "1", 0, 0)

Here is the call graph for this function:

Definition at line 275 of file test_fileio.py.

00275 
00276     def testModeStrings(self):
00277         # check invalid mode strings
00278         for mode in ("", "aU", "wU+", "rw", "rt"):
00279             try:
00280                 f = _FileIO(TESTFN, mode)
00281             except ValueError:
00282                 pass
00283             else:
00284                 f.close()
00285                 self.fail('%r is an invalid file mode' % mode)

Here is the call graph for this function:

Definition at line 331 of file test_fileio.py.

00331 
00332     def testTruncate(self):
00333         f = _FileIO(TESTFN, 'w')
00334         f.write(bytes(bytearray(range(10))))
00335         self.assertEqual(f.tell(), 10)
00336         f.truncate(5)
00337         self.assertEqual(f.tell(), 10)
00338         self.assertEqual(f.seek(0, os.SEEK_END), 5)
00339         f.truncate(15)
00340         self.assertEqual(f.tell(), 5)
00341         self.assertEqual(f.seek(0, os.SEEK_END), 15)
00342         f.close()

Here is the call graph for this function:

Definition at line 343 of file test_fileio.py.

00343 
00344     def testTruncateOnWindows(self):
00345         def bug801631():
00346             # SF bug <http://www.python.org/sf/801631>
00347             # "file.truncate fault on windows"
00348             f = _FileIO(TESTFN, 'w')
00349             f.write(bytes(range(11)))
00350             f.close()
00351 
00352             f = _FileIO(TESTFN,'r+')
00353             data = f.read(5)
00354             if data != bytes(range(5)):
00355                 self.fail("Read on file opened for update failed %r" % data)
00356             if f.tell() != 5:
00357                 self.fail("File pos after read wrong %d" % f.tell())
00358 
00359             f.truncate()
00360             if f.tell() != 5:
00361                 self.fail("File pos after ftruncate wrong %d" % f.tell())
00362 
00363             f.close()
00364             size = os.path.getsize(TESTFN)
00365             if size != 5:
00366                 self.fail("File size after ftruncate wrong %d" % size)
00367 
00368         try:
00369             bug801631()
00370         finally:
00371             os.unlink(TESTFN)

Here is the call graph for this function:

Definition at line 286 of file test_fileio.py.

00286 
00287     def testUnicodeOpen(self):
00288         # verify repr works for unicode too
00289         f = _FileIO(str(TESTFN), "w")
00290         f.close()
00291         os.unlink(TESTFN)

Definition at line 393 of file test_fileio.py.

00393 
00394     def testWarnings(self):
00395         with check_warnings(quiet=True) as w:
00396             self.assertEqual(w.warnings, [])
00397             self.assertRaises(TypeError, _FileIO, [])
00398             self.assertEqual(w.warnings, [])
00399             self.assertRaises(ValueError, _FileIO, "/some/invalid/name", "rt")
00400             self.assertEqual(w.warnings, [])
00401 

Here is the call graph for this function:


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