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-139716: StackRef tests are added#139717
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
efimov-mikhail wants to merge 32 commits into python:mainChoose a base branch from efimov-mikhail:stackref_tests
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.
Uh oh!
There was an error while loading. Please reload this page.
Open
Changes from all commits
Commits
Show all changes
32 commits Select commit Hold shift + click to select a range
f1b3c94 First variant of stackref tests
efimov-mikhail 871c013 Fixes for Py_STACKREF_DEBUG build
efimov-mikhail 0a82e96 Improvements
efimov-mikhail a5e39e8 Do not check flags on FT build
efimov-mikhail f2bd853 Merge branch 'main' into stackref_tests
sergey-miryanov 6feef7a Review addressed
efimov-mikhail 577c137 Merge branch 'main' into stackref_tests
efimov-mikhail 6390e57 Build fix
efimov-mikhail e5ec87c Merge branch 'main' into stackref_tests
efimov-mikhail b7d7a96 Merge branch 'main' into stackref_tests
efimov-mikhail f0a09c3 Review addressed
efimov-mikhail bf07654 Some comments are added
efimov-mikhail f53afa6 Wording
efimov-mikhail d4366c5 Merge branch 'main' into stackref_tests
efimov-mikhail 1cb4607 Merge branch 'main' into stackref_tests
efimov-mikhail 1554a94 Merge branch 'main' into stackref_tests
efimov-mikhail e45d254 Merge branch 'main' into stackref_tests
efimov-mikhail b1b3b2b Changes in Py_BuildValue calls
efimov-mikhail d965529 Changes in Py_BuildValue calls 2
efimov-mikhail 77c88bb Improvements in test
efimov-mikhail 06fe751 Improvements in test 2
efimov-mikhail c9ae051 Remove blank line
efimov-mikhail 474f650 Use -1 in flags only for immortal objects on ft build
efimov-mikhail 5274122 Rename obj -> res in aux functions
efimov-mikhail 2471609 Use constants in test
efimov-mikhail 36a2fb4 Remove blank line
efimov-mikhail 14521a1 Asserts on mortality
efimov-mikhail db34b87 Comment change
efimov-mikhail 63ea804 Comments improvement
efimov-mikhail 72ec253 Merge branch 'main' into stackref_tests
efimov-mikhail b927d08 Merge branch 'main' into stackref_tests
efimov-mikhail 2998fed Lint fix
efimov-mikhail 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import unittest | ||
| import sys | ||
| from test.support import cpython_only | ||
| try: | ||
| import _testinternalcapi | ||
| except ImportError: | ||
| _testinternalcapi = None | ||
| @cpython_only | ||
| class TestDefinition(unittest.TestCase): | ||
| BIG_REFCOUNT = 1000 | ||
| FLAGS_REFCOUNT = 1 | ||
| FLAGS_NO_REFCOUNT = 0 | ||
| FLAGS_INVALID = -1 | ||
| def test_equivalence(self): | ||
| def run_with_refcount_check(self, func, obj): | ||
| refcount = sys.getrefcount(obj) | ||
| res = func(obj) | ||
| self.assertEqual(sys.getrefcount(obj), refcount) | ||
| return res | ||
| funcs_with_incref = [ | ||
| _testinternalcapi.stackref_from_object_new, | ||
| _testinternalcapi.stackref_from_object_steal_with_incref, | ||
| _testinternalcapi.stackref_make_heap_safe, | ||
| _testinternalcapi.stackref_make_heap_safe_with_borrow, | ||
| _testinternalcapi.stackref_strong_reference, | ||
| ] | ||
| funcs_with_borrow = [ | ||
| _testinternalcapi.stackref_from_object_borrow, | ||
| _testinternalcapi.stackref_dup_borrowed_with_close, | ||
| ] | ||
| immortal_objs = (None, True, False, 42, '1') | ||
| for obj in immortal_objs: | ||
| self.assertTrue(sys._is_immortal(obj)) | ||
| results = set() | ||
| for func in funcs_with_incref + funcs_with_borrow: | ||
| refcount, flags = run_with_refcount_check(self, func, obj) | ||
| self.assertGreater(refcount, self.BIG_REFCOUNT) | ||
efimov-mikhail marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| self.assertIn(flags, (self.FLAGS_REFCOUNT, self.FLAGS_INVALID)) | ||
| results.add((refcount, flags)) | ||
| self.assertEqual(len(results), 1) | ||
| mortal_objs = (object(), range(10), iter([1, 2, 3])) | ||
| for obj in mortal_objs: | ||
| self.assertFalse(sys._is_immortal(obj)) | ||
| results = set() | ||
| for func in funcs_with_incref: | ||
| refcount, flags = run_with_refcount_check(self, func, obj) | ||
| self.assertLess(refcount, self.BIG_REFCOUNT) | ||
| self.assertEqual(flags, self.FLAGS_NO_REFCOUNT) | ||
| results.add((refcount, flags)) | ||
| self.assertEqual(len(results), 1) | ||
| results = set() | ||
| for func in funcs_with_borrow: | ||
| refcount, flags = run_with_refcount_check(self, func, obj) | ||
| self.assertLess(refcount, self.BIG_REFCOUNT) | ||
| self.assertEqual(flags, self.FLAGS_REFCOUNT) | ||
| results.add((refcount, flags)) | ||
| self.assertEqual(len(results), 1) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -35,6 +35,7 @@ | ||
| #include "pycore_pylifecycle.h" // _PyInterpreterConfig_InitFromDict() | ||
| #include "pycore_pystate.h" // _PyThreadState_GET() | ||
| #include "pycore_runtime_structs.h" // _PY_NSMALLPOSINTS | ||
| #include "pycore_stackref.h" // PyStackRef_FunctionCheck() | ||
| #include "pycore_unicodeobject.h" // _PyUnicode_TransformDecimalAndSpaceToASCII() | ||
| #include "clinic/_testinternalcapi.c.h" | ||
| @@ -2453,7 +2454,6 @@ module_get_gc_hooks(PyObject *self, PyObject *arg) | ||
| return result; | ||
| } | ||
| static void | ||
| check_threadstate_set_stack_protection(PyThreadState *tstate, | ||
| void *start, size_t size) | ||
| @@ -2504,6 +2504,103 @@ test_threadstate_set_stack_protection(PyObject *self, PyObject *Py_UNUSED(args)) | ||
| Py_RETURN_NONE; | ||
| } | ||
| static PyObject * | ||
| stackref_from_object_new(PyObject *self, PyObject *op) | ||
| { | ||
| // Make a new strong reference and give ownership of it to ref. | ||
| _PyStackRef ref = PyStackRef_FromPyObjectNew(op); | ||
| PyObject *res = _PyStackRef_AsTuple(ref, op); | ||
| // Remove a strong reference by closing ref. | ||
| PyStackRef_CLOSE(ref); | ||
| return res; | ||
| } | ||
| static PyObject * | ||
| stackref_from_object_steal_with_incref(PyObject *self, PyObject *op) | ||
| { | ||
| // Combination of Py_INCREF and PyStackRef_FromPyObjectSteal | ||
| // is equivalent to PyStackRef_FromPyObjectNew. | ||
| Py_INCREF(op); | ||
| // Transfer ownership of a strong reference from op to ref. | ||
| _PyStackRef ref = PyStackRef_FromPyObjectSteal(op); | ||
efimov-mikhail marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| PyObject *res = _PyStackRef_AsTuple(ref, op); | ||
| // Combination of PyStackRef_AsPyObjectSteal and Py_DECREF | ||
| // is equivalent to PyStackRef_CLOSE. | ||
| // Transfer ownership of a strong reference from ref to op2. | ||
| PyObject *op2 = PyStackRef_AsPyObjectSteal(ref); | ||
| Py_DECREF(op2); | ||
| return res; | ||
| } | ||
| static PyObject * | ||
| stackref_make_heap_safe(PyObject *self, PyObject *op) | ||
| { | ||
| _PyStackRef ref = PyStackRef_FromPyObjectNew(op); | ||
| // Transfer ownership of a strong reference to op from ref to ref2, | ||
| // ref shouldn't be used anymore. | ||
| _PyStackRef ref2 = PyStackRef_MakeHeapSafe(ref); | ||
| PyObject *res = _PyStackRef_AsTuple(ref2, op); | ||
| PyStackRef_CLOSE(ref2); | ||
| return res; | ||
| } | ||
| static PyObject * | ||
| stackref_make_heap_safe_with_borrow(PyObject *self, PyObject *op) | ||
| { | ||
| _PyStackRef ref = PyStackRef_FromPyObjectNew(op); | ||
| // Create a borrowed reference to op, ref keeps a strong reference. | ||
| _PyStackRef ref2 = PyStackRef_Borrow(ref); | ||
| // Make a new strong reference to op from ref2, | ||
| // ref2 shouldn't be used anymore. | ||
| _PyStackRef ref3 = PyStackRef_MakeHeapSafe(ref2); | ||
| // We can close ref, since ref3 is heap safe. | ||
| PyStackRef_CLOSE(ref); | ||
| PyObject *res = _PyStackRef_AsTuple(ref3, op); | ||
| PyStackRef_CLOSE(ref3); | ||
| return res; | ||
| } | ||
| static PyObject * | ||
| stackref_strong_reference(PyObject *self, PyObject *op) | ||
| { | ||
| // Combination of PyStackRef_FromPyObjectBorrow and | ||
| // PyStackRef_AsPyObjectSteal is equivalent to Py_NewRef. | ||
| _PyStackRef ref = PyStackRef_FromPyObjectBorrow(op); | ||
| PyObject *op2 = PyStackRef_AsPyObjectSteal(ref); | ||
| _PyStackRef ref2 = PyStackRef_FromPyObjectSteal(op2); | ||
| PyObject *res = _PyStackRef_AsTuple(ref2, op); | ||
| PyStackRef_CLOSE(ref2); | ||
| return res; | ||
| } | ||
| static PyObject * | ||
| stackref_from_object_borrow(PyObject *self, PyObject *op) | ||
| { | ||
| _PyStackRef ref = PyStackRef_FromPyObjectBorrow(op); | ||
| PyObject *res = _PyStackRef_AsTuple(ref, op); | ||
| // Closing borrowed ref is no-op. | ||
| PyStackRef_CLOSE(ref); | ||
| return res; | ||
| } | ||
| static PyObject * | ||
| stackref_dup_borrowed_with_close(PyObject *self, PyObject *op) | ||
| { | ||
| _PyStackRef ref = PyStackRef_FromPyObjectBorrow(op); | ||
| // Duplicate reference, ref and ref2 both will be | ||
| // correct borrowed references from op. | ||
| _PyStackRef ref2 = PyStackRef_DUP(ref); | ||
| PyStackRef_XCLOSE(ref); | ||
| PyObject *res = _PyStackRef_AsTuple(ref2, op); | ||
| PyStackRef_XCLOSE(ref2); | ||
| return res; | ||
| } | ||
| static PyMethodDef module_functions[] ={ | ||
| {"get_configs", get_configs, METH_NOARGS}, | ||
| @@ -2618,6 +2715,13 @@ static PyMethodDef module_functions[] ={ | ||
| {"module_get_gc_hooks", module_get_gc_hooks, METH_O}, | ||
| {"test_threadstate_set_stack_protection", | ||
| test_threadstate_set_stack_protection, METH_NOARGS}, | ||
| {"stackref_from_object_new", stackref_from_object_new, METH_O}, | ||
| {"stackref_from_object_steal_with_incref", stackref_from_object_steal_with_incref, METH_O}, | ||
| {"stackref_make_heap_safe", stackref_make_heap_safe, METH_O}, | ||
| {"stackref_make_heap_safe_with_borrow", stackref_make_heap_safe_with_borrow, METH_O}, | ||
| {"stackref_strong_reference", stackref_strong_reference, METH_O}, | ||
| {"stackref_from_object_borrow", stackref_from_object_borrow, METH_O}, | ||
| {"stackref_dup_borrowed_with_close", stackref_dup_borrowed_with_close, METH_O}, | ||
| {NULL, NULL} /* sentinel */ | ||
| }; | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.