Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5cc2c8c

Browse files
committed
Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
1 parent 05473ed commit 5cc2c8c

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

Objects/classobject.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -428,27 +428,48 @@ PyClass_IsSubclass(PyObject *class, PyObject *base)
428428
/* Instance objects */
429429

430430
PyObject *
431-
PyInstance_New(PyObject *class, PyObject *arg, PyObject *kw)
431+
PyInstance_NewRaw(PyObject *klass, PyObject *dict)
432432
{
433-
register PyInstanceObject *inst;
434-
PyObject *init;
435-
static PyObject *initstr;
436-
if (!PyClass_Check(class)) {
433+
PyInstanceObject *inst;
434+
435+
if (!PyClass_Check(klass)) {
437436
PyErr_BadInternalCall();
438437
return NULL;
439438
}
439+
if (dict == NULL) {
440+
dict = PyDict_New();
441+
if (dict == NULL)
442+
return NULL;
443+
}
444+
else {
445+
if (!PyDict_Check(dict)) {
446+
PyErr_BadInternalCall();
447+
return NULL;
448+
}
449+
Py_INCREF(dict);
450+
}
440451
inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type);
441-
if (inst == NULL)
442-
return NULL;
443-
inst->in_dict = PyDict_New();
444-
if (inst->in_dict == NULL) {
445-
inst = (PyInstanceObject *) PyObject_AS_GC(inst);
446-
PyObject_DEL(inst);
452+
if (inst == NULL) {
453+
Py_DECREF(dict);
447454
return NULL;
448455
}
449-
Py_INCREF(class);
450-
inst->in_class = (PyClassObject *)class;
456+
Py_INCREF(klass);
457+
inst->in_class = (PyClassObject *)klass;
458+
inst->in_dict = dict;
451459
PyObject_GC_Init(inst);
460+
return (PyObject *)inst;
461+
}
462+
463+
PyObject *
464+
PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw)
465+
{
466+
register PyInstanceObject *inst;
467+
PyObject *init;
468+
static PyObject *initstr;
469+
470+
inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL);
471+
if (inst == NULL)
472+
return NULL;
452473
if (initstr == NULL)
453474
initstr = PyString_InternFromString("__init__");
454475
init = instance_getattr2(inst, initstr);

0 commit comments

Comments
 (0)