Skip to content

Commit a8c26b2

Browse files
committed
Add more tests
1 parent 8e9170e commit a8c26b2

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

Lib/test/test_capi/test_file.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,55 @@
11
import os
22
import unittest
3-
from test.support import import_helper
3+
from test import support
4+
from test.support import import_helper, os_helper
45

56
_testcapi = import_helper.import_module('_testcapi')
67

78

89
class CAPIFileTest(unittest.TestCase):
910
def test_py_fopen(self):
11+
# Test Py_fopen() and Py_fclose()
1012
for filename in (__file__, os.fsencode(__file__)):
1113
with self.subTest(filename=filename):
1214
content = _testcapi.py_fopen(filename, "rb")
1315
with open(filename, "rb") as fp:
1416
self.assertEqual(fp.read(256), content)
1517

18+
with open(__file__, "rb") as fp:
19+
content = fp.read()
20+
for filename in (
21+
os_helper.TESTFN,
22+
os.fsencode(os_helper.TESTFN),
23+
os_helper.TESTFN_UNDECODABLE,
24+
os_helper.TESTFN_UNENCODABLE,
25+
):
26+
with self.subTest(filename=filename):
27+
try:
28+
with open(filename, "wb") as fp:
29+
fp.write(content)
30+
31+
content = _testcapi.py_fopen(filename, "rb")
32+
with open(filename, "rb") as fp:
33+
self.assertEqual(fp.read(256), content[:256])
34+
finally:
35+
os_helper.unlink(filename)
36+
37+
# embedded null character/byte in the filename
38+
with self.assertRaises(ValueError):
39+
_testcapi.py_fopen("a\x00b", "rb")
40+
with self.assertRaises(ValueError):
41+
_testcapi.py_fopen(b"a\x00b", "rb")
42+
1643
for invalid_type in (123, object()):
1744
with self.subTest(filename=invalid_type):
1845
with self.assertRaises(TypeError):
1946
_testcapi.py_fopen(invalid_type, "r")
2047

48+
if support.MS_WINDOWS:
49+
with self.assertRaises(OSError):
50+
# On Windows, the file mode is limited to 10 characters
51+
_testcapi.py_fopen(__file__, "rt+, ccs=UTF-8")
52+
2153

2254
if __name__ == "__main__":
2355
unittest.main()

0 commit comments

Comments
 (0)