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-158102: Add tests for the slice C API by serhiy-storchaka · Pull Request #158120 · python/cpython Conversation
Member
Test
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Test PySlice_Check(), PySlice_New(), PySlice_GetIndices(), PySlice_Unpack(), PySlice_AdjustIndices() and PySlice_GetIndicesEx() -- both the macro and the deprecated function. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
serhiy-storchaka
requested review from
a team,
AA-Turner,
emmatyping,
erlend-aasland and
itamaro
as code owners
vstinner
reviewed
Member
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. Thanks for writing an exhaustive test suite for the PySlice C API. It's more complicated than what I expected! Here is a first review.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page.
Comment on lines
+8
to
+9
Member
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. In C, SIZE_MAX is a different constant: Can you rename the constants to SSIZE_MAX and SSIZE_MIN? The _testcapi uses different names: PY_SSIZE_T_MAX and PY_SSIZE_T_MIN.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Uh oh!
There was an error while loading. Please reload this page.
Comment on lines
+11
to
+13
Member
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.
Suggested change
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Member
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. PySlice_Unpack() replaces step=PY_SSIZE_T_MIN with step=PY_SSIZE_T_MAX. Would it be acceptable do the same here? Well, if we change the behavior, I would prefer to do it in a separated PR. It's better to test the current behavior without changing code first, as you do :-)
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Member
Author
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. Normally,
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Member
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's unfortunate that the function cannot fail. Can you update PySlice_AdjustIndices() document to mention that step must not be equal to zero and must not be equal to PY_SSIZE_T_MIN?
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
| self.assertEqual(adjust(10, SIZE_MIN, SIZE_MAX, 1), (10, 0, 10)) | ||
| self.assertEqual(adjust(10, SIZE_MAX, SIZE_MIN, -1), (10, 9, -1)) | ||
| self.assertEqual(adjust(0, 1, 7, 1), (0, 0, 0)) | ||
| self.assertEqual(adjust(0, 7, 1, -1), (0, -1, -1)) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests on negative length?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| // Need limited C API 3.6.1 for PySlice_Unpack() and PySlice_AdjustIndices() | ||
| // and for PySlice_GetIndicesEx() implemented as a macro. | ||
| #if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The limited C API can now be tested on Free Threading. You can also define Py_LIMITED_API if Py_GIL_DISABLED is defined, Py_TARGET_ABI3T is set to Py_LIMITED_API in this case by Include/pyabi.h.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| /* Test PySlice_GetIndicesEx() implemented as a macro using PySlice_Unpack() | ||
| * and PySlice_AdjustIndices(). */ | ||
| static PyObject * | ||
| slice_getindicesex(PyObject *Py_UNUSED(module), PyObject *args) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I suggest to rename this function to "slice_getindicesex_macro()", or rename "slice_getindicesex_deprecated()" to "slice_getindicesex_func()". Same remark for the test case class name.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| self.assertEqual(getindicesex(slice(7, 1, -2**1000), 10), | ||
| (7, 1, -SIZE_MAX, 1)) | ||
| self.assertEqual(getindicesex(slice(7, 1, SIZE_MIN), 10), | ||
| (7, 1, -SIZE_MAX, 1)) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sadly, PySlice_GetIndicesEx() also accepts negative length. You may also add tests on negative length.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Co-authored-by: Victor Stinner <[email protected]>
Also remove trailing whitespace added by a suggestion.
Rename SIZE_MAX and SIZE_MIN to SSIZE_MAX and SSIZE_MIN, and the PySlice_GetIndicesEx() wrappers to slice_getindicesex_macro() and slice_getindicesex_func(). Test the limited C API also on free threading. Reduce the number of tested combinations. Add tests for zero and negative length, and document that the length must not be negative.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for the update. Tests should now cover all code paths.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
GH-158291 is a backport of this pull request to the 3.15 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
GH-158292 is a backport of this pull request to the 3.14 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
GH-158293 is a backport of this pull request to the 3.13 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Test PySlice_Check(), PySlice_New(), PySlice_GetIndices(), PySlice_Unpack(), PySlice_AdjustIndices() and PySlice_GetIndicesEx() -- both the macro and the deprecated function. Document the length and step precondition of PySlice_AdjustIndices() (cherry picked from commit 8c056c3) Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
Test PySlice_Check(), PySlice_New(), PySlice_GetIndices(), PySlice_Unpack(), PySlice_AdjustIndices() and PySlice_GetIndicesEx() -- both the macro and the deprecated function. Document the length and step precondition of PySlice_AdjustIndices() (cherry picked from commit 8c056c3) Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
Test PySlice_Check(), PySlice_New(), PySlice_GetIndices(), PySlice_Unpack(), PySlice_AdjustIndices() and PySlice_GetIndicesEx() -- both the macro and the deprecated function. Document the length and step precondition of PySlice_AdjustIndices() (cherry picked from commit 8c056c3) Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
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.