2000-08-12 03:02:59 +08:00
|
|
|
#!python
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
# test largefile support on system where this makes sense
|
|
|
|
#
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2002-07-24 03:04:11 +08:00
|
|
|
from test import test_support
|
2000-08-12 03:02:59 +08:00
|
|
|
import os, struct, stat, sys
|
|
|
|
|
2001-12-12 01:57:26 +08:00
|
|
|
try:
|
|
|
|
import signal
|
|
|
|
# The default handler for SIGXFSZ is to abort the process.
|
|
|
|
# By ignoring it, system calls exceeding the file size resource
|
|
|
|
# limit will raise IOError instead of crashing the interpreter.
|
|
|
|
oldhandler = signal.signal(signal.SIGXFSZ, signal.SIG_IGN)
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
pass
|
|
|
|
|
2000-08-12 03:02:59 +08:00
|
|
|
|
2001-09-10 23:03:18 +08:00
|
|
|
# create >2GB file (2GB = 2147483648 bytes)
|
2007-01-16 00:59:06 +08:00
|
|
|
size = 2500000000
|
2001-09-10 23:03:18 +08:00
|
|
|
name = test_support.TESTFN
|
|
|
|
|
|
|
|
|
2002-12-02 18:42:30 +08:00
|
|
|
# On Windows and Mac OSX this test comsumes large resources; It takes
|
|
|
|
# a long time to build the >2GB file and takes >2GB of disk space
|
|
|
|
# therefore the resource must be enabled to run this test. If not,
|
|
|
|
# nothing after this line stanza will be executed.
|
|
|
|
if sys.platform[:3] == 'win' or sys.platform == 'darwin':
|
2001-08-21 06:37:34 +08:00
|
|
|
test_support.requires(
|
|
|
|
'largefile',
|
|
|
|
'test requires %s bytes and a long time to run' % str(size))
|
2001-09-10 21:34:12 +08:00
|
|
|
else:
|
|
|
|
# Only run if the current filesystem supports large files.
|
|
|
|
# (Skip this test on Windows, since we now always support large files.)
|
|
|
|
f = open(test_support.TESTFN, 'wb')
|
|
|
|
try:
|
|
|
|
# 2**31 == 2147483648
|
2007-01-16 00:59:06 +08:00
|
|
|
f.seek(2147483649)
|
2001-09-10 21:34:12 +08:00
|
|
|
# Seeking is not enough of a test: you must write and flush, too!
|
2007-08-28 07:09:25 +08:00
|
|
|
f.write(b"x")
|
2001-09-10 21:34:12 +08:00
|
|
|
f.flush()
|
|
|
|
except (IOError, OverflowError):
|
|
|
|
f.close()
|
|
|
|
os.unlink(test_support.TESTFN)
|
2007-08-30 07:37:32 +08:00
|
|
|
raise test_support.TestSkipped(
|
|
|
|
"filesystem does not have largefile support")
|
2001-09-10 21:34:12 +08:00
|
|
|
else:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2000-08-12 03:02:59 +08:00
|
|
|
def expect(got_this, expect_this):
|
2000-10-24 00:38:20 +08:00
|
|
|
if test_support.verbose:
|
2007-02-09 13:37:30 +08:00
|
|
|
print('%r =?= %r ...' % (got_this, expect_this), end=' ')
|
2000-10-24 00:38:20 +08:00
|
|
|
if got_this != expect_this:
|
|
|
|
if test_support.verbose:
|
2007-02-09 13:37:30 +08:00
|
|
|
print('no')
|
2007-08-30 07:37:32 +08:00
|
|
|
raise test_support.TestFailed('got %r, but expected %r'
|
|
|
|
% (got_this, expect_this))
|
2000-10-24 00:38:20 +08:00
|
|
|
else:
|
|
|
|
if test_support.verbose:
|
2007-02-09 13:37:30 +08:00
|
|
|
print('yes')
|
2000-08-12 03:02:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
# test that each file function works as expected for a large (i.e. >2GB, do
|
|
|
|
# we have to check >4GB) files
|
|
|
|
|
|
|
|
if test_support.verbose:
|
2007-02-09 13:37:30 +08:00
|
|
|
print('create large file via seek (may be sparse file) ...')
|
2001-09-06 08:32:15 +08:00
|
|
|
f = open(name, 'wb')
|
2003-08-26 19:59:27 +08:00
|
|
|
try:
|
2007-08-28 07:09:25 +08:00
|
|
|
f.write(b'z')
|
2003-08-26 19:59:27 +08:00
|
|
|
f.seek(0)
|
|
|
|
f.seek(size)
|
2007-08-28 07:09:25 +08:00
|
|
|
f.write(b'a')
|
2003-08-26 19:59:27 +08:00
|
|
|
f.flush()
|
|
|
|
if test_support.verbose:
|
2007-02-09 13:37:30 +08:00
|
|
|
print('check file size with os.fstat')
|
2003-08-26 19:59:27 +08:00
|
|
|
expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
|
|
|
|
finally:
|
|
|
|
f.close()
|
2000-08-12 03:02:59 +08:00
|
|
|
if test_support.verbose:
|
2007-02-09 13:37:30 +08:00
|
|
|
print('check file size with os.stat')
|
2000-08-12 03:02:59 +08:00
|
|
|
expect(os.stat(name)[stat.ST_SIZE], size+1)
|
|
|
|
|
|
|
|
if test_support.verbose:
|
2007-02-09 13:37:30 +08:00
|
|
|
print('play around with seek() and read() with the built largefile')
|
2001-09-06 08:32:15 +08:00
|
|
|
f = open(name, 'rb')
|
2003-08-26 19:59:27 +08:00
|
|
|
try:
|
|
|
|
expect(f.tell(), 0)
|
2007-07-23 08:51:28 +08:00
|
|
|
expect(f.read(1), b'z')
|
2003-08-26 19:59:27 +08:00
|
|
|
expect(f.tell(), 1)
|
|
|
|
f.seek(0)
|
|
|
|
expect(f.tell(), 0)
|
|
|
|
f.seek(0, 0)
|
|
|
|
expect(f.tell(), 0)
|
|
|
|
f.seek(42)
|
|
|
|
expect(f.tell(), 42)
|
|
|
|
f.seek(42, 0)
|
|
|
|
expect(f.tell(), 42)
|
|
|
|
f.seek(42, 1)
|
|
|
|
expect(f.tell(), 84)
|
|
|
|
f.seek(0, 1)
|
|
|
|
expect(f.tell(), 84)
|
|
|
|
f.seek(0, 2) # seek from the end
|
|
|
|
expect(f.tell(), size + 1 + 0)
|
|
|
|
f.seek(-10, 2)
|
|
|
|
expect(f.tell(), size + 1 - 10)
|
|
|
|
f.seek(-size-1, 2)
|
|
|
|
expect(f.tell(), 0)
|
|
|
|
f.seek(size)
|
|
|
|
expect(f.tell(), size)
|
2007-07-23 08:51:28 +08:00
|
|
|
expect(f.read(1), b'a') # the 'a' that was written at the end of file above
|
2003-08-26 19:59:27 +08:00
|
|
|
f.seek(-size-1, 1)
|
2007-07-23 08:51:28 +08:00
|
|
|
expect(f.read(1), b'z')
|
2003-08-26 19:59:27 +08:00
|
|
|
expect(f.tell(), 1)
|
|
|
|
finally:
|
|
|
|
f.close()
|
2000-08-12 03:02:59 +08:00
|
|
|
|
|
|
|
if test_support.verbose:
|
2007-02-09 13:37:30 +08:00
|
|
|
print('play around with os.lseek() with the built largefile')
|
2001-09-06 09:17:45 +08:00
|
|
|
f = open(name, 'rb')
|
2003-08-26 19:59:27 +08:00
|
|
|
try:
|
|
|
|
expect(os.lseek(f.fileno(), 0, 0), 0)
|
|
|
|
expect(os.lseek(f.fileno(), 42, 0), 42)
|
|
|
|
expect(os.lseek(f.fileno(), 42, 1), 84)
|
|
|
|
expect(os.lseek(f.fileno(), 0, 1), 84)
|
|
|
|
expect(os.lseek(f.fileno(), 0, 2), size+1+0)
|
|
|
|
expect(os.lseek(f.fileno(), -10, 2), size+1-10)
|
|
|
|
expect(os.lseek(f.fileno(), -size-1, 2), 0)
|
|
|
|
expect(os.lseek(f.fileno(), size, 0), size)
|
2007-07-23 08:51:28 +08:00
|
|
|
expect(f.read(1), b'a') # the 'a' that was written at the end of file above
|
2003-08-26 19:59:27 +08:00
|
|
|
finally:
|
|
|
|
f.close()
|
2000-08-12 03:02:59 +08:00
|
|
|
|
2002-03-11 08:24:00 +08:00
|
|
|
if hasattr(f, 'truncate'):
|
|
|
|
if test_support.verbose:
|
2007-02-09 13:37:30 +08:00
|
|
|
print('try truncate')
|
2002-03-11 08:24:00 +08:00
|
|
|
f = open(name, 'r+b')
|
2003-08-26 19:59:27 +08:00
|
|
|
try:
|
|
|
|
f.seek(0, 2)
|
|
|
|
expect(f.tell(), size+1) # else we've lost track of the true size
|
|
|
|
# Cut it back via seek + truncate with no argument.
|
|
|
|
newsize = size - 10
|
|
|
|
f.seek(newsize)
|
|
|
|
f.truncate()
|
|
|
|
expect(f.tell(), newsize) # else pointer moved
|
|
|
|
f.seek(0, 2)
|
|
|
|
expect(f.tell(), newsize) # else wasn't truncated
|
|
|
|
# Ensure that truncate(smaller than true size) shrinks the file.
|
|
|
|
newsize -= 1
|
|
|
|
f.seek(42)
|
|
|
|
f.truncate(newsize)
|
|
|
|
expect(f.tell(), 42) # else pointer moved
|
|
|
|
f.seek(0, 2)
|
|
|
|
expect(f.tell(), newsize) # else wasn't truncated
|
|
|
|
|
|
|
|
# XXX truncate(larger than true size) is ill-defined across platforms
|
|
|
|
|
|
|
|
# cut it waaaaay back
|
|
|
|
f.seek(0)
|
|
|
|
f.truncate(1)
|
|
|
|
expect(f.tell(), 0) # else pointer moved
|
|
|
|
expect(len(f.read()), 1) # else wasn't truncated
|
|
|
|
|
|
|
|
finally:
|
|
|
|
f.close()
|
2000-08-12 03:02:59 +08:00
|
|
|
|
|
|
|
os.unlink(name)
|