summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-02-05 17:05:54 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-02-05 17:05:54 (GMT)
commitbb445a1f22835b97673f96da8f9edc75b69f5f2c (patch)
treef3d399d134896edc68edb46d5c761c385bbb20fe /Lib/test
parent007a618a38d0c4dac9182fed9b22f3caf55dd1ea (diff)
downloadcpython-bb445a1f22835b97673f96da8f9edc75b69f5f2c.zip
cpython-bb445a1f22835b97673f96da8f9edc75b69f5f2c.tar.gz
cpython-bb445a1f22835b97673f96da8f9edc75b69f5f2c.tar.bz2
Issue #5677: Explicitly forbid write operations on read-only file objects,
and read operations on write-only file objects. On Windows, the system C library would return a bogus result; on Solaris, it was possible to crash the interpreter. Patch by Stefan Krah.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_file2k.py36
-rw-r--r--Lib/test/test_sys.py2
2 files changed, 37 insertions, 1 deletions
diff --git a/Lib/test/test_file2k.py b/Lib/test/test_file2k.py
index bf47c6f..5c2de37 100644
--- a/Lib/test/test_file2k.py
+++ b/Lib/test/test_file2k.py
@@ -86,6 +86,8 @@ class AutoFileTests(unittest.TestCase):
self.assertTrue(repr(self.f).startswith("<open file '" + TESTFN))
def testErrors(self):
+ self.f.close()
+ self.f = open(TESTFN, 'rb')
f = self.f
self.assertEquals(f.name, TESTFN)
self.assertTrue(not f.isatty())
@@ -123,6 +125,40 @@ class AutoFileTests(unittest.TestCase):
def testReadWhenWriting(self):
self.assertRaises(IOError, self.f.read)
+ def testIssue5677(self):
+ # Remark: Do not perform more than one test per open file,
+ # since that does NOT catch the readline error on Windows.
+ data = 'xxx'
+ for mode in ['w', 'wb', 'a', 'ab']:
+ for attr in ['read', 'readline', 'readlines']:
+ self.f = open(TESTFN, mode)
+ self.f.write(data)
+ self.assertRaises(IOError, getattr(self.f, attr))
+ self.f.close()
+
+ self.f = open(TESTFN, mode)
+ self.f.write(data)
+ self.assertRaises(IOError, lambda: [line for line in self.f])
+ self.f.close()
+
+ self.f = open(TESTFN, mode)
+ self.f.write(data)
+ self.assertRaises(IOError, self.f.readinto, bytearray(len(data)))
+ self.f.close()
+
+ for mode in ['r', 'rb', 'U', 'Ub', 'Ur', 'rU', 'rbU', 'rUb']:
+ self.f = open(TESTFN, mode)
+ self.assertRaises(IOError, self.f.write, data)
+ self.f.close()
+
+ self.f = open(TESTFN, mode)
+ self.assertRaises(IOError, self.f.writelines, [data, data])
+ self.f.close()
+
+ self.f = open(TESTFN, mode)
+ self.assertRaises(IOError, self.f.truncate)
+ self.f.close()
+
class OtherFileTests(unittest.TestCase):
def testOpenDir(self):
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 6b45d12..f7e828b 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -581,7 +581,7 @@ class SizeofTest(unittest.TestCase):
# enumerate
check(enumerate([]), size(h + 'l3P'))
# file
- check(self.file, size(h + '4P2i4P3i3Pi'))
+ check(self.file, size(h + '4P2i4P3i3P3i'))
# float
check(float(0), size(h + 'd'))
# sys.floatinfo