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[] = |
| 8 | "Create an instance object from (CLASS, DICT) without calling its __init__()."; |
| 9 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 10 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 11 | new_instance(PyObject* unused, PyObject* args) |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 12 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 13 | PyObject* klass; |
| 14 | PyObject *dict; |
| 15 | PyInstanceObject *inst; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 16 | if (!PyArg_ParseTuple(args, "O!O!:instance", |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 17 | &PyClass_Type, &klass, |
| 18 | &PyDict_Type, &dict)) |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 19 | return NULL; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 20 | inst = PyObject_New(PyInstanceObject, &PyInstance_Type); |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 21 | if (inst == NULL) |
| 22 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 23 | Py_INCREF(klass); |
| 24 | Py_INCREF(dict); |
| 25 | inst->in_class = (PyClassObject *)klass; |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 26 | inst->in_dict = dict; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 27 | PyObject_GC_Init(inst); |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 28 | return (PyObject *)inst; |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 31 | static char new_im_doc[] = |
| 32 | "Create a instance method object from (FUNCTION, INSTANCE, CLASS)."; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 33 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 34 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 35 | new_instancemethod(PyObject* unused, PyObject* args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 36 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 37 | PyObject* func; |
| 38 | PyObject* self; |
| 39 | PyObject* classObj; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 41 | if (!PyArg_ParseTuple(args, "OOO!:instancemethod", |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 42 | &func, |
| 43 | &self, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 44 | &PyClass_Type, &classObj)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 45 | return NULL; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 46 | 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; |
| 53 | else if (!PyInstance_Check(self)) { |
| 54 | PyErr_SetString(PyExc_TypeError, |
| 55 | "second argument must be instance or None"); |
| 56 | return NULL; |
| 57 | } |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 58 | return PyMethod_New(func, self, classObj); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static char new_function_doc[] = |
Fred Drake | 5428c7d | 2000-10-10 22:07:18 +0000 | [diff] [blame^] | 62 | "Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]])."; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 63 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 64 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 65 | new_function(PyObject* unused, PyObject* args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 66 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 67 | PyObject* code; |
| 68 | PyObject* globals; |
| 69 | PyObject* name = Py_None; |
| 70 | PyObject* defaults = Py_None; |
| 71 | PyFunctionObject* newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 73 | if (!PyArg_ParseTuple(args, "O!O!|SO!:function", |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 74 | &PyCode_Type, &code, |
| 75 | &PyDict_Type, &globals, |
| 76 | &name, |
| 77 | &PyTuple_Type, &defaults)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 78 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 79 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 80 | newfunc = (PyFunctionObject *)PyFunction_New(code, globals); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 81 | if (newfunc == NULL) |
| 82 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 83 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 84 | if (name != Py_None) { |
| 85 | Py_XINCREF(name); |
| 86 | Py_XDECREF(newfunc->func_name); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 87 | newfunc->func_name = name; |
| 88 | } |
Guido van Rossum | b88679b | 1998-05-22 00:57:31 +0000 | [diff] [blame] | 89 | if (defaults != Py_None) { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 90 | Py_XINCREF(defaults); |
| 91 | Py_XDECREF(newfunc->func_defaults); |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame] | 92 | newfunc->func_defaults = defaults; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 93 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 94 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 95 | return (PyObject *)newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 96 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 98 | static char new_code_doc[] = |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 99 | "Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB)."; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 100 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 101 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 102 | new_code(PyObject* unused, PyObject* args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 104 | int argcount; |
| 105 | int nlocals; |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 106 | int stacksize; |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 107 | int flags; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 108 | PyObject* code; |
| 109 | PyObject* consts; |
| 110 | PyObject* names; |
| 111 | PyObject* varnames; |
| 112 | PyObject* filename; |
| 113 | PyObject* name; |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 114 | int firstlineno; |
| 115 | PyObject* lnotab; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 116 | PyBufferProcs *pb; |
| 117 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 118 | if (!PyArg_ParseTuple(args, "iiiiOO!O!O!SSiS:code", |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 119 | &argcount, &nlocals, &stacksize, &flags, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 120 | &code, |
| 121 | &PyTuple_Type, &consts, |
| 122 | &PyTuple_Type, &names, |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 123 | &PyTuple_Type, &varnames, |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 124 | &filename, &name, |
| 125 | &firstlineno, &lnotab)) |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 126 | return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 127 | |
| 128 | pb = code->ob_type->tp_as_buffer; |
| 129 | if (pb == NULL || |
| 130 | pb->bf_getreadbuffer == NULL || |
| 131 | pb->bf_getsegcount == NULL || |
| 132 | (*pb->bf_getsegcount)(code, NULL) != 1) |
| 133 | { |
| 134 | PyErr_SetString(PyExc_TypeError, |
| 135 | "bytecode object must be a single-segment read-only buffer"); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 139 | return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 140 | code, consts, names, varnames, |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 141 | filename, name, firstlineno, lnotab); |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 142 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 144 | static char new_module_doc[] = |
| 145 | "Create a module object from (NAME)."; |
| 146 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 147 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 148 | new_module(PyObject* unused, PyObject* args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 149 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 150 | char *name; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 152 | if (!PyArg_ParseTuple(args, "s:module", &name)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 153 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 154 | return PyModule_New(name); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 155 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 157 | static char new_class_doc[] = |
| 158 | "Create a class object from (NAME, BASE_CLASSES, DICT)."; |
| 159 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 160 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 161 | new_class(PyObject* unused, PyObject* args) |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 162 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 163 | PyObject * name; |
| 164 | PyObject * classes; |
| 165 | PyObject * dict; |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 167 | if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 168 | &PyDict_Type, &dict)) |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 169 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 170 | return PyClass_New(classes, dict, name); |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 173 | static PyMethodDef new_methods[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 174 | {"instance", new_instance, |
| 175 | METH_VARARGS, new_instance_doc}, |
| 176 | {"instancemethod", new_instancemethod, |
| 177 | METH_VARARGS, new_im_doc}, |
| 178 | {"function", new_function, |
| 179 | METH_VARARGS, new_function_doc}, |
| 180 | {"code", new_code, |
| 181 | METH_VARARGS, new_code_doc}, |
| 182 | {"module", new_module, |
| 183 | METH_VARARGS, new_module_doc}, |
| 184 | {"classobj", new_class, |
| 185 | METH_VARARGS, new_class_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 186 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 189 | char new_doc[] = |
| 190 | "Functions to create new objects used by the interpreter.\n\ |
| 191 | \n\ |
| 192 | You need to know a great deal about the interpreter to use this!"; |
| 193 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 194 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 195 | initnew(void) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 196 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 197 | Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL, |
| 198 | PYTHON_API_VERSION); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 199 | } |