FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-36203: Check callback is callable in PyWeakref_NewRef by tekknolagi · Pull Request #26273 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 8 additions & 0 deletions Lib/test/test_weakref.py
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
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
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 @@
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.
10 changes: 10 additions & 0 deletions Modules/_testcapimodule.c
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
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
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
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

Back | FazBrowse Home | New Git URL