Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions Lib/test/test_capi/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

_testcapi = import_helper.import_module('_testcapi')

NULL = None


class CAPIFileTest(unittest.TestCase):
def test_py_fopen(self):
Expand All @@ -25,15 +27,22 @@ def test_py_fopen(self):
os_helper.TESTFN,
os.fsencode(os_helper.TESTFN),
]
# TESTFN_UNDECODABLE cannot be used to create a file on macOS/WASI.
if os_helper.TESTFN_UNDECODABLE is not None:
filenames.append(os_helper.TESTFN_UNDECODABLE)
filenames.append(os.fsdecode(os_helper.TESTFN_UNDECODABLE))
if os_helper.TESTFN_UNENCODABLE is not None:
filenames.append(os_helper.TESTFN_UNENCODABLE)
for filename in filenames:
with self.subTest(filename=filename):
try:
with open(filename, "wb") as fp:
fp.write(source)

except OSError:
# TESTFN_UNDECODABLE cannot be used to create a file
# on macOS/WASI.
filename = None
continue
try:
data = _testcapi.py_fopen(filename, "rb")
self.assertEqual(data, source[:256])
finally:
Expand All @@ -47,7 +56,13 @@ def test_py_fopen(self):

# non-ASCII mode failing with "Invalid argument"
with self.assertRaises(OSError):
_testcapi.py_fopen(__file__, "\xe9")
_testcapi.py_fopen(__file__, b"\xc2\x80")
with self.assertRaises(OSError):
# \x98 is invalid in cp1250, cp1251, cp1257
# \x9d is invalid in cp1252-cp1255, cp1258
_testcapi.py_fopen(__file__, b"\xc2\x98\xc2\x9d")
with self.assertRaises((UnicodeDecodeError, OSError)):
_testcapi.py_fopen(__file__, b"\x98\x9d")

# invalid filename type
for invalid_type in (123, object()):
Expand All @@ -60,7 +75,8 @@ def test_py_fopen(self):
# On Windows, the file mode is limited to 10 characters
_testcapi.py_fopen(__file__, "rt+, ccs=UTF-8")

# CRASHES py_fopen(__file__, None)
# CRASHES _testcapi.py_fopen(NULL, 'rb')
# CRASHES _testcapi.py_fopen(__file__, NULL)


if __name__ == "__main__":
Expand Down
25 changes: 7 additions & 18 deletions Modules/_testcapi/clinic/file.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Modules/_testcapi/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ module _testcapi
_testcapi.py_fopen

path: object
mode: str
mode: str(zeroes=True, accept={robuffer, str, NoneType})
/

Call Py_fopen(), fread(256) and Py_fclose(). Return read bytes.
[clinic start generated code]*/

static PyObject *
_testcapi_py_fopen_impl(PyObject *module, PyObject *path, const char *mode)
/*[clinic end generated code: output=5a900af000f759de input=d7e7b8f0fd151953]*/
_testcapi_py_fopen_impl(PyObject *module, PyObject *path, const char *mode,
Py_ssize_t mode_length)
/*[clinic end generated code: output=69840d0cfd8b7fbb input=f3a579dd7eb60926]*/
{
NULLABLE(path);
FILE *fp = Py_fopen(path, mode);
if (fp == NULL) {
return NULL;
Expand Down
Loading