-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-116738: Make _json module safe in the free-threading build #119438
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
Open
eendebakpt
wants to merge
33
commits into
python:main
Choose a base branch
from
eendebakpt:json_ft
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
da0e917
Make the _json module thread safe
eendebakpt 3797dfa
Update Modules/_json.c
eendebakpt 366654c
handle goto and return statements
eendebakpt 5b72cdf
Apply suggestions from code review
eendebakpt c4c24c3
Update Include/internal/pycore_critical_section.h
eendebakpt 370191b
rename macro
eendebakpt 93c4466
Merge branch 'main' into json_ft
eendebakpt eafd3c1
fix typo
eendebakpt daeec46
Merge branch 'json_ft' of github.com:eendebakpt/cpython into json_ft
eendebakpt d54baf2
fix missing to exit critical section
eendebakpt e5fa305
revert changes to tests
eendebakpt d4ddf5d
📜🤖 Added by blurb_it.
blurb-it[bot] 67d942f
Merge branch 'main' into json_ft
eendebakpt 4ffc1b2
Merge branch 'main' into json_ft
eendebakpt 384ca59
sync with main
eendebakpt 64e20aa
sync with main
eendebakpt e6ce9c9
update news entry
eendebakpt 34885a0
fix normal build
eendebakpt 2fe760b
Merge branch 'main' into json_ft
eendebakpt eebccac
add lock around result of PyMapping_Items
eendebakpt db8947c
add tests
eendebakpt c19ad14
fix argument of Py_END_CRITICAL_SECTION_SEQUENCE_FAST
eendebakpt 8b12e0f
Merge branch 'main' into json_ft
eendebakpt 78d3595
avoid Py_EXIT_CRITICAL_SECTION_SEQUENCE_FAST
eendebakpt 6e8615f
use barriers in test
eendebakpt 39ebc00
typo
eendebakpt 7c5b185
whitespace
eendebakpt adf78c7
Merge branch 'main' into json_ft
eendebakpt acd0ad1
resolve merge conflicts
eendebakpt 41e3dee
Update Misc/NEWS.d/next/Core_and_Builtins/2024-06-04-20-26-21.gh-issu…
eendebakpt 75884cb
cleanup
eendebakpt 0424c58
Merge branch 'json_ft' of github.com:eendebakpt/cpython into json_ft
eendebakpt 9c964f9
Merge branch 'main' into json_ft
eendebakpt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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,84 @@ | ||
import unittest | ||
from threading import Barrier, Thread | ||
from test.test_json import CTest | ||
from test.support import threading_helper | ||
|
||
|
||
def encode_json_helper(json, worker, data, number_of_threads, number_of_json_encodings=100): | ||
worker_threads = [] | ||
barrier = Barrier(number_of_threads) | ||
for index in range(number_of_threads): | ||
worker_threads.append(Thread(target=worker, args=[barrier, data, index])) | ||
for t in worker_threads: | ||
t.start() | ||
for ii in range(number_of_json_encodings): | ||
json.dumps(data) | ||
data.clear() | ||
for t in worker_threads: | ||
t.join() | ||
|
||
|
||
class MyMapping(dict): | ||
def __init__(self): | ||
self.mapping = [] | ||
|
||
def items(self): | ||
return self.mapping | ||
|
||
|
||
@threading_helper.reap_threads | ||
@threading_helper.requires_working_threading() | ||
class TestJsonEncoding(CTest): | ||
# Test encoding json with multiple threads modifying the data cannot | ||
# corrupt the interpreter | ||
|
||
def test_json_mutating_list(self): | ||
|
||
def worker(barrier, data, index): | ||
barrier.wait() | ||
while data: | ||
for d in data: | ||
if len(d) > 5: | ||
d.clear() | ||
else: | ||
d.append(index) | ||
d.append(index) | ||
d.append(index) | ||
encode_json_helper(self.json, worker, [[], []], number_of_threads=16) | ||
|
||
def test_json_mutating_dict(self): | ||
|
||
def worker(barrier, data, index): | ||
barrier.wait() | ||
while data: | ||
for d in data: | ||
if len(d) > 5: | ||
try: | ||
d.pop(list(d)[0]) | ||
except (KeyError, IndexError): | ||
pass | ||
else: | ||
d[index] = index | ||
encode_json_helper(self.json, worker, [{}, {}], number_of_threads=16) | ||
|
||
def test_json_mutating_mapping(self): | ||
|
||
def worker(barrier, data, index): | ||
barrier.wait() | ||
while data: | ||
for d in data: | ||
if len(d.mapping) > 3: | ||
d.mapping.clear() | ||
else: | ||
d.mapping.append((index, index)) | ||
encode_json_helper(self.json, | ||
worker, [MyMapping(), MyMapping()], number_of_threads=16) | ||
|
||
|
||
if __name__ == "__main__": | ||
import time | ||
|
||
t0 = time.time() | ||
unittest.main() | ||
dt = time.time()-t0 | ||
print(f'Done: {dt:.2f}') |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core_and_Builtins/2024-06-04-20-26-21.gh-issue-116738.q_hPYq.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 module :mod:`json` safe to use under the free-theading build. | ||
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
Oops, something went wrong.
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.