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