8000 PyCSimpleType_new() -> init() · python/cpython@8bb0d97 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8bb0d97

Browse files
authored
PyCSimpleType_new() -> init()
1 parent 5f3cc90 commit 8bb0d97

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,16 @@ PyCSimpleType_paramfunc(CDataObject *self)
20312031

20322032
static PyObject *
20332033
PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2034+
{
2035+
PyObject *result = PyType_Type.tp_new(type, args, kwds);
2036+
#if 0
2037+
printf("new: %p %p\n", type, result);
2038+
#endif
2039+
return result;
2040+
}
2041+
2042+
static int
2043+
PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
20342044
{
20352045
PyTypeObject *result;
20362046
StgDictObject *stgdict;
@@ -2042,20 +2052,21 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20422052

20432053
/* create the new instance (which is a class,
20442054
since we are a metatype!) */
2045-
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
2046-
if (result == NULL)
2047-
return NULL;
2055+
result = (PyTypeObject *)self; // by PyType_Type.tp_new()
2056+
PyTypeObject *type = Py_TYPE(self);
2057+
#if 0
2058+
printf("ini: %p %p\n\n", type, result);
2059+
#endif
20482060

20492061
if (PyObject_GetOptionalAttr((PyObject *)result, &_Py_ID(_type_), &proto) < 0) {
2050-
return NULL;
2062+
return -1;
20512063
}
20522064
if (!proto) {
20532065
PyErr_SetString(PyExc_AttributeError,
20542066
"class must define a '_type_' attribute");
20552067
error:
20562068
Py_XDECREF(proto);
2057-
Py_DECREF(result);
2058-
return NULL;
2069+
return -1;
20592070
}
20602071
if (PyUnicode_Check(proto)) {
20612072
proto_str = PyUnicode_AsUTF8AndSize(proto, &proto_len);
@@ -2103,10 +2114,9 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
21032114
stgdict->format = _ctypes_alloc_format_string_for_type(proto_str[0], 0);
21042115
#endif
21052116
if (stgdict->format == NULL) {
2106-
10000 Py_DECREF(result);
21072117
Py_DECREF(proto);
21082118
Py_DECREF((PyObject *)stgdict);
2109-
return NULL;
2119+
return -1;
21102120
}
21112121

21122122
stgdict->paramfunc = PyCSimpleType_paramfunc;
@@ -2122,9 +2132,8 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
21222132

21232133
/* replace the class dict by our updated spam dict */
21242134
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
2125-
Py_DECREF(result);
21262135
Py_DECREF((PyObject *)stgdict);
2127-
return NULL;
2136+
return -1;
21282137
}
21292138
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
21302139

@@ -2161,16 +2170,14 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
21612170
int x;
21622171
meth = PyDescr_NewClassMethod(result, ml);
21632172
if (!meth) {
2164-
Py_DECREF(result);
2165-
return NULL;
2173+
return -1;
21662174
}
21672175
x = PyDict_SetItemString(result->tp_dict,
21682176
ml->ml_name,
21692177
meth);
21702178
Py_DECREF(meth);
21712179
if (x == -1) {
2172-
Py_DECREF(result);
2173-
return NULL;
2180+
return -1;
21742181
}
21752182
}
21762183
}
@@ -2180,8 +2187,7 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
21802187
proto, fmt);
21812188
StgDictObject *sw_dict;
21822189
if (swapped == NULL) {
2183-
Py_DECREF(result);
2184-
return NULL;
2190+
return -1;
21852191
}
21862192
sw_dict = PyType_stgdict(swapped);
21872193
#ifdef WORDS_BIGENDIAN
@@ -2201,12 +2207,11 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
22012207
#endif
22022208
Py_DECREF(swapped);
22032209
if (PyErr_Occurred()) {
2204-
Py_DECREF(result);
2205-
return NULL;
2210+
return -1;
22062211
}
22072212
};
22082213

2209-
return (PyObject *)result;
2214+
return 0;
22102215
}
22112216

22122217
/*
@@ -2328,7 +2333,7 @@ PyTypeObject PyCSimpleType_Type = {
23282333
0, /* tp_descr_get */
23292334
0, /* tp_descr_set */
23302335
0, /* tp_dictoffset */
2331-
0, /* tp_init */
2336+
(initproc)PyCSimpleType_init, /* tp_init */
23322337
0, /* tp_alloc */
23332338
PyCSimpleType_new, /* tp_new */
23342339
0, /* tp_free */

0 commit comments

Comments
 (0)
0