]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
new_instance(): Use PyInstance_NewRaw() instead of knowing too much
authorFred Drake <fdrake@acm.org>
Sun, 28 Jan 2001 03:55:09 +0000 (03:55 +0000)
committerFred Drake <fdrake@acm.org>
Sun, 28 Jan 2001 03:55:09 +0000 (03:55 +0000)
    about the internal initialization of instance objects.  Make the
    dict parameter optional, and allow None as equivalent to omission.

Modules/newmodule.c

index 06b7d823b8cb65515903f658731aede63e8503fb..7c7bee713bcd5717f371b23883c4e7a049659da8 100644 (file)
@@ -5,27 +5,29 @@
 #include "compile.h"
 
 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 *
 new_instance(PyObject* unused, PyObject* args)
 {
-       PyObject* klass;
-       PyObject *dict;
-       PyInstanceObject *inst;
-       if (!PyArg_ParseTuple(args, "O!O!:instance",
-                             &PyClass_Type, &klass,
-                             &PyDict_Type, &dict))
+       PyObject *klass;
+       PyObject *dict = NULL;
+
+       if (!PyArg_ParseTuple(args, "O!|O:instance",
+                             &PyClass_Type, &klass, &dict))
                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;
-       Py_INCREF(klass);
-       Py_INCREF(dict);
-       inst->in_class = (PyClassObject *)klass;
-       inst->in_dict = dict;
-       PyObject_GC_Init(inst);
-       return (PyObject *)inst;
+       }
+       return PyInstance_NewRaw(klass, dict);
 }
 
 static char new_im_doc[] =