Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-120608: Make reversed iterator work with free-threading#120971
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
33 commits
Select commit
Hold shift + click to select a range
57bc2dc
Make reversed iterator thread-safe
eendebakpt 7127a85
Add test for free-threading
eendebakpt 687a976
📜🤖 Added by blurb_it.
blurb-it[bot] a74a33c
Apply suggestions from code review
eendebakpt de1b3c6
make reversed_len thread safe
eendebakpt 3bef3fd
update news entry, make reversed_reduce safe
eendebakpt 220b414
update reversed_setstate for free-threading
eendebakpt 28aa548
Update Objects/enumobject.c
eendebakpt 2574051
Merge branch 'main' into reverse_ft_v2
eendebakpt e052ca4
simplify test
eendebakpt 8a7876a
Update Misc/NEWS.d/next/Core and Builtins/2024-06-24-20-08-55.gh-issu…
eendebakpt 081ba40
review comments: use for loop instead of comprehension
eendebakpt b2f2dd3
move test; skip free_after_iterating test on ft build
eendebakpt 8c6d136
Merge branch 'main' into reverse_ft_v2
eendebakpt 125645e
Merge branch 'main' into reverse_ft_v2
eendebakpt b90add3
Merge branch 'main' into reverse_ft_v2
eendebakpt 98f663c
Merge branch 'main' into reverse_ft_v2
eendebakpt 75a5fce
Merge branch 'main' into reverse_ft_v2
eendebakpt c7f6f92
lint
eendebakpt 568b6d4
Merge branch 'main' into reverse_ft_v2
eendebakpt a5524a6
Merge branch 'main' into reverse_ft_v2
eendebakpt a0c316b
improve test
eendebakpt fc80d56
use Barrier in the test
eendebakpt 4d2a7fc
Merge branch 'main' into reverse_ft_v2
eendebakpt b1071df
remove testing code
eendebakpt aa71804
review comments
eendebakpt 76931aa
rework
eendebakpt 2c5e7ef
add thread helper code
eendebakpt b36bd50
Update Lib/test/test_free_threading/test_reversed.py
kumaraditya303 01f31a7
use ctx manager
kumaraditya303 ff702ef
Merge branch 'main' into reverse_ft_v2
eendebakpt c4c1ea9
Remove trailing comma in .github/workflows/build.yml
ambv eb60c8f
Revert "Remove trailing comma in .github/workflows/build.yml"
ambv 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import unittest | ||
| from threading import Barrier, Thread | ||
| from test.support import threading_helper | ||
| threading_helper.requires_working_threading(module=True) | ||
| class TestReversed(unittest.TestCase): | ||
| @threading_helper.reap_threads | ||
| def test_reversed(self): | ||
| # Iterating over the iterator with multiple threads should not | ||
| # emit TSAN warnings | ||
| number_of_iterations = 10 | ||
| number_of_threads = 10 | ||
| size = 1_000 | ||
| barrier = Barrier(number_of_threads) | ||
| def work(r): | ||
| barrier.wait() | ||
| while True: | ||
| try: | ||
| l = r.__length_hint__() | ||
| next(r) | ||
| except StopIteration: | ||
| break | ||
| assert 0 <= l <= size | ||
| x = tuple(range(size)) | ||
| for _ in range(number_of_iterations): | ||
| r = reversed(x) | ||
| worker_threads = [] | ||
| for _ in range(number_of_threads): | ||
| worker_threads.append(Thread(target=work, args=[r])) | ||
| with threading_helper.start_threads(worker_threads): | ||
| pass | ||
| barrier.reset() | ||
| if __name__ == "__main__": | ||
| unittest.main() |
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
4 changes: 4 additions & 0 deletions
4 Misc/NEWS.d/next/Core_and_Builtins/2024-06-24-20-08-55.gh-issue-120608.d75n8U.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,4 @@ | ||
| Adapt :func:`reversed` for use in the free-theading build. | ||
| The :func:`reversed` is still not thread-safe in the sense that concurrent | ||
| iterations may see the same object, but they will not corrupt the interpreter | ||
| state. |
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 |
|---|---|---|
| @@ -439,20 +439,22 @@ reversed_next(PyObject *op) | ||
| { | ||
| reversedobject *ro = _reversedobject_CAST(op); | ||
| PyObject *item; | ||
| Py_ssize_t index = ro->index; | ||
| Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); | ||
| if (index >= 0) { | ||
| item = PySequence_GetItem(ro->seq, index); | ||
| if (item != NULL) { | ||
| ro->index--; | ||
| FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, index - 1); | ||
| return item; | ||
| } | ||
| if (PyErr_ExceptionMatches(PyExc_IndexError) || | ||
| PyErr_ExceptionMatches(PyExc_StopIteration)) | ||
| PyErr_Clear(); | ||
| } | ||
| ro->index = -1; | ||
| FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, -1); | ||
| #ifndef Py_GIL_DISABLED | ||
kumaraditya303 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Py_CLEAR(ro->seq); | ||
| #endif | ||
| return NULL; | ||
| } | ||
| @@ -461,13 +463,15 @@ reversed_len(PyObject *op, PyObject *Py_UNUSED(ignored)) | ||
| { | ||
| reversedobject *ro = _reversedobject_CAST(op); | ||
| Py_ssize_t position, seqsize; | ||
| Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); | ||
| if (ro->seq == NULL) | ||
| if (index == -1) | ||
| return PyLong_FromLong(0); | ||
| assert(ro->seq != NULL); | ||
| seqsize = PySequence_Size(ro->seq); | ||
| if (seqsize == -1) | ||
| return NULL; | ||
| position = ro->index + 1; | ||
| position = index + 1; | ||
| return PyLong_FromSsize_t((seqsize < position) ? 0 : position); | ||
| } | ||
| @@ -477,10 +481,13 @@ static PyObject * | ||
| reversed_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) | ||
| { | ||
| reversedobject *ro = _reversedobject_CAST(op); | ||
| if (ro->seq) | ||
| Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); | ||
| if (index != -1) { | ||
| return Py_BuildValue("O(O)n", Py_TYPE(ro), ro->seq, ro->index); | ||
| else | ||
| } | ||
| else { | ||
| return Py_BuildValue("O(())", Py_TYPE(ro)); | ||
| } | ||
| } | ||
| static PyObject * | ||
| @@ -490,15 +497,19 @@ reversed_setstate(PyObject *op, PyObject *state) | ||
| Py_ssize_t index = PyLong_AsSsize_t(state); | ||
| if (index == -1 && PyErr_Occurred()) | ||
| return NULL; | ||
| if (ro->seq != 0) { | ||
| Py_ssize_t ro_index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); | ||
| // if the iterator is exhausted we do not set the state | ||
| // this is for backwards compatibility reasons. in practice this situation | ||
| // will not occur, see gh-120971 | ||
| if (ro_index != -1) { | ||
| Py_ssize_t n = PySequence_Size(ro->seq); | ||
| if (n < 0) | ||
| return NULL; | ||
| if (index < -1) | ||
| index = -1; | ||
| else if (index > n-1) | ||
| index = n-1; | ||
| ro->index = index; | ||
| FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, index); | ||
| } | ||
| Py_RETURN_NONE; | ||
| } | ||
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.