-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-151130: Add more tests for PyWeakref_* C API #151131
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
Merged
serhiy-storchaka
merged 2 commits into
python:main
from
serhiy-storchaka:test_capi-test_weakref
Jun 9, 2026
Merged
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import weakref | ||
| import unittest | ||
| from test.support import import_helper | ||
|
|
||
| _testcapi = import_helper.import_module('_testcapi') | ||
| _testlimitedcapi = import_helper.import_module('_testlimitedcapi') | ||
| NULL = None | ||
|
|
||
| class Object: | ||
| pass | ||
|
|
||
| class Ref(weakref.ReferenceType): | ||
| pass | ||
|
|
||
|
|
||
| class CAPIWeakrefTest(unittest.TestCase): | ||
| def test_pyweakref_check(self): | ||
| # Test PyWeakref_Check() | ||
| check = _testlimitedcapi.pyweakref_check | ||
| obj = Object() | ||
| self.assertEqual(check(obj), 0) | ||
| self.assertEqual(check(weakref.ref(obj)), 1) | ||
| self.assertEqual(check(Ref(obj)), 1) | ||
| self.assertEqual(check(weakref.proxy(obj)), 1) | ||
|
|
||
| # CRASHES check(NULL) | ||
|
|
||
| def test_pyweakref_checkref(self): | ||
| # Test PyWeakref_CheckRef() | ||
| checkref = _testlimitedcapi.pyweakref_checkref | ||
| obj = Object() | ||
| self.assertEqual(checkref(obj), 0) | ||
| self.assertEqual(checkref(weakref.ref(obj)), 1) | ||
| self.assertEqual(checkref(Ref(obj)), 1) | ||
| self.assertEqual(checkref(weakref.proxy(obj)), 0) | ||
|
|
||
| # CRASHES checkref(NULL) | ||
|
|
||
| def test_pyweakref_checkrefexact(self): | ||
| # Test PyWeakref_CheckRefExact() | ||
| checkrefexact = _testlimitedcapi.pyweakref_checkrefexact | ||
| obj = Object() | ||
| self.assertEqual(checkrefexact(obj), 0) | ||
| self.assertEqual(checkrefexact(weakref.ref(obj)), 1) | ||
| self.assertEqual(checkrefexact(Ref(obj)), 0) | ||
| self.assertEqual(checkrefexact(weakref.proxy(obj)), 0) | ||
|
|
||
| # CRASHES checkrefexact(NULL) | ||
|
|
||
| def test_pyweakref_checkproxy(self): | ||
| # Test PyWeakref_CheckProxy() | ||
| checkproxy = _testlimitedcapi.pyweakref_checkproxy | ||
| obj = Object() | ||
| self.assertEqual(checkproxy(obj), 0) | ||
| self.assertEqual(checkproxy(weakref.ref(obj)), 0) | ||
| self.assertEqual(checkproxy(Ref(obj)), 0) | ||
| self.assertEqual(checkproxy(weakref.proxy(obj)), 1) | ||
|
|
||
| # CRASHES checkproxy(NULL) | ||
|
|
||
| def test_pyweakref_getref(self): | ||
| # Test PyWeakref_GetRef() | ||
| getref = _testcapi.pyweakref_getref | ||
| obj = Object() | ||
| wr = weakref.ref(obj) | ||
| wp = weakref.proxy(obj) | ||
| self.assertEqual(getref(wr), (1, obj)) | ||
| self.assertEqual(getref(wp), (1, obj)) | ||
| del obj | ||
| self.assertEqual(getref(wr), 0) | ||
| self.assertEqual(getref(wp), 0) | ||
|
|
||
| self.assertRaises(TypeError, getref, 42) | ||
| self.assertRaises(SystemError, getref, NULL) | ||
|
|
||
| def test_pyweakref_isdead(self): | ||
| # Test PyWeakref_IsDead() | ||
| isdead = _testcapi.pyweakref_isdead | ||
| obj = Object() | ||
| wr = weakref.ref(obj) | ||
| wp = weakref.proxy(obj) | ||
| self.assertEqual(isdead(wr), 0) | ||
| self.assertEqual(isdead(wp), 0) | ||
| del obj | ||
| self.assertEqual(isdead(wr), 1) | ||
| self.assertEqual(isdead(wp), 1) | ||
|
|
||
| self.assertRaises(TypeError, isdead, 42) | ||
| self.assertRaises(SystemError, isdead, NULL) | ||
|
|
||
| def test_pyweakref_newref(self): | ||
| # Test PyWeakref_NewRef() | ||
| newref = _testlimitedcapi.pyweakref_newref | ||
| obj = Object() | ||
| wr = newref(obj) | ||
| self.assertIs(type(wr), weakref.ReferenceType) | ||
| # PyWeakref_NewRef() handles None callback as NULL callback | ||
| wr = newref(obj, None) | ||
| self.assertIs(type(wr), weakref.ReferenceType) | ||
| log = [] | ||
| wr = newref(obj, log.append) | ||
| self.assertIs(type(wr), weakref.ReferenceType) | ||
| self.assertEqual(log, []) | ||
| del obj | ||
| self.assertEqual(log, [wr]) | ||
|
|
||
| self.assertRaises(TypeError, newref, []) | ||
| # CRASHES newref(NULL) | ||
|
|
||
| def test_pyweakref_newproxy(self): | ||
| # Test PyWeakref_NewProxy() | ||
| newproxy = _testlimitedcapi.pyweakref_newproxy | ||
| obj = Object() | ||
| wp = newproxy(obj) | ||
| self.assertIs(type(wp), weakref.ProxyType) | ||
| # PyWeakref_NewProxy() handles None callback as NULL callback | ||
| wp = newproxy(obj, None) | ||
|
serhiy-storchaka marked this conversation as resolved.
|
||
| self.assertIs(type(wp), weakref.ProxyType) | ||
| log = [] | ||
| wp = newproxy(obj, log.append) | ||
| self.assertIs(type(wp), weakref.ProxyType) | ||
| self.assertEqual(log, []) | ||
| del obj | ||
| self.assertEqual(log, [wp]) | ||
|
|
||
| def func(): | ||
| pass | ||
| wp = newproxy(func) | ||
| self.assertIs(type(wp), weakref.CallableProxyType) | ||
|
|
||
| self.assertRaises(TypeError, newproxy, []) | ||
| # CRASHES newproxy(NULL) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Tests/2026-06-09-11-52-52.gh-issue-151130.1vslPH.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 @@ | ||
| Add more tests for ``PyWeakref_*`` C API. |
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,46 @@ | ||
| #include "parts.h" | ||
| #include "util.h" | ||
|
|
||
|
|
||
| static PyObject * | ||
| pyweakref_getref(PyObject *module, PyObject *ref) | ||
| { | ||
| NULLABLE(ref); | ||
| PyObject *obj = UNINITIALIZED_PTR; | ||
| int rc = PyWeakref_GetRef(ref, &obj); | ||
| if (rc == -1 && PyErr_Occurred()) { | ||
| assert(obj == NULL); | ||
| return NULL; | ||
| } | ||
| if (obj == NULL) { | ||
| return Py_BuildValue("i", rc); | ||
| } | ||
| else { | ||
| assert(obj != UNINITIALIZED_PTR); | ||
| return Py_BuildValue("iN", rc, obj); | ||
| } | ||
| } | ||
|
|
||
| static PyObject * | ||
| pyweakref_isdead(PyObject *module, PyObject *obj) | ||
| { | ||
| NULLABLE(obj); | ||
| int rc = PyWeakref_IsDead(obj); | ||
| if (rc == -1 && PyErr_Occurred()) { | ||
| return NULL; | ||
| } | ||
| return PyLong_FromLong(rc); | ||
| } | ||
|
|
||
|
|
||
| static PyMethodDef test_methods[] = { | ||
| {"pyweakref_getref", pyweakref_getref, METH_O}, | ||
| {"pyweakref_isdead", pyweakref_isdead, METH_O}, | ||
| {NULL}, | ||
| }; | ||
|
|
||
| int | ||
| _PyTestCapi_Init_Weakref(PyObject *m) | ||
| { | ||
| return PyModule_AddFunctions(m, test_methods); | ||
| } |
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,78 @@ | ||
| #include "pyconfig.h" // Py_GIL_DISABLED | ||
| #ifndef Py_GIL_DISABLED | ||
| // Need limited C API 3.5 for PyModule_AddFunctions() | ||
| # define Py_LIMITED_API 0x03050000 | ||
| #endif | ||
|
|
||
| #include "parts.h" | ||
| #include "util.h" | ||
|
|
||
|
|
||
| static PyObject * | ||
| pyweakref_check(PyObject *module, PyObject *obj) | ||
| { | ||
| NULLABLE(obj); | ||
| return PyLong_FromLong(PyWeakref_Check(obj)); | ||
| } | ||
|
|
||
| static PyObject * | ||
| pyweakref_checkref(PyObject *module, PyObject *obj) | ||
| { | ||
| NULLABLE(obj); | ||
| return PyLong_FromLong(PyWeakref_CheckRef(obj)); | ||
| } | ||
|
|
||
| static PyObject * | ||
| pyweakref_checkrefexact(PyObject *module, PyObject *obj) | ||
| { | ||
| NULLABLE(obj); | ||
| return PyLong_FromLong(PyWeakref_CheckRefExact(obj)); | ||
| } | ||
|
|
||
| static PyObject * | ||
| pyweakref_checkproxy(PyObject *module, PyObject *obj) | ||
| { | ||
| NULLABLE(obj); | ||
| return PyLong_FromLong(PyWeakref_CheckProxy(obj)); | ||
| } | ||
|
|
||
| static PyObject * | ||
| pyweakref_newref(PyObject *module, PyObject *args) | ||
| { | ||
| PyObject *obj; | ||
| PyObject *callback = NULL; | ||
| if (!PyArg_ParseTuple(args, "O|O", &obj, &callback)) { | ||
| return NULL; | ||
| } | ||
| NULLABLE(obj); | ||
| return PyWeakref_NewRef(obj, callback); | ||
| } | ||
|
|
||
| static PyObject * | ||
| pyweakref_newproxy(PyObject *module, PyObject *args) | ||
| { | ||
| PyObject *obj; | ||
| PyObject *callback = NULL; | ||
| if (!PyArg_ParseTuple(args, "O|O", &obj, &callback)) { | ||
| return NULL; | ||
| } | ||
| NULLABLE(obj); | ||
| return PyWeakref_NewProxy(obj, callback); | ||
| } | ||
|
|
||
|
|
||
| static PyMethodDef test_methods[] = { | ||
| {"pyweakref_check", pyweakref_check, METH_O}, | ||
| {"pyweakref_checkref", pyweakref_checkref, METH_O}, | ||
| {"pyweakref_checkrefexact", pyweakref_checkrefexact, METH_O}, | ||
| {"pyweakref_checkproxy", pyweakref_checkproxy, METH_O}, | ||
| {"pyweakref_newref", pyweakref_newref, METH_VARARGS}, | ||
| {"pyweakref_newproxy", pyweakref_newproxy, METH_VARARGS}, | ||
| {NULL}, | ||
| }; | ||
|
|
||
| int | ||
| _PyTestLimitedCAPI_Init_Weakref(PyObject *m) | ||
| { | ||
| return PyModule_AddFunctions(m, test_methods); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.