blob: 0a48926da65ef1809b3323ebfe5fcdaf77ca5cf2 [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 Rossum4f3a62d2002-01-15 19:21:05 +000043 if (!PyArg_ParseTuple(args, "OOO:instancemethod",
44 &func, &self, &classObj))
Guido van Rossumf9888eb1995-01-07 11:50:36 +000045 return NULL;
Guido van Rossum7859f871998-07-08 14:58:16 +000046 if (!PyCallable_Check(func)) {
47 PyErr_SetString(PyExc_TypeError,
48 "first argument must be callable");
49 return NULL;
50 }
51 if (self == Py_None)
52 self = NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +000053 return PyMethod_New(func, self, classObj);
Guido van Rossumf9888eb1995-01-07 11:50:36 +000054}
55
56static char new_function_doc[] =
Fred Drake5428c7d2000-10-10 22:07:18 +000057"Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +000058
Barry Warsawa6b7c711996-12-10 16:25:56 +000059static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000060new_function(PyObject* unused, PyObject* args)
Guido van Rossum34162a11994-05-23 12:37:57 +000061{
Barry Warsawa6b7c711996-12-10 16:25:56 +000062 PyObject* code;
63 PyObject* globals;
64 PyObject* name = Py_None;
65 PyObject* defaults = Py_None;
66 PyFunctionObject* newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000067
Guido van Rossum1bff8832000-11-13 20:29:20 +000068 if (!PyArg_ParseTuple(args, "O!O!|OO!:function",
Barry Warsawa6b7c711996-12-10 16:25:56 +000069 &PyCode_Type, &code,
70 &PyDict_Type, &globals,
71 &name,
72 &PyTuple_Type, &defaults))
Guido van Rossumf9888eb1995-01-07 11:50:36 +000073 return NULL;
Guido van Rossum1bff8832000-11-13 20:29:20 +000074 if (name != Py_None && !PyString_Check(name)) {
75 PyErr_SetString(PyExc_TypeError,
76 "arg 3 (name) must be None or string");
77 return NULL;
78 }
Guido van Rossum34162a11994-05-23 12:37:57 +000079
Barry Warsawa6b7c711996-12-10 16:25:56 +000080 newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
Guido van Rossumf9888eb1995-01-07 11:50:36 +000081 if (newfunc == NULL)
82 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +000083
Barry Warsawa6b7c711996-12-10 16:25:56 +000084 if (name != Py_None) {
85 Py_XINCREF(name);
86 Py_XDECREF(newfunc->func_name);
Guido van Rossumf9888eb1995-01-07 11:50:36 +000087 newfunc->func_name = name;
88 }
Guido van Rossumb88679b1998-05-22 00:57:31 +000089 if (defaults != Py_None) {
Barry Warsawa6b7c711996-12-10 16:25:56 +000090 Py_XINCREF(defaults);
91 Py_XDECREF(newfunc->func_defaults);
Guido van Rossumb916faf1996-11-21 16:02:12 +000092 newfunc->func_defaults = defaults;
Guido van Rossumf9888eb1995-01-07 11:50:36 +000093 }
Guido van Rossum34162a11994-05-23 12:37:57 +000094
Barry Warsawa6b7c711996-12-10 16:25:56 +000095 return (PyObject *)newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000096}
Guido van Rossum34162a11994-05-23 12:37:57 +000097
Guido van Rossumf9888eb1995-01-07 11:50:36 +000098static char new_code_doc[] =
Jeremy Hylton6fe0a822001-02-01 19:50:29 +000099"Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING,\n"
100"CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB, FREEVARS,\n"
101"CELLVARS).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000102
Barry Warsawa6b7c711996-12-10 16:25:56 +0000103static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000104new_code(PyObject* unused, PyObject* args)
Guido van Rossum34162a11994-05-23 12:37:57 +0000105{
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000106 int argcount;
107 int nlocals;
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000108 int stacksize;
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000109 int flags;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000110 PyObject* code;
111 PyObject* consts;
112 PyObject* names;
113 PyObject* varnames;
Jeremy Hylton6fe0a822001-02-01 19:50:29 +0000114 PyObject* freevars = NULL;
115 PyObject* cellvars = NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000116 PyObject* filename;
117 PyObject* name;
Guido van Rossum463e55a1997-01-24 03:58:52 +0000118 int firstlineno;
119 PyObject* lnotab;
Guido van Rossumd076c731998-10-07 19:42:25 +0000120
Jeremy Hylton6fe0a822001-02-01 19:50:29 +0000121 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000122 &argcount, &nlocals, &stacksize, &flags,
Barry Warsawa6b7c711996-12-10 16:25:56 +0000123 &code,
124 &PyTuple_Type, &consts,
125 &PyTuple_Type, &names,
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000126 &PyTuple_Type, &varnames,
Guido van Rossum463e55a1997-01-24 03:58:52 +0000127 &filename, &name,
Jeremy Hylton6fe0a822001-02-01 19:50:29 +0000128 &firstlineno, &lnotab,
129 &PyTuple_Type, &freevars,
130 &PyTuple_Type, &cellvars))
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000131 return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000132
Jeremy Hylton6fe0a822001-02-01 19:50:29 +0000133 if (freevars == NULL || cellvars == NULL) {
134 PyObject *empty = PyTuple_New(0);
135 if (empty == NULL)
136 return NULL;
137 if (freevars == NULL) {
138 freevars = empty;
139 Py_INCREF(freevars);
140 }
141 if (cellvars == NULL) {
142 cellvars = empty;
143 Py_INCREF(cellvars);
144 }
145 Py_DECREF(empty);
146 }
147
Jeremy Hylton9f64caa2001-11-09 22:02:48 +0000148 if (!PyObject_CheckReadBuffer(code)) {
Guido van Rossumd076c731998-10-07 19:42:25 +0000149 PyErr_SetString(PyExc_TypeError,
150 "bytecode object must be a single-segment read-only buffer");
151 return NULL;
152 }
153
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000154 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
Barry Warsawa6b7c711996-12-10 16:25:56 +0000155 code, consts, names, varnames,
Jeremy Hylton903f6542001-01-25 20:07:56 +0000156 freevars, cellvars, filename, name,
157 firstlineno, lnotab);
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000158}
Guido van Rossum34162a11994-05-23 12:37:57 +0000159
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000160static char new_module_doc[] =
161"Create a module object from (NAME).";
162
Barry Warsawa6b7c711996-12-10 16:25:56 +0000163static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000164new_module(PyObject* unused, PyObject* args)
Guido van Rossum34162a11994-05-23 12:37:57 +0000165{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000166 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000167
Guido van Rossum43713e52000-02-29 13:59:29 +0000168 if (!PyArg_ParseTuple(args, "s:module", &name))
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000169 return NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000170 return PyModule_New(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000171}
Guido van Rossum34162a11994-05-23 12:37:57 +0000172
Guido van Rossumb28911c1996-01-12 01:38:22 +0000173static char new_class_doc[] =
174"Create a class object from (NAME, BASE_CLASSES, DICT).";
175
Barry Warsawa6b7c711996-12-10 16:25:56 +0000176static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000177new_class(PyObject* unused, PyObject* args)
Guido van Rossumb28911c1996-01-12 01:38:22 +0000178{
Barry Warsawa6b7c711996-12-10 16:25:56 +0000179 PyObject * name;
180 PyObject * classes;
181 PyObject * dict;
Guido van Rossumb28911c1996-01-12 01:38:22 +0000182
Guido van Rossum43713e52000-02-29 13:59:29 +0000183 if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes,
Barry Warsawa6b7c711996-12-10 16:25:56 +0000184 &PyDict_Type, &dict))
Guido van Rossumb28911c1996-01-12 01:38:22 +0000185 return NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000186 return PyClass_New(classes, dict, name);
Guido van Rossumb28911c1996-01-12 01:38:22 +0000187}
188
Barry Warsawa6b7c711996-12-10 16:25:56 +0000189static PyMethodDef new_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000190 {"instance", new_instance,
191 METH_VARARGS, new_instance_doc},
192 {"instancemethod", new_instancemethod,
193 METH_VARARGS, new_im_doc},
194 {"function", new_function,
195 METH_VARARGS, new_function_doc},
196 {"code", new_code,
197 METH_VARARGS, new_code_doc},
198 {"module", new_module,
199 METH_VARARGS, new_module_doc},
200 {"classobj", new_class,
201 METH_VARARGS, new_class_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000202 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000203};
204
Neil Schemenauer26775122001-10-21 22:26:43 +0000205static char new_doc[] =
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000206"Functions to create new objects used by the interpreter.\n\
207\n\
208You need to know a great deal about the interpreter to use this!";
209
Guido van Rossum3886bb61998-12-04 18:50:17 +0000210DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000211initnew(void)
Guido van Rossum34162a11994-05-23 12:37:57 +0000212{
Tim Peters89675072001-08-24 06:29:12 +0000213 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
214 PYTHON_API_VERSION);
Guido van Rossum34162a11994-05-23 12:37:57 +0000215}