55
66_testcapi = import_helper .import_module ('_testcapi' )
77
8+ NULL = None
9+
810
911class CAPIFileTest (unittest .TestCase ):
1012 def test_py_fopen (self ):
@@ -25,15 +27,22 @@ def test_py_fopen(self):
2527 os_helper .TESTFN ,
2628 os .fsencode (os_helper .TESTFN ),
2729 ]
28- # TESTFN_UNDECODABLE cannot be used to create a file on macOS/WASI.
30+ if os_helper .TESTFN_UNDECODABLE is not None :
31+ filenames .append (os_helper .TESTFN_UNDECODABLE )
32+ filenames .append (os .fsdecode (os_helper .TESTFN_UNDECODABLE ))
2933 if os_helper .TESTFN_UNENCODABLE is not None :
3034 filenames .append (os_helper .TESTFN_UNENCODABLE )
3135 for filename in filenames :
3236 with self .subTest (filename = filename ):
3337 try :
3438 with open (filename , "wb" ) as fp :
3539 fp .write (source )
36-
40+ except OSError :
41+ # TESTFN_UNDECODABLE cannot be used to create a file
42+ # on macOS/WASI.
43+ filename = None
44+ continue
45+ try :
3746 data = _testcapi .py_fopen (filename , "rb" )
3847 self .assertEqual (data , source [:256 ])
3948 finally :
@@ -47,7 +56,14 @@ def test_py_fopen(self):
4756
4857 # non-ASCII mode failing with "Invalid argument"
4958 with self .assertRaises (OSError ):
50- _testcapi .py_fopen (__file__ , "\xe9 " )
59+ _testcapi .py_fopen (__file__ , b"\xc2 \x80 " )
60+ with self .assertRaises (OSError ):
61+ # \x98 is invalid in cp1250, cp1251, cp1257
62+ # \x9d is invalid in cp1252-cp1255, cp1258
63+ _testcapi .py_fopen (__file__ , b"\xc2 \x98 \xc2 \x9d " )
64+ # UnicodeDecodeError can come from the audit hook code
65+ with self .assertRaises ((UnicodeDecodeError , OSError )):
66+ _testcapi .py_fopen (__file__ , b"\x98 \x9d " )
5167
5268 # invalid filename type
5369 for invalid_type in (123 , object ()):
@@ -60,7 +76,8 @@ def test_py_fopen(self):
6076 # On Windows, the file mode is limited to 10 characters
6177 _testcapi .py_fopen (__file__ , "rt+, ccs=UTF-8" )
6278
63- # CRASHES py_fopen(__file__, None)
79+ # CRASHES _testcapi.py_fopen(NULL, 'rb')
80+ # CRASHES _testcapi.py_fopen(__file__, NULL)
6481
6582
6683if __name__ == "__main__" :
0 commit comments