summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-10-18 20:34:25 (GMT)
committerGuido van Rossum <guido@python.org>2001-10-18 20:34:25 (GMT)
commit98bf58f1c61a1d6d8a21f75527c8ad7a7d47ef67 (patch)
tree7d8eda64ad82c7ba9f0ca3fe9c88b5e43505c98c /Lib/test
parent8dd7adeb34cdbbb7b3b95ab502699d08b7ba328c (diff)
downloadcpython-98bf58f1c61a1d6d8a21f75527c8ad7a7d47ef67.zip
cpython-98bf58f1c61a1d6d8a21f75527c8ad7a7d47ef67.tar.gz
cpython-98bf58f1c61a1d6d8a21f75527c8ad7a7d47ef67.tar.bz2
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.)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_os.py121
1 files changed, 119 insertions, 2 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 3f26f2f..bec3b90 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -11,7 +11,6 @@ warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__)
from test_support import TESTFN, run_unittest
-
class TemporaryFileTests(unittest.TestCase):
def setUp(self):
self.files = []
@@ -61,10 +60,128 @@ class TemporaryFileTests(unittest.TestCase):
"test_os")
self.check_tempfile(os.tmpnam())
+# 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()
+
+ 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")
+ except TypeError:
+ 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
+
+
+ def test_statvfs_attributes(self):
+ if not hasattr(os, "statvfs"):
+ return
+
+ import statvfs
+ result = os.statvfs(self.fname)
+
+ # 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
def test_main():
run_unittest(TemporaryFileTests)
-
+ run_unittest(StatAttributeTests)
if __name__ == "__main__":
test_main()