10000 gh-123378: fix some corner cases of `start` and `end` values in `PyUnicodeErrorObject` by picnixz · Pull Request #123380 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-123378: fix some corner cases of start and end values in PyUnicodeErrorObject #123380

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

Merged
merged 34 commits into from
Dec 4, 2024
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7a1574d
Fix `PyUnicode{Encode,Decode}Error_GetStart`.
picnixz Aug 26, 2024
6ef0c6d
blurb
picnixz Aug 27, 2024
60ab0bb
add tests
picnixz Aug 27, 2024
67b3d8e
fix NEWS
picnixz Aug 27, 2024
78fff57
remove a duplicated normal case
picnixz Aug 27, 2024
a6e6f80
handle start < 0
picnixz Aug 28, 2024
20c47ba
add C tests
picnixz Aug 28, 2024
51bc77e
add test coverage
picnixz Aug 28, 2024
b290e58
update docs
picnixz Aug 28, 2024
75398a9
fixup
picnixz Aug 28, 2024
546be87
update blurb
picnixz Aug 28, 2024
cded571
address Victor's review
picnixz Aug 29, 2024
1900d9a
refactor name
picnixz Aug 29, 2024
8acc563
fix refcounts
picnixz Aug 29, 2024
0538c83
remove debugging code
picnixz Aug 29, 2024
d5ea357
address Victor's review (round 2)
picnixz Aug 29, 2024
7c10769
handle negative 'start' and 'end' values
picnixz Aug 30, 2024
7ce2ef0
add C API tests
picnixz Aug 30, 2024
b55ca5a
add Python tests
picnixz Aug 30, 2024
4e34e5f
update docs
picnixz Aug 30, 2024
033a1ac
fix typo
picnixz Aug 30, 2024
c802e64
convert macros into `static inline` functions
picnixz Sep 13, 2024
66f33f8
Merge branch 'main' into fix/c-api-unicode-error-get-start
skirpichev Sep 20, 2024
6caf5b6
Merge remote-tracking branch 'upstream/main' into fix/c-api-unicode-e…
picnixz Oct 27, 2024
fcde448
post-merge cleanup
picnixz Oct 27, 2024
2e66302
Merge branch 'main' into fix/capi/unicode-error-start-end-123378
picnixz Nov 19, 2024
dc6b37b
Merge branch 'main' into fix/capi/unicode-error-start-end-123378
picnixz Nov 29, 2024
baa5cb2
fix typo
picnixz Dec 2, 2024
4c4808e
update NEWS and docs
picnixz Dec 2, 2024
efbdff1
add some assertion checks
picnixz Dec 2, 2024
180f3c2
add some assertion checks
picnixz Dec 2, 2024
4bd6e20
Merge branch 'main' into fix/c-api-unicode-error-get-start
picnixz Dec 2, 2024
5759a70
remove failing assertions for now
picnixz Dec 3, 2024
8c12171
fix docs
picnixz Dec 3, 2024
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/main' into fix/c-api-unicode-e…
…rror-get-start-123378
  • Loading branch information
picnixz committed Oct 27, 2024
commit 6caf5b66d045758211ad2c612a2a5e2fb73f9b65
69 changes: 49 additions & 20 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3058,23 +3058,27 @@ UnicodeEncodeError_str(PyObject *self)
PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;

