Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_largefile.py

    r2 r391  
    11"""Test largefile support on system where this makes sense.
    22"""
     3
     4from __future__ import print_function
    35
    46import os
     
    79import unittest
    810from test.test_support import run_unittest, TESTFN, verbose, requires, \
    9                               TestSkipped, unlink
     11                              unlink
     12import io  # C implementation of io
     13import _pyio as pyio # Python implementation of io
    1014
    1115try:
     
    1923
    2024# create >2GB file (2GB = 2147483648 bytes)
    21 size = 2500000000L
    22 
    23 
    24 class TestCase(unittest.TestCase):
     25size = 2500000000
     26
     27
     28class LargeFileTest(unittest.TestCase):
    2529    """Test that each file function works as expected for a large
    2630    (i.e. > 2GB, do  we have to check > 4GB) files.
     
    3438    def test_seek(self):
    3539        if verbose:
    36             print 'create large file via seek (may be sparse file) ...'
    37         with open(TESTFN, 'wb') as f:
    38             f.write('z')
     40            print('create large file via seek (may be sparse file) ...')
     41        with self.open(TESTFN, 'wb') as f:
     42            f.write(b'z')
    3943            f.seek(0)
    4044            f.seek(size)
    41             f.write('a')
     45            f.write(b'a')
    4246            f.flush()
    4347            if verbose:
    44                 print 'check file size with os.fstat'
     48                print('check file size with os.fstat')
    4549            self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
    4650
    4751    def test_osstat(self):
    4852        if verbose:
    49             print 'check file size with os.stat'
     53            print('check file size with os.stat')
    5054        self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1)
    5155
    5256    def test_seek_read(self):
    5357        if verbose:
    54             print 'play around with seek() and read() with the built largefile'
    55         with open(TESTFN, 'rb') as f:
    56             self.assertEqual(f.tell(), 0)
    57             self.assertEqual(f.read(1), 'z')
     58            print('play around with seek() and read() with the built largefile')
     59        with self.open(TESTFN, 'rb') as f:
     60            self.assertEqual(f.tell(), 0)
     61            self.assertEqual(f.read(1), b'z')
    5862            self.assertEqual(f.tell(), 1)
    5963            f.seek(0)
     
    7882            self.assertEqual(f.tell(), size)
    7983            # the 'a' that was written at the end of file above
    80             self.assertEqual(f.read(1), 'a')
     84            self.assertEqual(f.read(1), b'a')
    8185            f.seek(-size-1, 1)
    82             self.assertEqual(f.read(1), 'z')
     86            self.assertEqual(f.read(1), b'z')
    8387            self.assertEqual(f.tell(), 1)
    8488
    8589    def test_lseek(self):
    8690        if verbose:
    87             print 'play around with os.lseek() with the built largefile'
    88         with open(TESTFN, 'rb') as f:
     91            print('play around with os.lseek() with the built largefile')
     92        with self.open(TESTFN, 'rb') as f:
    8993            self.assertEqual(os.lseek(f.fileno(), 0, 0), 0)
    9094            self.assertEqual(os.lseek(f.fileno(), 42, 0), 42)
     
    96100            self.assertEqual(os.lseek(f.fileno(), size, 0), size)
    97101            # the 'a' that was written at the end of file above
    98             self.assertEqual(f.read(1), 'a')
     102            self.assertEqual(f.read(1), b'a')
    99103
    100104    def test_truncate(self):
    101105        if verbose:
    102             print 'try truncate'
    103         with open(TESTFN, 'r+b') as f:
     106            print('try truncate')
     107        with self.open(TESTFN, 'r+b') as f:
    104108            # this is already decided before start running the test suite
    105109            # but we do it anyway for extra protection
    106110            if not hasattr(f, 'truncate'):
    107                 raise TestSkipped, "open().truncate() not available on this system"
     111                raise unittest.SkipTest("open().truncate() not available on this system")
    108112            f.seek(0, 2)
    109113            # else we've lost track of the true size
     
    121125            f.seek(42)
    122126            f.truncate(newsize)
    123             self.assertEqual(f.tell(), 42)       # else pointer moved
     127            if self.new_io:
     128                self.assertEqual(f.tell(), 42)
    124129            f.seek(0, 2)
    125             self.assertEqual(f.tell(), newsize)  # else wasn't truncated
    126 
     130            self.assertEqual(f.tell(), newsize)
    127131            # XXX truncate(larger than true size) is ill-defined
    128132            # across platform; cut it waaaaay back
    129133            f.seek(0)
    130134            f.truncate(1)
    131             self.assertEqual(f.tell(), 0)       # else pointer moved
     135            if self.new_io:
     136                self.assertEqual(f.tell(), 0)       # else pointer moved
     137            f.seek(0)
    132138            self.assertEqual(len(f.read()), 1)  # else wasn't truncated
     139
     140    def test_seekable(self):
     141        # Issue #5016; seekable() can return False when the current position
     142        # is negative when truncated to an int.
     143        if not self.new_io:
     144            self.skipTest("builtin file doesn't have seekable()")
     145        for pos in (2**31-1, 2**31, 2**31+1):
     146            with self.open(TESTFN, 'rb') as f:
     147                f.seek(pos)
     148                self.assertTrue(f.seekable())
    133149
    134150
     
    145161        # (Skip this test on Windows, since we now always support
    146162        # large files.)
    147         f = open(TESTFN, 'wb')
     163        f = open(TESTFN, 'wb', buffering=0)
    148164        try:
    149165            # 2**31 == 2147483648
    150             f.seek(2147483649L)
     166            f.seek(2147483649)
    151167            # Seeking is not enough of a test: you must write and
    152168            # flush, too!
    153             f.write("x")
     169            f.write(b'x')
    154170            f.flush()
    155171        except (IOError, OverflowError):
    156172            f.close()
    157173            unlink(TESTFN)
    158             raise TestSkipped, "filesystem does not have largefile support"
     174            raise unittest.SkipTest("filesystem does not have largefile support")
    159175        else:
    160176            f.close()
    161177    suite = unittest.TestSuite()
    162     suite.addTest(TestCase('test_seek'))
    163     suite.addTest(TestCase('test_osstat'))
    164     suite.addTest(TestCase('test_seek_read'))
    165     suite.addTest(TestCase('test_lseek'))
    166     with open(TESTFN, 'w') as f:
    167         if hasattr(f, 'truncate'):
    168             suite.addTest(TestCase('test_truncate'))
    169     unlink(TESTFN)
     178    for _open, prefix in [(io.open, 'C'), (pyio.open, 'Py'),
     179                          (open, 'Builtin')]:
     180        class TestCase(LargeFileTest):
     181            pass
     182        TestCase.open = staticmethod(_open)
     183        TestCase.new_io = _open is not open
     184        TestCase.__name__ = prefix + LargeFileTest.__name__
     185        suite.addTest(TestCase('test_seek'))
     186        suite.addTest(TestCase('test_osstat'))
     187        suite.addTest(TestCase('test_seek_read'))
     188        suite.addTest(TestCase('test_lseek'))
     189        with _open(TESTFN, 'wb') as f:
     190            if hasattr(f, 'truncate'):
     191                suite.addTest(TestCase('test_truncate'))
     192        suite.addTest(TestCase('test_seekable'))
     193        unlink(TESTFN)
    170194    try:
    171195        run_unittest(suite)
     
    173197        unlink(TESTFN)
    174198
    175 
    176199if __name__ == '__main__':
    177200    test_main()
Note: See TracChangeset for help on using the changeset viewer.