Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Module new -- create new objects of various types */ |
| 33 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 35 | #include "compile.h" |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 37 | static char new_instance_doc[] = |
| 38 | "Create an instance object from (CLASS, DICT) without calling its __init__()."; |
| 39 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 40 | static PyObject * |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 41 | new_instance(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 42 | PyObject* unused; |
| 43 | PyObject* args; |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 44 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 45 | PyObject* klass; |
| 46 | PyObject *dict; |
| 47 | PyInstanceObject *inst; |
| 48 | if (!PyArg_ParseTuple(args, "O!O!", |
| 49 | &PyClass_Type, &klass, |
| 50 | &PyDict_Type, &dict)) |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 51 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 52 | inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type); |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 53 | if (inst == NULL) |
| 54 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 55 | Py_INCREF(klass); |
| 56 | Py_INCREF(dict); |
| 57 | inst->in_class = (PyClassObject *)klass; |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 58 | inst->in_dict = dict; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 59 | return (PyObject *)inst; |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 62 | static char new_im_doc[] = |
| 63 | "Create a instance method object from (FUNCTION, INSTANCE, CLASS)."; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 64 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 65 | static PyObject * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 66 | new_instancemethod(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 67 | PyObject* unused; |
| 68 | PyObject* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 69 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 70 | PyObject* func; |
| 71 | PyObject* self; |
| 72 | PyObject* classObj; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 74 | if (!PyArg_ParseTuple(args, "OOO!", |
| 75 | &func, |
| 76 | &self, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 77 | &PyClass_Type, &classObj)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 78 | return NULL; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 79 | if (!PyCallable_Check(func)) { |
| 80 | PyErr_SetString(PyExc_TypeError, |
| 81 | "first argument must be callable"); |
| 82 | return NULL; |
| 83 | } |
| 84 | if (self == Py_None) |
| 85 | self = NULL; |
| 86 | else if (!PyInstance_Check(self)) { |
| 87 | PyErr_SetString(PyExc_TypeError, |
| 88 | "second argument must be instance or None"); |
| 89 | return NULL; |
| 90 | } |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 91 | return PyMethod_New(func, self, classObj); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static char new_function_doc[] = |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame] | 95 | "Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS])."; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 96 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 97 | static PyObject * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 98 | new_function(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 99 | PyObject* unused; |
| 100 | PyObject* args; |
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 | PyObject* code; |
| 103 | PyObject* globals; |
| 104 | PyObject* name = Py_None; |
| 105 | PyObject* defaults = Py_None; |
| 106 | PyFunctionObject* newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 107 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 108 | if (!PyArg_ParseTuple(args, "O!O!|SO!", |
| 109 | &PyCode_Type, &code, |
| 110 | &PyDict_Type, &globals, |
| 111 | &name, |
| 112 | &PyTuple_Type, &defaults)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 113 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 114 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 115 | newfunc = (PyFunctionObject *)PyFunction_New(code, globals); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 116 | if (newfunc == NULL) |
| 117 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 118 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 119 | if (name != Py_None) { |
| 120 | Py_XINCREF(name); |
| 121 | Py_XDECREF(newfunc->func_name); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 122 | newfunc->func_name = name; |
| 123 | } |
Guido van Rossum | b88679b | 1998-05-22 00:57:31 +0000 | [diff] [blame] | 124 | if (defaults != Py_None) { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 125 | Py_XINCREF(defaults); |
| 126 | Py_XDECREF(newfunc->func_defaults); |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame] | 127 | newfunc->func_defaults = defaults; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 128 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 129 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 130 | return (PyObject *)newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 131 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 133 | static char new_code_doc[] = |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 134 | "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] | 135 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 136 | static PyObject * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 137 | new_code(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 138 | PyObject* unused; |
| 139 | PyObject* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 140 | { |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 141 | int argcount; |
| 142 | int nlocals; |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 143 | int stacksize; |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 144 | int flags; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 145 | PyObject* code; |
| 146 | PyObject* consts; |
| 147 | PyObject* names; |
| 148 | PyObject* varnames; |
| 149 | PyObject* filename; |
| 150 | PyObject* name; |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 151 | int firstlineno; |
| 152 | PyObject* lnotab; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 153 | PyBufferProcs *pb; |
| 154 | |
| 155 | if (!PyArg_ParseTuple(args, "iiiiOO!O!O!SSiS", |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 156 | &argcount, &nlocals, &stacksize, &flags, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 157 | &code, |
| 158 | &PyTuple_Type, &consts, |
| 159 | &PyTuple_Type, &names, |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 160 | &PyTuple_Type, &varnames, |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 161 | &filename, &name, |
| 162 | &firstlineno, &lnotab)) |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 163 | return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 164 | |
| 165 | pb = code->ob_type->tp_as_buffer; |
| 166 | if (pb == NULL || |
| 167 | pb->bf_getreadbuffer == NULL || |
| 168 | pb->bf_getsegcount == NULL || |
| 169 | (*pb->bf_getsegcount)(code, NULL) != 1) |
| 170 | { |
| 171 | PyErr_SetString(PyExc_TypeError, |
| 172 | "bytecode object must be a single-segment read-only buffer"); |
| 173 | return NULL; |
| 174 | } |
| 175 | |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 176 | return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 177 | code, consts, names, varnames, |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame] | 178 | filename, name, firstlineno, lnotab); |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 179 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 181 | static char new_module_doc[] = |
| 182 | "Create a module object from (NAME)."; |
| 183 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 184 | static PyObject * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 185 | new_module(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 186 | PyObject* unused; |
| 187 | PyObject* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 188 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 189 | char *name; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 190 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 191 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 192 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 193 | return PyModule_New(name); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 194 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 196 | static char new_class_doc[] = |
| 197 | "Create a class object from (NAME, BASE_CLASSES, DICT)."; |
| 198 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 199 | static PyObject * |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 200 | new_class(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 201 | PyObject* unused; |
| 202 | PyObject* args; |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 203 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 204 | PyObject * name; |
| 205 | PyObject * classes; |
| 206 | PyObject * dict; |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 207 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 208 | if (!PyArg_ParseTuple(args, "SO!O!", &name, &PyTuple_Type, &classes, |
| 209 | &PyDict_Type, &dict)) |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 210 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 211 | return PyClass_New(classes, dict, name); |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 214 | static PyMethodDef new_methods[] = { |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 215 | {"instance", new_instance, 1, new_instance_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 216 | {"instancemethod", new_instancemethod, 1, new_im_doc}, |
| 217 | {"function", new_function, 1, new_function_doc}, |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 218 | {"code", new_code, 1, new_code_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 219 | {"module", new_module, 1, new_module_doc}, |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 220 | {"classobj", new_class, 1, new_class_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 221 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 224 | char new_doc[] = |
| 225 | "Functions to create new objects used by the interpreter.\n\ |
| 226 | \n\ |
| 227 | You need to know a great deal about the interpreter to use this!"; |
| 228 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame^] | 229 | DL_EXPORT(void) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 230 | initnew() |
| 231 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 232 | Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL, |
| 233 | PYTHON_API_VERSION); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 234 | } |