forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 4
Adding tp_reachable #65
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
mjp41
wants to merge
6
commits into
immutable-main
Choose a base branch
from
tp_reaches
base: immutable-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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """Tests for freeze warnings when tp_reachable is missing. | ||
|
|
||
| Uses the _test_reachable C extension which provides two static types: | ||
|
|
||
| HasTraverseNoReachable – has tp_traverse, tp_reachable deliberately NULL | ||
| NoTraverseNoReachable – neither tp_traverse nor tp_reachable | ||
| """ | ||
| import subprocess | ||
| import sys | ||
| import textwrap | ||
| import unittest | ||
|
|
||
|
|
||
| class TestReachableWarnings(unittest.TestCase): | ||
| """Test that freeze logs warnings when tp_reachable is missing.""" | ||
|
|
||
| def _run_code(self, code): | ||
| """Run code in a subprocess and return (stdout, stderr).""" | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", textwrap.dedent(code)], | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| return result.stdout, result.stderr | ||
|
|
||
| def test_warn_tp_traverse_no_tp_reachable(self): | ||
| """Warn when a C type has tp_traverse but no tp_reachable.""" | ||
| stdout, stderr = self._run_code("""\ | ||
| import _immutable, _test_reachable | ||
| obj = _test_reachable.HasTraverseNoReachable(42) | ||
| _immutable.freeze(obj) | ||
| """) | ||
| self.assertIn( | ||
| "freeze: type '_test_reachable.HasTraverseNoReachable' " | ||
| "has tp_traverse but no tp_reachable", | ||
| stderr, | ||
| ) | ||
|
|
||
| def test_warn_no_traverse_no_reachable(self): | ||
| """Warn when a C type has neither tp_traverse nor tp_reachable.""" | ||
| stdout, stderr = self._run_code("""\ | ||
| import _immutable, _test_reachable | ||
| obj = _test_reachable.NoTraverseNoReachable() | ||
| _immutable.freeze(obj) | ||
| """) | ||
| self.assertIn( | ||
| "freeze: type '_test_reachable.NoTraverseNoReachable' " | ||
| "has no tp_traverse and no tp_reachable", | ||
| stderr, | ||
| ) | ||
|
|
||
| def test_warn_only_once_per_type(self): | ||
| """A type should only produce the warning on the first freeze.""" | ||
| stdout, stderr = self._run_code("""\ | ||
| import _immutable, _test_reachable | ||
| _immutable.freeze(_test_reachable.HasTraverseNoReachable(1)) | ||
| _immutable.freeze(_test_reachable.HasTraverseNoReachable(2)) | ||
| _immutable.freeze(_test_reachable.HasTraverseNoReachable(3)) | ||
| """) | ||
| msg = ( | ||
| "freeze: type '_test_reachable.HasTraverseNoReachable' " | ||
| "has tp_traverse but no tp_reachable" | ||
| ) | ||
| count = stderr.count(msg) | ||
| self.assertEqual(count, 1, f"Expected 1 warning, got {count}:\n{stderr}") | ||
|
|
||
| def test_warn_different_types_separately(self): | ||
| """Different types should each produce their own warning.""" | ||
| stdout, stderr = self._run_code("""\ | ||
| import _immutable, _test_reachable | ||
| _immutable.freeze(_test_reachable.HasTraverseNoReachable(1)) | ||
| _immutable.freeze(_test_reachable.NoTraverseNoReachable()) | ||
| """) | ||
| self.assertIn("HasTraverseNoReachable", stderr) | ||
| self.assertIn("NoTraverseNoReachable", stderr) | ||
|
|
||
| def test_no_warning_with_tp_reachable(self): | ||
| """No warning for a type that has tp_reachable set.""" | ||
| stdout, stderr = self._run_code("""\ | ||
| import _immutable, _test_reachable | ||
| obj = _test_reachable.HasReachable(42) | ||
| _immutable.freeze(obj) | ||
| """) | ||
| self.assertNotIn("HasReachable", stderr) | ||
|
|
||
|
|
||
| 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
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
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
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 worry about write barriers on the cache. There is a lot of stuff around modifying this for NoGIL