-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-127350: Add Py_fopen() function #127821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
773c4d9
b80f424
14fb249
7390c5e
26a5a56
4375566
a779321
e613374
a908b72
2af4d2b
8e9170e
a8c26b2
5b52a4e
e6a53fe
1d1e7f4
335c1bc
8241422
c91aa82
0fc6dc1
dda4335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import os | ||
import unittest | ||
from test import support | ||
from test.support import import_helper, os_helper | ||
|
||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
|
||
class CAPIFileTest(unittest.TestCase): | ||
def test_py_fopen(self): | ||
# Test Py_fopen() and Py_fclose() | ||
|
||
with open(__file__, "rb") as fp: | ||
source = fp.read() | ||
|
||
for filename in (__file__, os.fsencode(__file__)): | ||
with self.subTest(filename=filename): | ||
data = _testcapi.py_fopen(filename, "rb") | ||
self.assertEqual(data, source[:256]) | ||
|
||
data = _testcapi.py_fopen(os_helper.FakePath(filename), "rb") | ||
self.assertEqual(data, source[:256]) | ||
|
||
filenames = [ | ||
os_helper.TESTFN, | ||
os.fsencode(os_helper.TESTFN), | ||
] | ||
# TESTFN_UNDECODABLE cannot be used to create a file on macOS/WASI. | ||
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) | ||
|
||
data = _testcapi.py_fopen(filename, "rb") | ||
self.assertEqual(data, source[:256]) | ||
finally: | ||
os_helper.unlink(filename) | ||
|
||
# embedded null character/byte in the filename | ||
with self.assertRaises(ValueError): | ||
_testcapi.py_fopen("a\x00b", "rb") | ||
with self.assertRaises(ValueError): | ||
_testcapi.py_fopen(b"a\x00b", "rb") | ||
|
||
# non-ASCII mode failing with "Invalid argument" | ||
with self.assertRaises(OSError): | ||
_testcapi.py_fopen(__file__, "\xe9") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not possible to pass non-UTF-8 bytes, if (PySys_Audit("open", "Osi", path, mode, 0) < 0) {
return NULL;
} I don't think that it's worth to "support" non-UTF-8 just for the test, whereas it's rejected anyway by |
||
|
||
# invalid filename type | ||
for invalid_type in (123, object()): | ||
with self.subTest(filename=invalid_type): | ||
with self.assertRaises(TypeError): | ||
_testcapi.py_fopen(invalid_type, "rb") | ||
|
||
if support.MS_WINDOWS: | ||
with self.assertRaises(OSError): | ||
# On Windows, the file mode is limited to 10 characters | ||
_testcapi.py_fopen(__file__, "rt+, ccs=UTF-8") | ||
|
||
# CRASHES py_fopen(__file__, None) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Add :c:func:`Py_fopen` function to open a file. Similar to the :c:func:`!fopen` | ||
function, but the *path* parameter is a Python object and an exception is set | ||
on error. Add also :c:func:`Py_fclose` function to close a file, function | ||
needed for Windows support. | ||
Patch by Victor Stinner. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.