blob: 7b91fb8bc3ef8f0226e10ccead87f17c9ba7cfc8 [file] [log] [blame]
Guido van Rossumf9888eb1995-01-07 11:50:36 +00001
2/* Module new -- create new objects of various types */
3
Barry Warsawa6b7c711996-12-10 16:25:56 +00004#include "Python.h"
Guido van Rossum34162a11994-05-23 12:37:57 +00005#include "compile.h"
Guido van Rossum34162a11994-05-23 12:37:57 +00006
Guido van Rossum26b310a1996-06-17 16:56:56 +00007static char new_instance_doc[] =
Fred Drakeceb2bff2001-01-28 03:55:09 +00008"Create an instance object from (CLASS [, DICT]) without calling its\n\
9__init__() method. DICT must be a dictionary or None.";
Guido van Rossum26b310a1996-06-17 16:56:56 +000010
Barry Warsawa6b7c711996-12-10 16:25:56 +000011static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000012new_instance(PyObject* unused, PyObject* args)
Guido van Rossum26b310a1996-06-17 16:56:56 +000013{
Fred Drakeceb2bff2001-01-28 03:55:09 +000014 PyObject *klass;
15 PyObject *dict = NULL;
16
17 if (!PyArg_ParseTuple(args, "O!|O:instance",
18 &PyClass_Type, &klass, &dict))
Guido van Rossum26b310a1996-06-17 16:56:56 +000019 return NULL;
Fred Drakeceb2bff2001-01-28 03:55:09 +000020
21 if (dict == Py_None)
22 dict = NULL;
23 else if (dict == NULL)
24 /* do nothing */;
25 else if (!PyDict_Check(dict)) {
26 PyErr_SetString(PyExc_TypeError,
27 "new.instance() second arg must be dictionary or None");
Guido van Rossum26b310a1996-06-17 16:56:56 +000028 return NULL;
Fred Drakeceb2bff2001-01-28 03:55:09 +000029 }
30 return PyInstance_NewRaw(klass, dict);
Guido van Rossum26b310a1996-06-17 16:56:56 +000031}
32
Guido van Rossumf9888eb1995-01-07 11:50:36 +000033static char new_im_doc[] =
34"Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
Guido van Rossum34162a11994-05-23 12:37:57 +000035
Barry Warsawa6b7c711996-12-10 16:25:56 +000036static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000037new_instancemethod(PyObject* unused, PyObject* args)
Guido van Rossum34162a11994-05-23 12:37:57 +000038{
Barry Warsawa6b7c711996-12-10 16:25:56 +000039 PyObject* func;
40 PyObject* self;
41 PyObject* classObj;
Guido van Rossum34162a11994-05-23 12:37:57 +000042
Guido van Rossum43713e52000-02-29 13:59:29 +000043 if (!PyArg_ParseTuple(args, "OOO!:instancemethod",
Guido van Rossum7859f871998-07-08 14:58:16 +000044 &func,
45 &self,
Barry Warsawa6b7c711996-12-10 16:25:56 +000046 &PyClass_Type, &classObj))
Guido van Rossumf9888eb1995-01-07 11:50:36 +000047 return NULL;
Guido van Rossum7859f871998-07-08 14:58:16 +000048 if (!PyCallable_Check(func)) {
49 PyErr_SetString(PyExc_TypeError,
50 "first argument must be callable");
51 return NULL;
52 }
53 if (self == Py_None)
54 self = NULL;
55 else if (!PyInstance_Check(self)) {
56 PyErr_SetString(PyExc_TypeError,
57 "second argument must be instance or None");
58 return NULL;
59 }
Barry Warsawa6b7c711996-12-10 16:25:56 +000060 return PyMethod_New(func, self, classObj);
Guido van Rossumf9888eb1995-01-07 11:50:36 +000061}
62
63static char new_function_doc[] =
Fred Drake5428c7d2000-10-10 22:07:18 +000064"Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +000065
Barry Warsawa6b7c711996-12-10 16:25:56 +000066static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000067new_function(PyObject* unused, PyObject* args)
Guido van Rossum34162a11994-05-23 12:37:57 +000068{
Barry Warsawa6b7c711996-12-10 16:25:56 +000069 PyObject* code;
70 PyObject* globals;
71 PyObject* name = Py_None;
72 PyObject* defaults = Py_None;
73 PyFunctionObject* newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000074
Guido van Rossum1bff8832000-11-13 20:29:20 +000075 if (!PyArg_ParseTuple(args, "O!O!|OO!:function",
Barry Warsawa6b7c711996-12-10 16:25:56 +000076 &PyCode_Type, &code,
77 &PyDict_Type, &globals,
78 &name,
79 &PyTuple_Type, &defaults))
Guido van Rossumf9888eb1995-01-07 11:50:36 +000080 return NULL;
Guido van Rossum1bff8832000-11-13 20:29:20 +000081 if (name != Py_None && !PyString_Check(name)) {
82 PyErr_SetString(PyExc_TypeError,
83 "arg 3 (name) must be None or string");
84 return NULL;
85 }
Guido van Rossum34162a11994-05-23 12:37:57 +000086
Barry Warsawa6b7c711996-12-10 16:25:56 +000087 newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
Guido van Rossumf9888eb1995-01-07 11:50:36 +000088 if (newfunc == NULL)
89 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +000090
Barry Warsawa6b7c711996-12-10 16:25:56 +000091 if (name != Py_None) {
92 Py_XINCREF(name);
93 Py_XDECREF(newfunc->func_name);
Guido van Rossumf9888eb1995-01-07 11:50:36 +000094 newfunc->func_name = name;
95 }
Guido van Rossumb88679b1998-05-22 00:57:31 +000096 if (defaults != Py_None) {
Barry Warsawa6b7c711996-12-10 16:25:56 +000097 Py_XINCREF(defaults);
98 Py_XDECREF(newfunc->func_defaults);
Guido van Rossumb916faf1996-11-21 16:02:12 +000099 newfunc->func_defaults = defaults;
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000100 }
Guido van Rossum34162a11994-05-23 12:37:57 +0000101
Barry Warsawa6b7c711996-12-10 16:25:56 +0000102 return (PyObject *)newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +0000103}
Guido van Rossum34162a11994-05-23 12:37:57 +0000104
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000105static char new_code_doc[] =
Jeremy Hylton6fe0a822001-02-01 19:50:29 +0000106"Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING,\n"
107"CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB, FREEVARS,\n"
108"CELLVARS).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000109
Barry Warsawa6b7c711996-12-10 16:25:56 +0000110static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000111new_code(PyObject* unused, PyObject* args)
Guido van Rossum34162a11994-05-23 12:37:57 +0000112{
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000113 int argcount;
114 int nlocals;
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000115 int stacksize;
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000116 int flags;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000117 PyObject* code;
118 PyObject* consts;
119 PyObject* names;
120 PyObject* varnames;
Jeremy Hylton6fe0a822001-02-01 19:50:29 +0000121 PyObject* freevars = NULL;
122 PyObject* cellvars = NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000123 PyObject* filename;
124 PyObject* name;
Guido van Rossum463e55a1997-01-24 03:58:52 +0000125 int firstlineno;
126 PyObject* lnotab;
Guido van Rossumd076c731998-10-07 19:42:25 +0000127 PyBufferProcs *pb;
128
Jeremy Hylton6fe0a822001-02-01 19:50:29 +0000129 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000130 &argcount, &nlocals, &stacksize, &flags,
Barry Warsawa6b7c711996-12-10 16:25:56 +0000131 &code,
132 &PyTuple_Type, &consts,
133 &PyTuple_Type, &names,
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000134 &PyTuple_Type, &varnames,
Guido van Rossum463e55a1997-01-24 03:58:52 +0000135 &filename, &name,
Jeremy Hylton6fe0a822001-02-01 19:50:29 +0000136 &firstlineno, &lnotab,
137 &PyTuple_Type, &freevars,
138 &PyTuple_Type, &cellvars))
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000139 return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000140
Jeremy Hylton6fe0a822001-02-01 19:50:29 +0000141 if (freevars == NULL || cellvars == NULL) {
142 PyObject *empty = PyTuple_New(0);
143 if (empty == NULL)
144 return NULL;
145 if (freevars == NULL) {
146 freevars = empty;
147 Py_INCREF(freevars);
148 }
149 if (cellvars == NULL) {
150 cellvars = empty;
151 Py_INCREF(cellvars);
152 }
153 Py_DECREF(empty);
154 }
155
Guido van Rossumd076c731998-10-07 19:42:25 +0000156 pb = code->ob_type->tp_as_buffer;
157 if (pb == NULL ||
158 pb->bf_getreadbuffer == NULL ||
159 pb->bf_getsegcount == NULL ||
160 (*pb->bf_getsegcount)(code, NULL) != 1)
161 {
162 PyErr_SetString(PyExc_TypeError,
163 "bytecode object must be a single-segment read-only buffer");
164 return NULL;
165 }
166
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000167 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
Barry Warsawa6b7c711996-12-10 16:25:56 +0000168 code, consts, names, varnames,
Jeremy Hylton903f6542001-01-25 20:07:56 +0000169 freevars, cellvars, filename, name,
170 firstlineno, lnotab);
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000171}
Guido van Rossum34162a11994-05-23 12:37:57 +0000172
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000173static char new_module_doc[] =
174"Create a module object from (NAME).";
175
Barry Warsawa6b7c711996-12-10 16:25:56 +0000176static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000177new_module(PyObject* unused, PyObject* args)
Guido van Rossum34162a11994-05-23 12:37:57 +0000178{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000179 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000180
Guido van Rossum43713e52000-02-29 13:59:29 +0000181 if (!PyArg_ParseTuple(args, "s:module", &name))
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000182 return NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000183 return PyModule_New(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000184}
Guido van Rossum34162a11994-05-23 12:37:57 +0000185
Guido van Rossumb28911c1996-01-12 01:38:22 +0000186static char new_class_doc[] =
187"Create a class object from (NAME, BASE_CLASSES, DICT).";
188
Barry Warsawa6b7c711996-12-10 16:25:56 +0000189static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000190new_class(PyObject* unused, PyObject* args)
Guido van Rossumb28911c1996-01-12 01:38:22 +0000191{
Barry Warsawa6b7c711996-12-10 16:25:56 +0000192 PyObject * name;
193 PyObject * classes;
194 PyObject * dict;
Guido van Rossumb28911c1996-01-12 01:38:22 +0000195
Guido van Rossum43713e52000-02-29 13:59:29 +0000196 if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes,
Barry Warsawa6b7c711996-12-10 16:25:56 +0000197 &PyDict_Type, &dict))
Guido van Rossumb28911c1996-01-12 01:38:22 +0000198 return NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000199 return PyClass_New(classes, dict, name);
Guido van Rossumb28911c1996-01-12 01:38:22 +0000200}
201
Barry Warsawa6b7c711996-12-10 16:25:56 +0000202static PyMethodDef new_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000203 {"instance", new_instance,
204 METH_VARARGS, new_instance_doc},
205 {"instancemethod", new_instancemethod,
206 METH_VARARGS, new_im_doc},
207 {"function", new_function,
208 METH_VARARGS, new_function_doc},
209 {"code", new_code,
210 METH_VARARGS, new_code_doc},
211 {"module", new_module,
212 METH_VARARGS, new_module_doc},
213 {"classobj", new_class,
214 METH_VARARGS, new_class_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000215 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000216};
217
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000218char new_doc[] =
219"Functions to create new objects used by the interpreter.\n\
220\n\
221You need to know a great deal about the interpreter to use this!";
222
Guido van Rossum3886bb61998-12-04 18:50:17 +0000223DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000224initnew(void)
Guido van Rossum34162a11994-05-23 12:37:57 +0000225{
Barry Warsawa6b7c711996-12-10 16:25:56 +0000226 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
227 PYTHON_API_VERSION);
Guido van Rossum34162a11994-05-23 12:37:57 +0000228}