Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 1 | |
| 2 | /* Module new -- create new objects of various types */ |
| 3 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 5 | #include "compile.h" |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 7 | static char new_instance_doc[] = |
Fred Drake | ceb2bff | 2001-01-28 03:55:09 +0000 | [diff] [blame] | 8 | "Create an instance object from (CLASS [, DICT]) without calling its\n\ |
| 9 | __init__() method. DICT must be a dictionary or None."; |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 10 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 11 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 12 | new_instance(PyObject* unused, PyObject* args) |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 13 | { |
Fred Drake | ceb2bff | 2001-01-28 03:55:09 +0000 | [diff] [blame] | 14 | PyObject *klass; |
| 15 | PyObject *dict = NULL; |
| 16 | |
| 17 | if (!PyArg_ParseTuple(args, "O!|O:instance", |
| 18 | &PyClass_Type, &klass, &dict)) |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 19 | return NULL; |
Fred Drake | ceb2bff | 2001-01-28 03:55:09 +0000 | [diff] [blame] | 20 | |
| 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 Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 28 | return NULL; |
Fred Drake | ceb2bff | 2001-01-28 03:55:09 +0000 | [diff] [blame] | 29 | } |
| 30 | return PyInstance_NewRaw(klass, dict); |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 33 | static char new_im_doc[] = |
| 34 | "Create a instance method object from (FUNCTION, INSTANCE, CLASS)."; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 35 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 36 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 37 | new_instancemethod(PyObject* unused, PyObject* args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 38 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 39 | PyObject* func; |
| 40 | PyObject* self; |
| 41 | PyObject* classObj; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 43 | if (!PyArg_ParseTuple(args, "OOO!:instancemethod", |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 44 | &func, |
| 45 | &self, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 46 | &PyClass_Type, &classObj)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 47 | return NULL; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 48 | 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 Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 60 | return PyMethod_New(func, self, classObj); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static char new_function_doc[] = |
Fred Drake | 5428c7d | 2000-10-10 22:07:18 +0000 | [diff] [blame] | 64 | "Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]])."; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 65 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 66 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 67 | new_function(PyObject* unused, PyObject* args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 68 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 69 | PyObject* code; |
| 70 | PyObject* globals; |
| 71 | PyObject* name = Py_None; |
| 72 | PyObject* defaults = Py_None; |
| 73 | PyFunctionObject* newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 1bff883 | 2000-11-13 20:29:20 +0000 | [diff] [blame] | 75 | if (!PyArg_ParseTuple(args, "O!O!|OO!:function", |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 76 | &PyCode_Type, &code, |
| 77 | &PyDict_Type, &globals, |
| 78 | &name, |
| 79 | &PyTuple_Type, &defaults)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 80 | return NULL; |
Guido van Rossum | 1bff883 | 2000-11-13 20:29:20 +0000 | [diff] [blame] | 81 | 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 Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 86 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 87 | newfunc = (PyFunctionObject *)PyFunction_New(code, globals); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 88 | if (newfunc == NULL) |
| 89 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 90 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 91 | if (name != Py_None) { |
| 92 | Py_XINCREF(name); |
| 93 | Py_XDECREF(newfunc->func_name); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 94 | newfunc->func_name = name; |
| 95 | } |
Guido van Rossum | b88679b | 1998-05-22 00:57:31 +0000 | [diff] [blame] | 96 | if (defaults != Py_None) { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 97 | Py_XINCREF(defaults); |
| 98 | Py_XDECREF(newfunc->func_defaults); |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame] | 99 | newfunc->func_defaults = defaults; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 100 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 101 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 102 | return (PyObject *)newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 103 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 105 | static char new_code_doc[] = |
Jeremy Hylton | 6fe0a82 | 2001-02-01 19:50:29 +0000 | [diff] [blame] | 106 | "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 Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 109 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 110 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 111 | new_code(PyObject* unused, PyObject* args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 112 | { |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 113 | int argcount; |
| 114 | int nlocals; |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 115 | int stacksize; |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 116 | int flags; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 117 | PyObject* code; |
| 118 | PyObject* consts; |
| 119 | PyObject* names; |
| 120 | PyObject* varnames; |
Jeremy Hylton | 6fe0a82 | 2001-02-01 19:50:29 +0000 | [diff] [blame] | 121 | PyObject* freevars = NULL; |
| 122 | PyObject* cellvars = NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 123 | PyObject* filename; |
| 124 | PyObject* name; |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 125 | int firstlineno; |
| 126 | PyObject* lnotab; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 127 | |
Jeremy Hylton | 6fe0a82 | 2001-02-01 19:50:29 +0000 | [diff] [blame] | 128 | if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code", |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 129 | &argcount, &nlocals, &stacksize, &flags, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 130 | &code, |
| 131 | &PyTuple_Type, &consts, |
| 132 | &PyTuple_Type, &names, |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 133 | &PyTuple_Type, &varnames, |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 134 | &filename, &name, |
Jeremy Hylton | 6fe0a82 | 2001-02-01 19:50:29 +0000 | [diff] [blame] | 135 | &firstlineno, &lnotab, |
| 136 | &PyTuple_Type, &freevars, |
| 137 | &PyTuple_Type, &cellvars)) |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 138 | return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 139 | |
Jeremy Hylton | 6fe0a82 | 2001-02-01 19:50:29 +0000 | [diff] [blame] | 140 | if (freevars == NULL || cellvars == NULL) { |
| 141 | PyObject *empty = PyTuple_New(0); |
| 142 | if (empty == NULL) |
| 143 | return NULL; |
| 144 | if (freevars == NULL) { |
| 145 | freevars = empty; |
| 146 | Py_INCREF(freevars); |
| 147 | } |
| 148 | if (cellvars == NULL) { |
| 149 | cellvars = empty; |
| 150 | Py_INCREF(cellvars); |
| 151 | } |
| 152 | Py_DECREF(empty); |
| 153 | } |
| 154 | |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 155 | if (!PyObject_CheckReadBuffer(code)) { |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 156 | PyErr_SetString(PyExc_TypeError, |
| 157 | "bytecode object must be a single-segment read-only buffer"); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 161 | return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 162 | code, consts, names, varnames, |
Jeremy Hylton | 903f654 | 2001-01-25 20:07:56 +0000 | [diff] [blame] | 163 | freevars, cellvars, filename, name, |
| 164 | firstlineno, lnotab); |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 165 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 167 | static char new_module_doc[] = |
| 168 | "Create a module object from (NAME)."; |
| 169 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 170 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 171 | new_module(PyObject* unused, PyObject* args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 172 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 173 | char *name; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 175 | if (!PyArg_ParseTuple(args, "s:module", &name)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 176 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 177 | return PyModule_New(name); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 178 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 180 | static char new_class_doc[] = |
| 181 | "Create a class object from (NAME, BASE_CLASSES, DICT)."; |
| 182 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 183 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 184 | new_class(PyObject* unused, PyObject* args) |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 185 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 186 | PyObject * name; |
| 187 | PyObject * classes; |
| 188 | PyObject * dict; |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 190 | if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 191 | &PyDict_Type, &dict)) |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 192 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 193 | return PyClass_New(classes, dict, name); |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 196 | static PyMethodDef new_methods[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 197 | {"instance", new_instance, |
| 198 | METH_VARARGS, new_instance_doc}, |
| 199 | {"instancemethod", new_instancemethod, |
| 200 | METH_VARARGS, new_im_doc}, |
| 201 | {"function", new_function, |
| 202 | METH_VARARGS, new_function_doc}, |
| 203 | {"code", new_code, |
| 204 | METH_VARARGS, new_code_doc}, |
| 205 | {"module", new_module, |
| 206 | METH_VARARGS, new_module_doc}, |
| 207 | {"classobj", new_class, |
| 208 | METH_VARARGS, new_class_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 209 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
Neil Schemenauer | 2677512 | 2001-10-21 22:26:43 +0000 | [diff] [blame] | 212 | static char new_doc[] = |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 213 | "Functions to create new objects used by the interpreter.\n\ |
| 214 | \n\ |
| 215 | You need to know a great deal about the interpreter to use this!"; |
| 216 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 217 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 218 | initnew(void) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 219 | { |
Tim Peters | 8967507 | 2001-08-24 06:29:12 +0000 | [diff] [blame] | 220 | Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL, |
| 221 | PYTHON_API_VERSION); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 222 | } |