Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-111789: Use PyDict_GetItemRef() in _ctypes#111828
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 4 commits into python:main from serhiy-storchaka:use-dict_getitemref-in-ctypesNov 14, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
4c594fc gh-111789: Use PyDict_GetItemRef() in _ctypes
serhiy-storchaka 633bd89 Update Modules/_ctypes/_ctypes.c
serhiy-storchaka 0234faa Merge branch 'main' into use-dict_getitemref-in-ctypes
serhiy-storchaka 08d6090 Update Modules/_ctypes/callproc.c
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 |
|---|---|---|
| @@ -243,26 +243,13 @@ PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item) | ||
| static int | ||
| _PyDict_GetItemProxy(PyObject *dict, PyObject *key, PyObject **presult) | ||
| { | ||
| PyObject *item = PyDict_GetItemWithError(dict, key); | ||
| if (item == NULL){ | ||
| if (PyErr_Occurred()){ | ||
| return -1; | ||
| } | ||
| *presult = NULL; | ||
| return 0; | ||
| int rc = PyDict_GetItemRef(dict, key, presult); | ||
| PyObject *item = *presult; | ||
| if (item && PyWeakref_CheckProxy(item)){ | ||
| rc = PyWeakref_GetRef(item, presult); | ||
| Py_DECREF(item); | ||
| } | ||
| if (!PyWeakref_CheckProxy(item)){ | ||
| *presult = Py_NewRef(item); | ||
| return 0; | ||
| } | ||
| PyObject *ref; | ||
| if (PyWeakref_GetRef(item, &ref) < 0){ | ||
| return -1; | ||
| } | ||
| // ref is NULL if the referenced object was destroyed | ||
| *presult = ref; | ||
| return 0; | ||
| return rc; | ||
| } | ||
| /******************************************************************/ | ||
| @@ -565,18 +552,19 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt | ||
| dict->paramfunc = StructUnionType_paramfunc; | ||
| fields = PyDict_GetItemWithError((PyObject *)dict, &_Py_ID(_fields_)); | ||
| if (PyDict_GetItemRef((PyObject *)dict, &_Py_ID(_fields_), &fields) < 0){ | ||
| Py_DECREF(result); | ||
| return NULL; | ||
| } | ||
| if (fields){ | ||
| if (PyObject_SetAttr((PyObject *)result, &_Py_ID(_fields_), fields) < 0){ | ||
| Py_DECREF(result); | ||
| Py_DECREF(fields); | ||
| return NULL; | ||
| } | ||
| Py_DECREF(fields); | ||
| return (PyObject *)result; | ||
| } | ||
| else if (PyErr_Occurred()){ | ||
| Py_DECREF(result); | ||
| return NULL; | ||
| } | ||
| else{ | ||
| StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base); | ||
| @@ -1110,11 +1098,15 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||
| stgdict->paramfunc = PyCPointerType_paramfunc; | ||
| stgdict->flags |= TYPEFLAG_ISPOINTER; | ||
| proto = PyDict_GetItemWithError(typedict, &_Py_ID(_type_)); /* Borrowed ref */ | ||
| if (PyDict_GetItemRef(typedict, &_Py_ID(_type_), &proto) < 0){ | ||
| Py_DECREF((PyObject *)stgdict); | ||
| return NULL; | ||
| } | ||
| if (proto){ | ||
| StgDictObject *itemdict; | ||
| const char *current_format; | ||
| if (-1 == PyCPointerType_SetProto(stgdict, proto)){ | ||
| Py_DECREF(proto); | ||
| Py_DECREF((PyObject *)stgdict); | ||
| return NULL; | ||
| } | ||
| @@ -1134,15 +1126,12 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||
| } else{ | ||
| stgdict->format = _ctypes_alloc_format_string("&", current_format); | ||
| } | ||
| Py_DECREF(proto); | ||
| if (stgdict->format == NULL){ | ||
| Py_DECREF((PyObject *)stgdict); | ||
| return NULL; | ||
| } | ||
| } | ||
| else if (PyErr_Occurred()){ | ||
| Py_DECREF((PyObject *)stgdict); | ||
| return NULL; | ||
| } | ||
| /* create the new instance (which is a class, | ||
| since we are a metatype!) */ | ||
| @@ -2461,58 +2450,61 @@ make_funcptrtype_dict(StgDictObject *stgdict) | ||
| stgdict->getfunc = NULL; | ||
| stgdict->ffi_type_pointer = ffi_type_pointer; | ||
| ob = PyDict_GetItemWithError((PyObject *)stgdict, &_Py_ID(_flags_)); | ||
| if (PyDict_GetItemRef((PyObject *)stgdict, &_Py_ID(_flags_), &ob) < 0){ | ||
| return -1; | ||
| } | ||
| if (!ob || !PyLong_Check(ob)){ | ||
| if (!PyErr_Occurred()){ | ||
| PyErr_SetString(PyExc_TypeError, | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "class must define _flags_ which must be an integer"); | ||
| } | ||
| Py_XDECREF(ob); | ||
| return -1; | ||
| } | ||
| stgdict->flags = PyLong_AsUnsignedLongMask(ob) | TYPEFLAG_ISPOINTER; | ||
| Py_DECREF(ob); | ||
| /* _argtypes_ is optional... */ | ||
| ob = PyDict_GetItemWithError((PyObject *)stgdict, &_Py_ID(_argtypes_)); | ||
| if (PyDict_GetItemRef((PyObject *)stgdict, &_Py_ID(_argtypes_), &ob) < 0){ | ||
| return -1; | ||
| } | ||
| if (ob){ | ||
| converters = converters_from_argtypes(ob); | ||
| if (!converters) | ||
| if (!converters){ | ||
| Py_DECREF(ob); | ||
| return -1; | ||
| stgdict->argtypes = Py_NewRef(ob); | ||
| } | ||
| stgdict->argtypes = ob; | ||
| stgdict->converters = converters; | ||
| } | ||
| else if (PyErr_Occurred()){ | ||
| if (PyDict_GetItemRef((PyObject *)stgdict, &_Py_ID(_restype_), &ob) < 0){ | ||
| return -1; | ||
| } | ||
| ob = PyDict_GetItemWithError((PyObject *)stgdict, &_Py_ID(_restype_)); | ||
| if (ob){ | ||
| if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)){ | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "_restype_ must be a type, a callable, or None"); | ||
| Py_DECREF(ob); | ||
| return -1; | ||
| } | ||
| stgdict->restype = Py_NewRef(ob); | ||
| stgdict->restype = ob; | ||
| if (PyObject_GetOptionalAttr(ob, &_Py_ID(_check_retval_), | ||
| &stgdict->checker) < 0) | ||
| { | ||
| return -1; | ||
| } | ||
| } | ||
| else if (PyErr_Occurred()){ | ||
| /* XXX later, maybe. | ||
| if (PyDict_GetItemRef((PyObject *)stgdict, &_Py _ID(_errcheck_), &ob) < 0){ | ||
| return -1; | ||
| } | ||
| /* XXX later, maybe. | ||
| ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__errcheck_); | ||
| if (ob){ | ||
| if (!PyCallable_Check(ob)){ | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "_errcheck_ must be callable"); | ||
| Py_DECREF(ob); | ||
| return -1; | ||
| } | ||
| stgdict->errcheck = Py_NewRef(ob); | ||
| } | ||
| else if (PyErr_Occurred()){ | ||
| return -1; | ||
| stgdict->errcheck = ob; | ||
| } | ||
| */ | ||
| return 0; | ||
| @@ -3812,13 +3804,12 @@ _get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObje | ||
| return Py_NewRef(v); | ||
| } | ||
| if (kwds && name){ | ||
| v = PyDict_GetItemWithError(kwds, name); | ||
| if (PyDict_GetItemRef(kwds, name, &v) < 0){ | ||
| return NULL; | ||
| } | ||
| if (v){ | ||
| ++*pindex; | ||
| return Py_NewRef(v); | ||
| } | ||
| else if (PyErr_Occurred()){ | ||
| return NULL; | ||
| return v; | ||
| } | ||
| } | ||
| if (defval){ | ||
| @@ -4870,15 +4861,12 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length) | ||
| return NULL; | ||
| PyObject *result; | ||
| if (_PyDict_GetItemProxy(cache, key, &result) < 0){ | ||
| Py_DECREF(key); | ||
| return NULL; | ||
| } | ||
| if (result){ | ||
| if (_PyDict_GetItemProxy(cache, key, &result) != 0){ | ||
| // found or error | ||
| Py_DECREF(key); | ||
| return result; | ||
| } | ||
| // not found | ||
serhiy-storchaka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| if (!PyType_Check(itemtype)){ | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "Expected a type object"); | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_PyDict_GetItemProxy() now returns 1 if PyDict_GetItemRef() returns 1 and PyWeakref_CheckProxy() is false. It's only called once with
if (_PyDict_GetItemProxy(cache, key, &result) != 0){. So it's ok.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it has the same semantic as
PyDict_GetItemRef().1 -- found, returns a strong reference
0 -- not found (or was destroyed)
-1 -- error