Skip to content

Commit 5bc7101

Browse files
committed
factor out a helper
1 parent 5018200 commit 5bc7101

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,28 @@ c_void_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
22222222
return NULL;
22232223
}
22242224

2225+
static int
2226+
set_stginfo_ffi_type_pointer(StgInfo *stginfo, struct fielddesc *fmt)
2227+
{
2228+
if (!fmt->pffi_type->elements) {
2229+
stginfo->ffi_type_pointer = *fmt->pffi_type;
2230+
}
2231+
else {
2232+
assert(fmt->pffi_type->type == FFI_TYPE_COMPLEX);
2233+
const size_t els_size = sizeof(2 * sizeof(ffi_type *));
2234+
stginfo->ffi_type_pointer.size = fmt->pffi_type->size;
2235+
stginfo->ffi_type_pointer.alignment = fmt->pffi_type->alignment;
2236+
stginfo->ffi_type_pointer.type = fmt->pffi_type->type;
2237+
stginfo->ffi_type_pointer.elements = PyMem_Malloc(els_size);
2238+
if (!stginfo->ffi_type_pointer.elements) {
2239+
return -1;
2240+
}
2241+
memcpy(stginfo->ffi_type_pointer.elements,
2242+
fmt->pffi_type->elements, els_size);
2243+
}
2244+
return 0;
2245+
}
2246+
22252247
static PyMethodDef c_void_p_methods[] = {C_VOID_P_FROM_PARAM_METHODDEF {0}};
22262248
static PyMethodDef c_char_p_methods[] = {C_CHAR_P_FROM_PARAM_METHODDEF {0}};
22272249
static PyMethodDef c_wchar_p_methods[] = {C_WCHAR_P_FROM_PARAM_METHODDEF {0}};
@@ -2266,23 +2288,9 @@ static PyObject *CreateSwappedType(ctypes_state *st, PyTypeObject *type,
22662288
Py_DECREF(result);
22672289
return NULL;
22682290
}
2269-
2270-
if (!fmt->pffi_type->elements) {
2271-
stginfo->ffi_type_pointer = *fmt->pffi_type;
2272-
}
2273-
else {
2274-
assert(fmt->pffi_type->type == FFI_TYPE_COMPLEX);
2275-
const size_t els_size = sizeof(2 * sizeof(ffi_type *));
2276-
stginfo->ffi_type_pointer.size = fmt->pffi_type->size;
2277-
stginfo->ffi_type_pointer.alignment = fmt->pffi_type->alignment;
2278-
stginfo->ffi_type_pointer.type = fmt->pffi_type->type;
2279-
stginfo->ffi_type_pointer.elements = PyMem_Malloc(els_size);
2280-
if (!stginfo->ffi_type_pointer.elements) {
2281-
Py_DECREF(result);
2282-
return PyErr_NoMemory();
2283-
}
2284-
memcpy(stginfo->ffi_type_pointer.elements,
2285-
fmt->pffi_type->elements, els_size);
2291+
if (set_stginfo_ffi_type_pointer(stginfo, fmt)) {
2292+
Py_DECREF(result);
2293+
return PyErr_NoMemory();
22862294
}
22872295
stginfo->align = fmt->pffi_type->alignment;
22882296
stginfo->length = 0;
@@ -2378,23 +2386,9 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
23782386
if (!stginfo) {
23792387
goto error;
23802388
}
2381-
2382-
if (!fmt->pffi_type->elements) {
2383-
stginfo->ffi_type_pointer = *fmt->pffi_type;
2384-
}
2385-
else {
2386-
assert(fmt->pffi_type->type == FFI_TYPE_COMPLEX);
2387-
const size_t els_size = sizeof(2 * sizeof(ffi_type *));
2388-
stginfo->ffi_type_pointer.size = fmt->pffi_type->size;
2389-
stginfo->ffi_type_pointer.alignment = fmt->pffi_type->alignment;
2390-
stginfo->ffi_type_pointer.type = fmt->pffi_type->type;
2391-
stginfo->ffi_type_pointer.elements = PyMem_Malloc(els_size);
2392-
if (!stginfo->ffi_type_pointer.elements) {
2393-
PyErr_NoMemory();
2394-
goto error;
2395-
}
2396-
memcpy(stginfo->ffi_type_pointer.elements,
2397-
fmt->pffi_type->elements, els_size);
2389+
if (set_stginfo_ffi_type_pointer(stginfo, fmt)) {
2390+
PyErr_NoMemory();
2391+
goto error;
23982392
}
23992393
stginfo->align = fmt->pffi_type->alignment;
24002394
stginfo->length = 0;

0 commit comments

Comments
 (0)