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-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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
f1b3c94871c0130a82e96a5e39e8f2bd8536feef7a577c1376390e57e5ec87cb7d7a96f0a09c3bf07654f53afa6d4366c51cb46071554a94e45d254b1b3b2bd96552977c88bb06fe751c9ae051474f6505274122247160936a2fb414521a1db34b8763ea80472ec253b927d082998fedFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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.
Uh 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() | ||
| 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.
Uh 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 */ | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.