-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-37140: Fix StructUnionType_paramfunc() #15612
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
Merged
Changes from all commits
Commits
Show all changes
3 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
5 changes: 5 additions & 0 deletions
5
Misc/NEWS.d/next/Library/2019-08-30-11-21-10.bpo-37140.cFAX-a.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,5 @@ | ||
Fix a ctypes regression of Python 3.8. When a ctypes.Structure is passed by | ||
copy to a function, ctypes internals created a temporary object which had | ||
the side effect of calling the structure finalizer (__del__) twice. The | ||
Python semantics requires a finalizer to be called exactly once. Fix ctypes | ||
internals to no longer call the finalizer twice. |
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 |
---|---|---|
|
@@ -392,6 +392,35 @@ _ctypes_alloc_format_string_with_shape(int ndim, const Py_ssize_t *shape, | |
return result; | ||
} | ||
|
||
/* StructParamObject and StructParam_Type are used in _ctypes_callproc() | ||
for argument.keep to call PyMem_Free(ptr) on Py_DECREF(argument). | ||
|
||
StructUnionType_paramfunc() creates such object when a ctypes Structure is | ||
passed by copy to a C function. */ | ||
typedef struct { | ||
PyObject_HEAD | ||
void *ptr; | ||
} StructParamObject; | ||
|
||
|
||
static void | ||
StructParam_dealloc(PyObject *myself) | ||
{ | ||
StructParamObject *self = (StructParamObject *)myself; | ||
PyMem_Free(self->ptr); | ||
Py_TYPE(self)->tp_free(myself); | ||
} | ||
|
||
|
||
static PyTypeObject StructParam_Type = { | ||
PyVarObject_HEAD_INIT(NULL, 0) | ||
.tp_name = "_ctypes.StructParam_Type", | ||
.tp_basicsize = sizeof(StructParamObject), | ||
.tp_dealloc = StructParam_dealloc, | ||
.tp_flags = Py_TPFLAGS_DEFAULT, | ||
}; | ||
|
||
|
||
/* | ||
PyCStructType_Type - a meta type/class. Creating a new class using this one as | ||
__metaclass__ will call the constructor StructUnionType_new. It replaces the | ||
|
@@ -403,35 +432,47 @@ static PyCArgObject * | |
StructUnionType_paramfunc(CDataObject *self) | ||
{ | ||
PyCArgObject *parg; | ||
CDataObject *copied_self; | ||
PyObject *obj; | ||
StgDictObject *stgdict; | ||
void *ptr; | ||
|
||
if ((size_t)self->b_size > sizeof(void*)) { | ||
void *new_ptr = PyMem_Malloc(self->b_size); | ||
if (new_ptr == NULL) | ||
ptr = PyMem_Malloc(self->b_size); | ||
if (ptr == NULL) { | ||
return NULL; | ||
memcpy(new_ptr, self->b_ptr, self->b_size); | ||
copied_self = (CDataObject *)PyCData_AtAddress( | ||
(PyObject *)Py_TYPE(self), new_ptr); | ||
copied_self->b_needsfree = 1; | ||
} | ||
memcpy(ptr, self->b_ptr, self->b_size); | ||
|
||
/* Create a Python object which calls PyMem_Free(ptr) in | ||
its deallocator. The object will be destroyed | ||
at _ctypes_callproc() cleanup. */ | ||
obj = (&StructParam_Type)->tp_alloc(&StructParam_Type, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/obj/arg_obj/ to increase readability ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose "obj" name to get "parg->obj = obj" which looks nice to me :-) |
||
if (obj == NULL) { | ||
PyMem_Free(ptr); | ||
return NULL; | ||
} | ||
|
||
StructParamObject *struct_param = (StructParamObject *)obj; | ||
struct_param->ptr = ptr; | ||
} else { | ||
copied_self = self; | ||
Py_INCREF(copied_self); | ||
ptr = self->b_ptr; | ||
obj = (PyObject *)self; | ||
Py_INCREF(obj); | ||
} | ||
|
||
parg = PyCArgObject_new(); | ||
if (parg == NULL) { | ||
Py_DECREF(copied_self); | ||
Py_DECREF(obj); | ||
return NULL; | ||
} | ||
|
||
parg->tag = 'V'; | ||
stgdict = PyObject_stgdict((PyObject *)copied_self); | ||
stgdict = PyObject_stgdict((PyObject *)self); | ||
assert(stgdict); /* Cannot be NULL for structure/union instances */ | ||
parg->pffi_type = &stgdict->ffi_type_pointer; | ||
parg->value.p = copied_self->b_ptr; | ||
parg->size = copied_self->b_size; | ||
parg->obj = (PyObject *)copied_self; | ||
parg->value.p = ptr; | ||
parg->size = self->b_size; | ||
parg->obj = obj; | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return parg; | ||
} | ||
|
||
|
@@ -5700,6 +5741,10 @@ PyInit__ctypes(void) | |
if (PyType_Ready(&DictRemover_Type) < 0) | ||
return NULL; | ||
|
||
if (PyType_Ready(&StructParam_Type) < 0) { | ||
return NULL; | ||
} | ||
|
||
#ifdef MS_WIN32 | ||
if (create_comerror() < 0) | ||
return NULL; | ||
|
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.
<3 the test case