GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing github.com — gh-119182: Use strict error handler in PyUnicode_FromFormat() by vstinner · Pull Request #120307 · python/cpython Conversation
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
What happens with truncated strings, like |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There are two tests on that: UnicodeDecodeError is raised in this case. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Example of test: test_capi.test_unicode # test "%s" format with precision check_format('abc', b'%.3s', b'abcdef') with self.assertRaises(UnicodeDecodeError): PyUnicode_FromFormat(b'%.5s', 'abc[\u20ac]'.encode('utf8')) check_format('abc[\u20ac', b'%.7s', 'abc[\u20ac]'.encode('utf8')) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This is bad. Such formats are common in error formatting code (not only in CPython, but in third-party code), and now you will get a UnicodeDecodeError instead of the original error even if all was fine with encoding. In this case I think that it it is better to truncate the string before the truncated sequence. But even without truncation, it may be better to get a replacement character in the error message of the correct exception than a UnicodeDecodeError. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
On my PR gh-120248, @methane wrote:
So I created this PR. @methane: What do you think? I can modify the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I think that @methane's comment was only related to the format string (which currently is ASCII-only), not to arguments. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I think 100 codepoints is the best option. About error handler, there is no correct answer. Theorically speaking, "strict" is "Errors should never pass silently." |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Precision should specify the length in bytes. This feature can be used to format not-null-teminated strings. char buffer[100]; PyUnicode_FromFormat("%.100s", buffer); If you start to count codepoints, you can read past the end of the array. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I abandon this PR. It seems like using |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.