Add forgotten initialization. Fixes bug #120994, "Traceback with
[python/dscho.git] / Modules / newmodule.c
blob21b82ef824de3cf0ea2052ac856acd81732a064c
2 /* Module new -- create new objects of various types */
4 #include "Python.h"
5 #include "compile.h"
7 static char new_instance_doc[] =
8 "Create an instance object from (CLASS, DICT) without calling its __init__().";
10 static PyObject *
11 new_instance(PyObject* unused, PyObject* args)
13 PyObject* klass;
14 PyObject *dict;
15 PyInstanceObject *inst;
16 if (!PyArg_ParseTuple(args, "O!O!:instance",
17 &PyClass_Type, &klass,
18 &PyDict_Type, &dict))
19 return NULL;
20 inst = PyObject_New(PyInstanceObject, &PyInstance_Type);
21 if (inst == NULL)
22 return NULL;
23 Py_INCREF(klass);
24 Py_INCREF(dict);
25 inst->in_class = (PyClassObject *)klass;
26 inst->in_dict = dict;
27 PyObject_GC_Init(inst);
28 return (PyObject *)inst;
31 static char new_im_doc[] =
32 "Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
34 static PyObject *
35 new_instancemethod(PyObject* unused, PyObject* args)
37 PyObject* func;
38 PyObject* self;
39 PyObject* classObj;
41 if (!PyArg_ParseTuple(args, "OOO!:instancemethod",
42 &func,
43 &self,
44 &PyClass_Type, &classObj))
45 return NULL;
46 if (!PyCallable_Check(func)) {
47 PyErr_SetString(PyExc_TypeError,
48 "first argument must be callable");
49 return NULL;
51 if (self == Py_None)
52 self = NULL;
53 else if (!PyInstance_Check(self)) {
54 PyErr_SetString(PyExc_TypeError,
55 "second argument must be instance or None");
56 return NULL;
58 return PyMethod_New(func, self, classObj);
61 static char new_function_doc[] =
62 "Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).";
64 static PyObject *
65 new_function(PyObject* unused, PyObject* args)
67 PyObject* code;
68 PyObject* globals;
69 PyObject* name = Py_None;
70 PyObject* defaults = Py_None;
71 PyFunctionObject* newfunc;
73 if (!PyArg_ParseTuple(args, "O!O!|OO!:function",
74 &PyCode_Type, &code,
75 &PyDict_Type, &globals,
76 &name,
77 &PyTuple_Type, &defaults))
78 return NULL;
79 if (name != Py_None && !PyString_Check(name)) {
80 PyErr_SetString(PyExc_TypeError,
81 "arg 3 (name) must be None or string");
82 return NULL;
85 newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
86 if (newfunc == NULL)
87 return NULL;
89 if (name != Py_None) {
90 Py_XINCREF(name);
91 Py_XDECREF(newfunc->func_name);
92 newfunc->func_name = name;
94 if (defaults != Py_None) {
95 Py_XINCREF(defaults);
96 Py_XDECREF(newfunc->func_defaults);
97 newfunc->func_defaults = defaults;
100 return (PyObject *)newfunc;
103 static char new_code_doc[] =
104 "Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB).";
106 static PyObject *
107 new_code(PyObject* unused, PyObject* args)
109 int argcount;
110 int nlocals;
111 int stacksize;
112 int flags;
113 PyObject* code;
114 PyObject* consts;
115 PyObject* names;
116 PyObject* varnames;
117 PyObject* filename;
118 PyObject* name;
119 int firstlineno;
120 PyObject* lnotab;
121 PyBufferProcs *pb;
123 if (!PyArg_ParseTuple(args, "iiiiOO!O!O!SSiS:code",
124 &argcount, &nlocals, &stacksize, &flags,
125 &code,
126 &PyTuple_Type, &consts,
127 &PyTuple_Type, &names,
128 &PyTuple_Type, &varnames,
129 &filename, &name,
130 &firstlineno, &lnotab))
131 return NULL;
133 pb = code->ob_type->tp_as_buffer;
134 if (pb == NULL ||
135 pb->bf_getreadbuffer == NULL ||
136 pb->bf_getsegcount == NULL ||
137 (*pb->bf_getsegcount)(code, NULL) != 1)
139 PyErr_SetString(PyExc_TypeError,
140 "bytecode object must be a single-segment read-only buffer");
141 return NULL;
144 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
145 code, consts, names, varnames,
146 filename, name, firstlineno, lnotab);
149 static char new_module_doc[] =
150 "Create a module object from (NAME).";
152 static PyObject *
153 new_module(PyObject* unused, PyObject* args)
155 char *name;
157 if (!PyArg_ParseTuple(args, "s:module", &name))
158 return NULL;
159 return PyModule_New(name);
162 static char new_class_doc[] =
163 "Create a class object from (NAME, BASE_CLASSES, DICT).";
165 static PyObject *
166 new_class(PyObject* unused, PyObject* args)
168 PyObject * name;
169 PyObject * classes;
170 PyObject * dict;
172 if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes,
173 &PyDict_Type, &dict))
174 return NULL;
175 return PyClass_New(classes, dict, name);
178 static PyMethodDef new_methods[] = {
179 {"instance", new_instance,
180 METH_VARARGS, new_instance_doc},
181 {"instancemethod", new_instancemethod,
182 METH_VARARGS, new_im_doc},
183 {"function", new_function,
184 METH_VARARGS, new_function_doc},
185 {"code", new_code,
186 METH_VARARGS, new_code_doc},
187 {"module", new_module,
188 METH_VARARGS, new_module_doc},
189 {"classobj", new_class,
190 METH_VARARGS, new_class_doc},
191 {NULL, NULL} /* sentinel */
194 char new_doc[] =
195 "Functions to create new objects used by the interpreter.\n\
197 You need to know a great deal about the interpreter to use this!";
199 DL_EXPORT(void)
200 initnew(void)
202 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
203 PYTHON_API_VERSION);