new_instance(): Use PyInstance_NewRaw() instead of knowing too much

about the internal initialization of instance objects.  Make the
    dict parameter optional, and allow None as equivalent to omission.
This commit is contained in:
Fred Drake 2001-01-28 03:55:09 +00:00
parent 5cc2c8c3c8
commit ceb2bff09e
1 changed files with 17 additions and 15 deletions

View File

@ -5,27 +5,29 @@
#include "compile.h" #include "compile.h"
static char new_instance_doc[] = static char new_instance_doc[] =
"Create an instance object from (CLASS, DICT) without calling its __init__()."; "Create an instance object from (CLASS [, DICT]) without calling its\n\
__init__() method. DICT must be a dictionary or None.";
static PyObject * static PyObject *
new_instance(PyObject* unused, PyObject* args) new_instance(PyObject* unused, PyObject* args)
{ {
PyObject* klass; PyObject *klass;
PyObject *dict; PyObject *dict = NULL;
PyInstanceObject *inst;
if (!PyArg_ParseTuple(args, "O!O!:instance", if (!PyArg_ParseTuple(args, "O!|O:instance",
&PyClass_Type, &klass, &PyClass_Type, &klass, &dict))
&PyDict_Type, &dict))
return NULL; return NULL;
inst = PyObject_New(PyInstanceObject, &PyInstance_Type);
if (inst == NULL) if (dict == Py_None)
dict = NULL;
else if (dict == NULL)
/* do nothing */;
else if (!PyDict_Check(dict)) {
PyErr_SetString(PyExc_TypeError,
"new.instance() second arg must be dictionary or None");
return NULL; return NULL;
Py_INCREF(klass); }
Py_INCREF(dict); return PyInstance_NewRaw(klass, dict);
inst->in_class = (PyClassObject *)klass;
inst->in_dict = dict;
PyObject_GC_Init(inst);
return (PyObject *)inst;
} }
static char new_im_doc[] = static char new_im_doc[] =