-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-142651: Make call_count of Mock thread safe #142656
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
chaope
wants to merge
17
commits into
python:main
Choose a base branch
from
chaope:fix-issue-142651
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.
+29
−2
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a401818
Make call_count of Mock thread safe
chaope d532d23
📜🤖 Added by blurb_it.
blurb-it[bot] fe7be86
update doc
chaope 29578a9
calculate call_count after append call_args_list
chaope c2e4ca7
revert change on documentation for call_count.
chaope e9e6dfa
revert unnecessary change.
chaope 3a4b590
revert blank line deletion
chaope 70e9107
added TestThreadingMockRaceCondition
chaope dc0d804
Update Misc/NEWS.d/next/Library/2025-12-13-06-17-44.gh-issue-142651.Z…
chaope f05407e
Update Lib/test/test_unittest/testmock/testthreadingmock.py
chaope 3a81b0d
update test
chaope 23825f4
Merge branch 'fix-issue-142651' of https://github.com/chaope/cpython …
chaope a76318e
move self._executor.shutdown to addCleanup
chaope 8e2348c
use 3k loops
chaope e7dcc57
add @requires_resource('cpu')
chaope 851f698
update test according to comments
chaope 3b29867
update according to comments
chaope 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
Some comments aren't visible on the classic Files Changed page.
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
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2025-12-13-06-17-44.gh-issue-142651.ZRtBu4.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,3 @@ | ||
| :mod:`unittest.mock`: fix a thread safety issue where :attr:`Mock.call_count | ||
| <unittest.mock.Mock.call_count>` may return inaccurate values when the mock | ||
| is called concurrently from multiple threads. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me think that we should have this as a context manager instead but this is a separate issue. I'll do it tomorrow myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to do it in a separate PR? I can create a new PR for context manager.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you but I will need to check if it'll be useful in much places and whether it could create conflicts with backports (usually refactoring and new features in tests are annoying because it creates differences with older branches).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I think adding a context manager would be a good practice for me to learn and involve more in CPython development. Do you mind me giving it a try? Is there a sample PR you would like me to follow? How would you evaluate it being useful? (Like will be used in more than x places?)
also, do we need to back port this change to older versions? If so, may I also give it a try? Is there any pending steps before we want to merge this PR? (Saw that you have requested a review from @cjw296 , assuming that is the next step?)