2001-07-17 17:52:51 -03:00
|
|
|
# As a test suite for the os module, this is woefully inadequate, but this
|
|
|
|
# does add tests for a few functions which have been determined to be more
|
|
|
|
# more portable than they had been thought to be.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import unittest
|
2001-08-20 17:10:01 -03:00
|
|
|
import warnings
|
2001-07-17 17:52:51 -03:00
|
|
|
|
2001-08-22 16:24:42 -03:00
|
|
|
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__)
|
|
|
|
warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__)
|
|
|
|
|
2002-08-22 16:40:33 -03:00
|
|
|
from test.test_support import TESTFN, run_suite
|
2001-07-17 17:52:51 -03:00
|
|
|
|
|
|
|
class TemporaryFileTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.files = []
|
|
|
|
os.mkdir(TESTFN)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
for name in self.files:
|
|
|
|
os.unlink(name)
|
|
|
|
os.rmdir(TESTFN)
|
|
|
|
|
|
|
|
def check_tempfile(self, name):
|
|
|
|
# make sure it doesn't already exist:
|
|
|
|
self.failIf(os.path.exists(name),
|
|
|
|
"file already exists for temporary file")
|
|
|
|
# make sure we can create the file
|
|
|
|
open(name, "w")
|
|
|
|
self.files.append(name)
|
|
|
|
|
|
|
|
def test_tempnam(self):
|
|
|
|
if not hasattr(os, "tempnam"):
|
|
|
|
return
|
2001-08-20 17:10:01 -03:00
|
|
|
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
|
2002-04-15 22:27:44 -03:00
|
|
|
r"test_os$")
|
2001-07-17 17:52:51 -03:00
|
|
|
self.check_tempfile(os.tempnam())
|
|
|
|
|
|
|
|
name = os.tempnam(TESTFN)
|
|
|
|
self.check_tempfile(name)
|
|
|
|
|
|
|
|
name = os.tempnam(TESTFN, "pfx")
|
|
|
|
self.assert_(os.path.basename(name)[:3] == "pfx")
|
|
|
|
self.check_tempfile(name)
|
|
|
|
|
|
|
|
def test_tmpfile(self):
|
|
|
|
if not hasattr(os, "tmpfile"):
|
|
|
|
return
|
|
|
|
fp = os.tmpfile()
|
|
|
|
fp.write("foobar")
|
|
|
|
fp.seek(0,0)
|
|
|
|
s = fp.read()
|
|
|
|
fp.close()
|
|
|
|
self.assert_(s == "foobar")
|
|
|
|
|
|
|
|
def test_tmpnam(self):
|
|
|
|
if not hasattr(os, "tmpnam"):
|
|
|
|
return
|
2001-08-20 17:10:01 -03:00
|
|
|
warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
|
2002-04-15 22:27:44 -03:00
|
|
|
r"test_os$")
|
2001-07-17 17:52:51 -03:00
|
|
|
self.check_tempfile(os.tmpnam())
|
2001-07-20 22:41:30 -03:00
|
|
|
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
# Test attributes on return values from os.*stat* family.
|
|
|
|
class StatAttributeTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
os.mkdir(TESTFN)
|
|
|
|
self.fname = os.path.join(TESTFN, "f1")
|
|
|
|
f = open(self.fname, 'wb')
|
|
|
|
f.write("ABC")
|
|
|
|
f.close()
|
2001-10-18 18:57:37 -03:00
|
|
|
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
def tearDown(self):
|
|
|
|
os.unlink(self.fname)
|
|
|
|
os.rmdir(TESTFN)
|
|
|
|
|
|
|
|
def test_stat_attributes(self):
|
|
|
|
if not hasattr(os, "stat"):
|
|
|
|
return
|
|
|
|
|
|
|
|
import stat
|
|
|
|
result = os.stat(self.fname)
|
|
|
|
|
|
|
|
# Make sure direct access works
|
|
|
|
self.assertEquals(result[stat.ST_SIZE], 3)
|
|
|
|
self.assertEquals(result.st_size, 3)
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# Make sure all the attributes are there
|
|
|
|
members = dir(result)
|
|
|
|
for name in dir(stat):
|
|
|
|
if name[:3] == 'ST_':
|
|
|
|
attr = name.lower()
|
|
|
|
self.assertEquals(getattr(result, attr),
|
|
|
|
result[getattr(stat, name)])
|
|
|
|
self.assert_(attr in members)
|
|
|
|
|
|
|
|
try:
|
|
|
|
result[200]
|
|
|
|
self.fail("No exception thrown")
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Make sure that assignment fails
|
|
|
|
try:
|
|
|
|
result.st_mode = 1
|
|
|
|
self.fail("No exception thrown")
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
result.st_rdev = 1
|
|
|
|
self.fail("No exception thrown")
|
2001-10-18 18:19:31 -03:00
|
|
|
except (AttributeError, TypeError):
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
result.parrot = 1
|
|
|
|
self.fail("No exception thrown")
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Use the stat_result constructor with a too-short tuple.
|
|
|
|
try:
|
|
|
|
result2 = os.stat_result((10,))
|
|
|
|
self.fail("No exception thrown")
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Use the constructr with a too-long tuple.
|
|
|
|
try:
|
|
|
|
result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2001-10-18 18:57:37 -03:00
|
|
|
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
def test_statvfs_attributes(self):
|
|
|
|
if not hasattr(os, "statvfs"):
|
|
|
|
return
|
|
|
|
|
|
|
|
import statvfs
|
2002-06-11 03:22:31 -03:00
|
|
|
try:
|
|
|
|
result = os.statvfs(self.fname)
|
|
|
|
except OSError, e:
|
|
|
|
# On AtheOS, glibc always returns ENOSYS
|
|
|
|
import errno
|
|
|
|
if e.errno == errno.ENOSYS:
|
|
|
|
return
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
|
|
|
|
# Make sure direct access works
|
|
|
|
self.assertEquals(result.f_bfree, result[statvfs.F_BFREE])
|
|
|
|
|
|
|
|
# Make sure all the attributes are there
|
|
|
|
members = dir(result)
|
|
|
|
for name in dir(statvfs):
|
|
|
|
if name[:2] == 'F_':
|
|
|
|
attr = name.lower()
|
|
|
|
self.assertEquals(getattr(result, attr),
|
|
|
|
result[getattr(statvfs, name)])
|
|
|
|
self.assert_(attr in members)
|
|
|
|
|
|
|
|
# Make sure that assignment really fails
|
|
|
|
try:
|
|
|
|
result.f_bfree = 1
|
|
|
|
self.fail("No exception thrown")
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
result.parrot = 1
|
|
|
|
self.fail("No exception thrown")
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Use the constructor with a too-short tuple.
|
|
|
|
try:
|
|
|
|
result2 = os.statvfs_result((10,))
|
|
|
|
self.fail("No exception thrown")
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Use the constructr with a too-long tuple.
|
|
|
|
try:
|
|
|
|
result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2001-07-17 17:52:51 -03:00
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
def test_main():
|
2002-08-22 16:40:33 -03:00
|
|
|
suite = unittest.TestSuite()
|
|
|
|
suite.addTest(unittest.makeSuite(TemporaryFileTests))
|
|
|
|
suite.addTest(unittest.makeSuite(StatAttributeTests))
|
|
|
|
run_suite(suite)
|
2001-09-20 18:33:42 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|