-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-111495: Add PyFile_*
CAPI tests
#111709
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
77afe78
gh-111495: Add `PyFile_*` CAPI tests
sobolevn 340d256
Merge branch 'main' into issue-111495
sobolevn 52c5918
Address review
sobolevn 8022299
Merge branch 'main' into issue-111495
sobolevn 3781efb
Partially address review, stil need to change invalid utf8 tests
sobolevn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import unittest | ||
import io | ||
import os | ||
|
||
from test.support import import_helper, os_helper | ||
|
||
# Skip this test if the _testcapi module isn't available. | ||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
NULL = None | ||
|
||
|
||
class TestPyFileCAPI(unittest.TestCase): | ||
def tearDown(self): | ||
try: | ||
os.unlink(os_helper.TESTFN) | ||
except FileNotFoundError: | ||
pass | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_file_from_fd(self): | ||
# PyFile_FromFd() | ||
with open(os_helper.TESTFN, "w") as f: | ||
# raise ValueError(*file_mode, 0, "utf-8", "strict", "\n", 0,) | ||
file_obj = _testcapi.file_from_fd( | ||
f.fileno(), os_helper.TESTFN, "w", | ||
1, "utf-8", "strict", "\n", 0, | ||
) | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# We don't apply heavy testing here, because inside it directly calls | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# `_io.open` which is fully tested in `test_io`. | ||
self.assertIsInstance(file_obj, io.TextIOWrapper) | ||
|
||
def test_file_get_line(self): | ||
# PyFile_GetLine | ||
get_line = _testcapi.file_get_line | ||
|
||
# Create file with unicode content: | ||
first_line = "text with юникод 统一码" | ||
with open(os_helper.TESTFN, "w") as f: | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f.writelines([first_line]) | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
with open(os_helper.TESTFN) as f: | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(get_line(f, 0), first_line) | ||
with open(os_helper.TESTFN) as f: | ||
self.assertEqual(get_line(f, 4), 'text') | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with open(os_helper.TESTFN) as f: | ||
self.assertEqual(get_line(f, -1), first_line) | ||
|
||
# Create file with bytes content: | ||
first_line = "text with юникод 统一码".encode("utf8") | ||
with open(os_helper.TESTFN, mode="wb") as f: | ||
f.writelines([first_line]) | ||
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 has the same content. |
||
|
||
with open(os_helper.TESTFN, 'rb') as f: | ||
self.assertEqual(get_line(f, 0), first_line) | ||
with open(os_helper.TESTFN, 'rb') as f: | ||
self.assertEqual(get_line(f, 4), b'text') | ||
with open(os_helper.TESTFN, 'rb') as f: | ||
self.assertEqual(get_line(f, -1), first_line) | ||
|
||
# Create empty file: | ||
with open(os_helper.TESTFN, mode="w") as f: | ||
pass | ||
|
||
with open(os_helper.TESTFN) as f: | ||
self.assertEqual(get_line(f, 0), '') | ||
with open(os_helper.TESTFN) as f: | ||
self.assertEqual(get_line(f, 4), '') | ||
with open(os_helper.TESTFN) as f: | ||
self.assertRaises(EOFError, get_line, f, -1) | ||
|
||
# Not `bytes` or `str` is returned: | ||
class _BadIO(io.IOBase): | ||
def readline(self, size = 0): | ||
return object() | ||
|
||
self.assertRaises(TypeError, get_line, _BadIO(), 0) | ||
self.assertRaises(AttributeError, get_line, object(), 0) | ||
# CRASHES: get_line(NULL) | ||
|
||
def test_file_write_object(self): | ||
# PyFile_WriteObject | ||
write = _testcapi.file_write_object | ||
|
||
def write_and_return(obj, flags=0): | ||
dst = io.StringIO() | ||
write(obj, dst, flags) | ||
return dst.getvalue() | ||
|
||
self.assertEqual(write_and_return(1), '1') | ||
self.assertEqual(write_and_return('1'), "'1'") | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(write_and_return(False), 'False') | ||
self.assertEqual(write_and_return(b'123'), "b'123'") | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class Custom: | ||
def __str__(self): | ||
return '<str>' | ||
def __repr__(self): | ||
return '<repr>' | ||
|
||
self.assertEqual(write_and_return(Custom()), '<repr>') | ||
self.assertEqual( | ||
write_and_return(Custom(), flags=_testcapi.Py_PRINT_RAW), | ||
'<str>', | ||
) | ||
|
||
self.assertRaises(TypeError, write, object(), None) | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# CRASHES: write(NULL, io.StringIO(), flags) | ||
# CRASHES: write(NULL, NULL, flags) | ||
|
||
def test_file_write_string(self): | ||
# PyFile_WriteString | ||
write = _testcapi.file_write_string | ||
|
||
def write_and_return(string): | ||
dst = io.StringIO() | ||
write(string, dst) | ||
return dst.getvalue() | ||
|
||
self.assertEqual(write_and_return("abc"), "abc") | ||
self.assertEqual( | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
write_and_return("text with юникод 统一码"), | ||
"text with юникод 统一码", | ||
) | ||
self.assertRaises(TypeError, write, object(), io.StringIO()) | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertRaises(AttributeError, write, 'abc', object()) | ||
# CRASHES: write('', NULL) | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_object_as_file_descriptor(self): | ||
# PyObject_AsFileDescriptor() | ||
as_fd = _testcapi.object_as_file_descriptor | ||
with open(os_helper.TESTFN, mode="w") as f: | ||
self.assertEqual(as_fd(f), f.fileno()) | ||
with open(os_helper.TESTFN, mode="w") as f: | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(as_fd(f.fileno()), f.fileno()) | ||
|
||
class _BadIO(io.IOBase): | ||
def fileno(self): | ||
return object() # not int | ||
self.assertRaises(TypeError, as_fd, _BadIO()) | ||
self.assertRaises(TypeError, as_fd, object()) | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# CRASHES as_fd(NULL) | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.