Skip to content

bpo-36203: Check callback is callable in PyWeakref_NewRef #26273

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ def test_cfunction(self):
self.check_basic_ref(create_cfunction)
self.check_basic_callback(create_cfunction)

def test_PyWeakref_NewRef_with_non_callable_raises_type_error(self):
import _testcapi
o = C()
with self.assertRaises(TypeError) as ctx:
_testcapi.PyWeakref_NewRef(o, 3)
self.assertEqual(str(ctx.exception),
"callback must be None, NULL, or callable object")

def test_multiple_callbacks(self):
o = C()
ref1 = weakref.ref(o, self.callback)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Amend PyWeakref_New to raise a TypeError if the callback given is not NULL, None, or a callable object. The documentation previously falsely claimed this check occurred.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Amend PyWeakref_New to raise a TypeError if the callback given is not NULL, None, or a callable object. The documentation previously falsely claimed this check occurred.
Amend :c:func:`PyWeakref_New` to raise :exc:`TypeError` if the *callback* given is not NULL, None, or a callable object. The documentation previously falsely claimed this check occurred.

10 changes: 10 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,15 @@ test_fatal_error(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}

static PyObject *
test_PyWeakref_NewRef(PyObject *self, PyObject *args) {
PyObject *ob = NULL;
PyObject *callback = NULL;
if (!PyArg_ParseTuple(args, "OO", &ob, &callback))
return NULL;
return PyWeakref_NewRef(ob, callback);
}

// type->tp_version_tag
static PyObject *
type_get_version(PyObject *self, PyObject *type)
Expand Down Expand Up @@ -3368,6 +3377,7 @@ static PyMethodDef TestMethods[] = {
{"function_set_defaults", function_set_defaults, METH_VARARGS, NULL},
{"function_get_kw_defaults", function_get_kw_defaults, METH_O, NULL},
{"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
{"PyWeakref_NewRef", test_PyWeakref_NewRef, METH_VARARGS},
{NULL, NULL} /* sentinel */
};

Expand Down
7 changes: 7 additions & 0 deletions Objects/weakrefobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,13 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback)
if (callback == NULL)
/* return existing weak reference if it exists */
result = ref;
else {
if (!PyCallable_Check(callback)) {
PyErr_SetString(PyExc_TypeError,
"callback must be None, NULL, or callable object");
return NULL;
}
}
if (result != NULL)
Py_INCREF(result);
else {
Expand Down