if (!uself->object) {
if (exc->object == NULL) {
/* Not properly initialized. */
return PyUnicode_FromString("");
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
}

/* Get reason and encoding as strings, which they might not be if
they've been modified after we were constructed. */
reason_str = PyObject_Str(uself->reason);
reason_str = PyObject_Str(exc->reason);
if (reason_str == NULL) {
goto done;
}
encoding_str = PyObject_Str(uself->encoding);
encoding_str = PyObject_Str(exc->encoding);
if (encoding_str == NULL) {
goto done;
}
if (unicode_error_is_single_bad_char(uself)) {
Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);

Py_ssize_t len = PyUnicode_GET_LENGTH(exc->object);
Py_ssize_t start = exc->start, end = exc->end;

if ((start >= 0 && start < len) && (end >= 0 && end <= len) && end == start + 1) {
Py_UCS4 badchar = PyUnicode_ReadChar(exc->object, start);
const char *fmt;
if (badchar <= 0xff) {
fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Expand All @@ -3086,12 +3090,19 @@ UnicodeEncodeError_str(PyObject *self)
fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
}
result = PyUnicode_FromFormat(
fmt, encoding_str, (int)badchar, uself->start, reason_str);
fmt,
encoding_str,
(int)badchar,
start,
reason_str);
}
else {
result = PyUnicode_FromFormat(
"'%U' codec can't encode characters in position %zd-%zd: %U",
encoding_str, uself->start, uself->end - 1, reason_str);
encoding_str,
start,
end - 1,
reason_str);
}
done:
Py_XDECREF(reason_str);
Expand Down Expand Up @@ -3167,32 +3178,41 @@ UnicodeDecodeError_str(PyObject *self)
PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;

if (!uself->object) {
if (exc->object == NULL) {
/* Not properly initialized. */
return PyUnicode_FromString("");
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
}

/* Get reason and encoding as strings, which they might not be if
they've been modified after we were constructed. */
reason_str = PyObject_Str(uself->reason);
reason_str = PyObject_Str(exc->reason);
if (reason_str == NULL) {
goto done;
}
encoding_str = PyObject_Str(uself->encoding);
encoding_str = PyObject_Str(exc->encoding);
if (encoding_str == NULL) {
goto done;
}

if (unicode_error_is_single_bad_byte(uself)) {
int byte = (int)(PyBytes_AS_STRING(uself->object)[uself->start] & 0xff);
Py_ssize_t len = PyBytes_GET_SIZE(exc->object);
Py_ssize_t start = exc->start, end = exc->end;

if ((start >= 0 && start < len) && (end >= 0 && end <= len) && end == start + 1) {
int badbyte = (int)(PyBytes_AS_STRING(exc->object)[start] & 0xff);
result = PyUnicode_FromFormat(
"'%U' codec can't decode byte 0x%02x in position %zd: %U",
encoding_str, byte, uself->start, reason_str);
encoding_str,
badbyte,
start,
reason_str);
}
else {
result = PyUnicode_FromFormat(
"'%U' codec can't decode bytes in position %zd-%zd: %U",
encoding_str, uself->start, uself->end - 1, reason_str);
encoding_str,
start,
end - 1,
reason_str);
}
done:
Py_XDECREF(reason_str);
Expand Down Expand Up @@ -3270,8 +3290,11 @@ UnicodeTranslateError_str(PyObject *self)
goto done;
}

if (unicode_error_is_single_bad_char(uself)) {
Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
Py_ssize_t len = PyUnicode_GET_LENGTH(exc->object);
Py_ssize_t start = exc->start, end = exc->end;

if ((start >= 0 && start < len) && (end >= 0 && end <= len) && end == start + 1) {
Py_UCS4 badchar = PyUnicode_ReadChar(exc->object, start);
const char *fmt;
if (badchar <= 0xff) {
fmt = "can't translate character '\\x%02x' in position %zd: %U";
Expand All @@ -3282,12 +3305,18 @@ UnicodeTranslateError_str(PyObject *self)
else {
fmt = "can't translate character '\\U%08x' in position %zd: %U";
}
result = PyUnicode_FromFormat(fmt, (int)badchar, uself->start, reason_str);
result = PyUnicode_FromFormat(
fmt,
(int)badchar,
start,
reason_str);
}
else {
result = PyUnicode_FromFormat(
"can't translate characters in position %zd-%zd: %U",
uself->start, uself->end - 1, reason_str);
start,
end - 1,
reason_str);
}
done:
Py_XDECREF(reason_str);
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0