8000 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
Show file tree
Hide file tree
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
add tests
  • Loading branch information
picnixz committed Aug 27, 2024
commit 60ab0bb67f1b0469a2b76f9b25fc6f5ae2e89efb
46 changes: 46 additions & 0 deletions Lib/test/test_capi/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,52 @@ def test_err_formatunraisable(self):
# CRASHES formatunraisable(NULL, NULL)


class TestUnicodeError(unittest.TestCase):

def test_unicode_encode_error_get_start(self):
test_func = _testcapi.unicode_encode_get_start
self._test_unicode_error_get_start('x', UnicodeEncodeError, test_func)

def test_unicode_decode_error_get_start(self):
test_func = _testcapi.unicode_decode_get_start
self._test_unicode_error_get_start(b'x', UnicodeDecodeError, test_func)

def _test_unicode_error_get_start(self, literal, exc_type, test_func):
for obj_len, py_start, c_start in [
# normal cases
(5, 0, 0),
(5, 1, 1),
(5, 2, 2),
# negative start is clamped to 0
(0, -1, 0),
(2, -1, 0),
# out of range start is clamped to max(0, obj_len - 1)
(0, 0, 0),
(0, 1, 0),
(0, 10, 0),
(2, 0, 0),
(5, 5, 4),
(5, 10, 4),
]:
c_start_computed = py_start
if c_start_computed < 0:
c_start_computed = 0
if c_start_computed >= obj_len:
if obj_len == 0:
c_start_computed = 0
else:
c_start_computed = obj_len - 1

s = literal * obj_len
py_end = py_start + 1

with self.subTest(s, exc_type=exc_type, py_start=py_start, c_start=c_start):
self.assertEqual(c_start, c_start_computed)
exc = exc_type('utf-8', s, py_start, py_end, 'reason')
c_start_actual = test_func(exc)
self.assertEqual(c_start_actual, c_start)


class Test_PyUnstable_Exc_PrepReraiseStar(ExceptionIsLikeMixin, unittest.TestCase):

def setUp(self):
Expand Down
23 changes: 23 additions & 0 deletions Modules/_testcapi/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,27 @@ _testcapi_unstable_exc_prep_reraise_star_impl(PyObject *module,
return PyUnstable_Exc_PrepReraiseStar(orig, excs);
}

/* Test PyUnicodeEncodeError_GetStart */
static PyObject *
unicode_encode_get_start(PyObject *Py_UNUSED(module), PyObject *arg)
{
Py_ssize_t start;
if (PyUnicodeEncodeError_GetStart(arg, &start) < 0) {
return NULL;
}
RETURN_SIZE(start);
}

/* Test PyUnicodeDecodeError_GetStart */
static PyObject *
unicode_decode_get_start(PyObject *Py_UNUSED(module), PyObject *arg)
{
Py_ssize_t start;
if (PyUnicodeDecodeError_GetStart(arg, &start) < 0) {
return NULL;
}
RETURN_SIZE(start);
}

/*
* Define the PyRecurdingInfinitelyError_Type
Expand Down Expand Up @@ -403,6 +424,8 @@ static PyMethodDef test_methods[] = {
_TESTCAPI_SET_EXCEPTION_METHODDEF
_TESTCAPI_TRACEBACK_PRINT_METHODDEF
_TESTCAPI_UNSTABLE_EXC_PREP_RERAISE_STAR_METHODDEF
{"unicode_encode_get_start", unicode_encode_get_start, METH_O},
{"unicode_decode_get_start", unicode_decode_get_start, METH_O},
{NULL},
};

Expand Down
Loading
0