Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
GH-84783: Make the slice object hashable#101264
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
15 commits Select commit Hold shift + click to select a range
61c8048 fix the typo in the tuplehash func's comment
furkanonder ef22a92 make hashable the slice
furkanonder 12c2488 Merge branch 'main' into issue-84783
furkanonder 6dd9926 Merge branch 'main' into issue-84783
furkanonder 886bfff fix failed slice's test cases
furkanonder 2971147 Merge branch 'main' into issue-84783
furkanonder 093bf38 📜🤖 Added by blurb_it.
blurb-it[bot] 159f3f2 Add version changed to slice's doc
furkanonder 0c78cfc Update test_doctest.py
OTheDev cff08dd Update Doc/library/functions.rst
furkanonder ea55931 Merge branch 'main' into issue-84783
furkanonder f0302bf Update version changed to slice's doc
furkanonder 2c76491 Merge pull request #1 from OTheDev/patch-1
furkanonder cbd1527 Add more testcases for slice's hash
furkanonder 9f55e46 update testcases for slice's hash
furkanonder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -80,10 +80,16 @@ def test_repr(self): | ||
| self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)") | ||
| def test_hash(self): | ||
| # Verify clearing of SF bug #800796 | ||
| self.assertRaises(TypeError, hash, slice(5)) | ||
| self.assertEqual(hash(slice(5)), slice(5).__hash__()) | ||
| self.assertEqual(hash(slice(1, 2)), slice(1, 2).__hash__()) | ||
| self.assertEqual(hash(slice(1, 2, 3)), slice(1, 2, 3).__hash__()) | ||
| self.assertNotEqual(slice(5), slice(6)) | ||
furkanonder marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| with self.assertRaises(TypeError): | ||
| hash(slice(1, 2, [])) | ||
| with self.assertRaises(TypeError): | ||
| slice(5).__hash__() | ||
| hash(slice(4,{})) | ||
| def test_cmp(self): | ||
| s1 = slice(1, 2, 3) | ||
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Core and Builtins/2023-02-11-23-14-06.gh-issue-84783._P5sMa.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Make the slice object hashable. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -628,6 +628,42 @@ slice_traverse(PySliceObject *v, visitproc visit, void *arg) | ||
| return 0; | ||
| } | ||
| /* code based on tuplehash() of Objects/tupleobject.c */ | ||
rhettinger marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| #if SIZEOF_PY_UHASH_T > 4 | ||
| #define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL) | ||
| #define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL) | ||
| #define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL) | ||
| #define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */ | ||
| #else | ||
| #define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL) | ||
| #define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL) | ||
| #define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL) | ||
| #define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */ | ||
| #endif | ||
| static Py_hash_t | ||
| slicehash(PySliceObject *v) | ||
| { | ||
| Py_uhash_t acc = _PyHASH_XXPRIME_5; | ||
| #define _PyHASH_SLICE_PART(com){\ | ||
furkanonder marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| Py_uhash_t lane = PyObject_Hash(v->com); \ | ||
| if(lane == (Py_uhash_t)-1){\ | ||
| return -1; \ | ||
| } \ | ||
| acc += lane * _PyHASH_XXPRIME_2; \ | ||
| acc = _PyHASH_XXROTATE(acc); \ | ||
| acc *= _PyHASH_XXPRIME_1; \ | ||
| } | ||
| _PyHASH_SLICE_PART(start); | ||
| _PyHASH_SLICE_PART(stop); | ||
| _PyHASH_SLICE_PART(step); | ||
| #undef _PyHASH_SLICE_PART | ||
| if(acc == (Py_uhash_t)-1){ | ||
| return 1546275796; | ||
| } | ||
| return acc; | ||
| } | ||
| PyTypeObject PySlice_Type ={ | ||
| PyVarObject_HEAD_INIT(&PyType_Type, 0) | ||
| "slice", /* Name of this type */ | ||
| @@ -642,7 +678,7 @@ PyTypeObject PySlice_Type ={ | ||
| 0, /* tp_as_number */ | ||
| 0, /* tp_as_sequence */ | ||
| 0, /* tp_as_mapping */ | ||
| PyObject_HashNotImplemented, /* tp_hash */ | ||
| (hashfunc)slicehash, /* tp_hash */ | ||
| 0, /* tp_call */ | ||
| 0, /* tp_str */ | ||
| PyObject_GenericGetAttr, /* tp_getattro */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.