diff options
author | Victor Stinner <vstinner@python.org> | 2023-08-24 13:59:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-24 13:59:12 (GMT) |
commit | 67266266469fe0e817736227f39537182534c1a5 (patch) | |
tree | 1ae99ebf16335cbd67678f3911702e9819cbe039 /Modules/_ctypes | |
parent | c163d7f0b67a568e9b64eeb9c1cbbaa127818596 (diff) | |
download | cpython-67266266469fe0e817736227f39537182534c1a5.zip cpython-67266266469fe0e817736227f39537182534c1a5.tar.gz cpython-67266266469fe0e817736227f39537182534c1a5.tar.bz2 |
gh-108314: Add PyDict_ContainsString() function (#108323)
Diffstat (limited to 'Modules/_ctypes')
-rw-r--r-- | Modules/_ctypes/_ctypes.c | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index dc80291..ed9efca 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5139,8 +5139,6 @@ static PyObject * Pointer_get_contents(CDataObject *self, void *closure) { StgDictObject *stgdict; - PyObject *keep, *ptr_probe; - CDataObject *ptr2ptr; if (*(void **)self->b_ptr == NULL) { PyErr_SetString(PyExc_ValueError, @@ -5151,29 +5149,37 @@ Pointer_get_contents(CDataObject *self, void *closure) stgdict = PyObject_stgdict((PyObject *)self); assert(stgdict); /* Cannot be NULL for pointer instances */ - keep = GetKeepedObjects(self); + PyObject *keep = GetKeepedObjects(self); if (keep != NULL) { // check if it's a pointer to a pointer: // pointers will have '0' key in the _objects - ptr_probe = PyDict_GetItemString(keep, "0"); - - if (ptr_probe != NULL) { - ptr2ptr = (CDataObject*) PyDict_GetItemString(keep, "1"); - if (ptr2ptr == NULL) { + int ptr_probe = PyDict_ContainsString(keep, "0"); + if (ptr_probe < 0) { + return NULL; + } + if (ptr_probe) { + PyObject *item; + if (PyDict_GetItemStringRef(keep, "1", &item) < 0) { + return NULL; + } + if (item == NULL) { PyErr_SetString(PyExc_ValueError, - "Unexpected NULL pointer in _objects"); + "Unexpected NULL pointer in _objects"); return NULL; } - // don't construct a new object, - // return existing one instead to preserve refcount +#ifndef NDEBUG + CDataObject *ptr2ptr = (CDataObject *)item; + // Don't construct a new object, + // return existing one instead to preserve refcount. + // Double-check that we are returning the same thing. assert( *(void**) self->b_ptr == ptr2ptr->b_ptr || *(void**) self->b_value.c == ptr2ptr->b_ptr || *(void**) self->b_ptr == ptr2ptr->b_value.c || *(void**) self->b_value.c == ptr2ptr->b_value.c - ); // double-check that we are returning the same thing - Py_INCREF(ptr2ptr); - return (PyObject *) ptr2ptr; + ); +#endif + return item; } } |