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
gh-111495: Add PyFile_* CAPI tests
#111709
base: main
Are you sure you want to change the base?
Conversation
|
Tests fail on Windows (I have a very limited experience with this platform): Is it correct? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This family has little functions, but they should be tested with many cases.
Because the default encoding on Windows is not UTF-8. Always specify encoding for text files. |
|
@serhiy-storchaka thanks a lot for your detailed review! You are one of the best reviewers I know :) |
|
Address sanitizer build fails with: Maybe I should use a different string? Suggestions? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
| @@ -0,0 +1,296 @@ | |||
| import unittest | |||
| import io | |||
| import os | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: sort imports :-)
It's unrelated to Address sanitizer. It's just that this CI builds Python is release mode. And in release mode, the error handler is only used if the string cannot be decoded (decoding error). In debug mode, the error handler is always checked. You can skip this test if |
|
To reproduce the Address Sanitizer issue, I used: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I have not finished the review yet. It is difficult with so many tests. So I can find other issues later.
The main problem is that they incorrectly create non-decodable files. You should use binary files to write them.
It would be nice also to reduce the number of lines where it is possible.
| def test_name_invalid_utf(self): | ||
| with open(os_helper.TESTFN, "w", encoding="utf-8") as f: | ||
| file_obj = _testcapi.file_from_fd( | ||
| f.fileno(), "abc\xe9", "w", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not invalid UTF-8. When you pass the Python string, it is encoded to UTF-8, therefore the C string is always valid UTF-8. You have to pass a bytes object, e.g. b'\xff'. See for example tests for PyDict_GetItemString() or PyObject_GetAttrString().
| TypeError, | ||
| r"open\(\) argument 'mode' must be str, not None", | ||
| _testcapi.file_from_fd, | ||
| f.fileno(), "abc\xe9", NULL, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use TESTFN?
When the assertRaisesRegex() call is multiline, it may be clearer to write it in the context manager form. The functional form is convenient when you can fit all in a single line.
| file_obj = _testcapi.file_from_fd( | ||
| f.fileno(), os_helper.TESTFN, "w", | ||
| 1, "utf-8", "strict", "\n", 0, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would write this in more compact form. First, add shorted name from_fd = _testcapi.file_from_fd, then this call can fit in just two lines:
file_obj = from_fd(f.fileno(), os_helper.TESTFN, "w",
1, "utf-8", "strict", "\n", 0)| f.fileno(), os_helper.TESTFN, "w", | ||
| 1, "utf-8", "strict", "\n", 0, | ||
| ) | ||
| self.assertIsInstance(file_obj, io.TextIOWrapper) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a check for the .name attribute.
| ) | ||
|
|
||
| def test_string_args_as_null(self): | ||
| for arg_pos in (4, 5, 6): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to write it without loop. So you can see what case is crashed if there is a crash. And it is easier to modify every case.
For example, I suggest to add checks for corresponding attributes (.encoding, .errors, .newlines).
| def test_file_empty_line(self): | ||
| first_line = "" | ||
| with open(os_helper.TESTFN, "w", encoding="utf-8") as f: | ||
| f.writelines([first_line]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to write an empty line.
| first_line = "\xc3\x28\n" | ||
| with open(os_helper.TESTFN, "w", encoding="utf-8") as f: | ||
| f.writelines([first_line]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, it does not create invalid UTF-8.
| ) | ||
| self.assertEqual(self.write_and_return(False), "False") | ||
|
|
||
| def test_file_write_custom_obj(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a test for object that raises an exception in __str__() or __repr__()?
| self.assertRaises(AttributeError, self.write, NULL, object(), 0) | ||
| self.assertRaises(TypeError, self.write, NULL, NULL, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please test these cases also with Py_PRINT_RAW.
Add cases for writing NULL to StringIO.
| with open(os_helper.TESTFN, "w", encoding="utf-8") as f: | ||
| f.writelines([first_line, second_line]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many tests can use StringIO. E.g.
f = io.StringIO('first_line\nsecond_line\n')
Looks like
PyFile_SetOpenCodeHookis already tested here:cpython/Programs/_testembed.c
Lines 1177 to 1232 in 20cfab9