home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_largefile.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  4.2 KB  |  148 lines

  1. #!python
  2.  
  3. #----------------------------------------------------------------------
  4. # test largefile support on system where this makes sense
  5. #
  6. #----------------------------------------------------------------------
  7.  
  8. import test_support
  9. import os, struct, stat, sys
  10.  
  11. try:
  12.     import signal
  13.     # The default handler for SIGXFSZ is to abort the process.
  14.     # By ignoring it, system calls exceeding the file size resource
  15.     # limit will raise IOError instead of crashing the interpreter.
  16.     oldhandler = signal.signal(signal.SIGXFSZ, signal.SIG_IGN)
  17. except (ImportError, AttributeError):
  18.     pass
  19.  
  20.  
  21. # create >2GB file (2GB = 2147483648 bytes)
  22. size = 2500000000L
  23. name = test_support.TESTFN
  24.  
  25.  
  26. # On Windows this test comsumes large resources; It takes a long time to build
  27. # the >2GB file and takes >2GB of disk space therefore the resource must be
  28. # enabled to run this test.  If not, nothing after this line stanza will be
  29. # executed.
  30. if sys.platform[:3] == 'win':
  31.     test_support.requires(
  32.         'largefile',
  33.         'test requires %s bytes and a long time to run' % str(size))
  34. else:
  35.     # Only run if the current filesystem supports large files.
  36.     # (Skip this test on Windows, since we now always support large files.)
  37.     f = open(test_support.TESTFN, 'wb')
  38.     try:
  39.         # 2**31 == 2147483648
  40.         f.seek(2147483649L)
  41.         # Seeking is not enough of a test: you must write and flush, too!
  42.         f.write("x")
  43.         f.flush()
  44.     except (IOError, OverflowError):
  45.         f.close()
  46.         os.unlink(test_support.TESTFN)
  47.         raise test_support.TestSkipped, \
  48.               "filesystem does not have largefile support"
  49.     else:
  50.         f.close()
  51.  
  52.  
  53. def expect(got_this, expect_this):
  54.     if test_support.verbose:
  55.         print '%r =?= %r ...' % (got_this, expect_this),
  56.     if got_this != expect_this:
  57.         if test_support.verbose:
  58.             print 'no'
  59.         raise test_support.TestFailed, 'got %r, but expected %r' %\
  60.               (got_this, expect_this)
  61.     else:
  62.         if test_support.verbose:
  63.             print 'yes'
  64.  
  65.  
  66. # test that each file function works as expected for a large (i.e. >2GB, do
  67. # we have to check >4GB) files
  68.  
  69. if test_support.verbose:
  70.     print 'create large file via seek (may be sparse file) ...'
  71. f = open(name, 'wb')
  72. f.write('z')
  73. f.seek(0)
  74. f.seek(size)
  75. f.write('a')
  76. f.flush()
  77. if test_support.verbose:
  78.     print 'check file size with os.fstat'
  79. expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
  80. f.close()
  81. if test_support.verbose:
  82.     print 'check file size with os.stat'
  83. expect(os.stat(name)[stat.ST_SIZE], size+1)
  84.  
  85. if test_support.verbose:
  86.     print 'play around with seek() and read() with the built largefile'
  87. f = open(name, 'rb')
  88. expect(f.tell(), 0)
  89. expect(f.read(1), 'z')
  90. expect(f.tell(), 1)
  91. f.seek(0)
  92. expect(f.tell(), 0)
  93. f.seek(0, 0)
  94. expect(f.tell(), 0)
  95. f.seek(42)
  96. expect(f.tell(), 42)
  97. f.seek(42, 0)
  98. expect(f.tell(), 42)
  99. f.seek(42, 1)
  100. expect(f.tell(), 84)
  101. f.seek(0, 1)
  102. expect(f.tell(), 84)
  103. f.seek(0, 2) # seek from the end
  104. expect(f.tell(), size + 1 + 0)
  105. f.seek(-10, 2)
  106. expect(f.tell(), size + 1 - 10)
  107. f.seek(-size-1, 2)
  108. expect(f.tell(), 0)
  109. f.seek(size)
  110. expect(f.tell(), size)
  111. expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
  112. f.seek(-size-1, 1)
  113. expect(f.read(1), 'z')
  114. expect(f.tell(), 1)
  115. f.close()
  116.  
  117. if test_support.verbose:
  118.     print 'play around with os.lseek() with the built largefile'
  119. f = open(name, 'rb')
  120. expect(os.lseek(f.fileno(), 0, 0), 0)
  121. expect(os.lseek(f.fileno(), 42, 0), 42)
  122. expect(os.lseek(f.fileno(), 42, 1), 84)
  123. expect(os.lseek(f.fileno(), 0, 1), 84)
  124. expect(os.lseek(f.fileno(), 0, 2), size+1+0)
  125. expect(os.lseek(f.fileno(), -10, 2), size+1-10)
  126. expect(os.lseek(f.fileno(), -size-1, 2), 0)
  127. expect(os.lseek(f.fileno(), size, 0), size)
  128. expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
  129. f.close()
  130.  
  131.  
  132. # XXX add tests for truncate if it exists
  133. # XXX has truncate ever worked on Windows? specifically on WinNT I get:
  134. #     "IOError: [Errno 13] Permission denied"
  135. ##try:
  136. ##      newsize = size - 10
  137. ##      f.seek(newsize)
  138. ##      f.truncate()
  139. ##      expect(f.tell(), newsize)
  140. ##      newsize = newsize - 1
  141. ##      f.seek(0)
  142. ##      f.truncate(newsize)
  143. ##      expect(f.tell(), newsize)
  144. ##except AttributeError:
  145. ##      pass
  146.  
  147. os.unlink(name)
  148.