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 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 74 | if (!PyArg_ParseTuple(args, "O!O!O!", |
| 75 | &PyFunction_Type, &func, |
| 76 | &PyInstance_Type, &self, |
| 77 | &PyClass_Type, &classObj)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 78 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 79 | return PyMethod_New(func, self, classObj); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static char new_function_doc[] = |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame] | 83 | "Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS])."; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 84 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 85 | static PyObject * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 86 | new_function(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 87 | PyObject* unused; |
| 88 | PyObject* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 89 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 90 | PyObject* code; |
| 91 | PyObject* globals; |
| 92 | PyObject* name = Py_None; |
| 93 | PyObject* defaults = Py_None; |
| 94 | PyFunctionObject* newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 95 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 96 | if (!PyArg_ParseTuple(args, "O!O!|SO!", |
| 97 | &PyCode_Type, &code, |
| 98 | &PyDict_Type, &globals, |
| 99 | &name, |
| 100 | &PyTuple_Type, &defaults)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 101 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 102 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 103 | newfunc = (PyFunctionObject *)PyFunction_New(code, globals); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 104 | if (newfunc == NULL) |
| 105 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 106 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 107 | if (name != Py_None) { |
| 108 | Py_XINCREF(name); |
| 109 | Py_XDECREF(newfunc->func_name); |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 110 | newfunc->func_name = name; |
| 111 | } |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame] | 112 | if (defaults != NULL) { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 113 | Py_XINCREF(defaults); |
| 114 | Py_XDECREF(newfunc->func_defaults); |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame] | 115 | newfunc->func_defaults = defaults; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 116 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 117 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 118 | return (PyObject *)newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 119 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 121 | static char new_code_doc[] = |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame^] | 122 | "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] | 123 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 124 | static PyObject * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 125 | new_code(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 126 | PyObject* unused; |
| 127 | PyObject* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 128 | { |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 129 | int argcount; |
| 130 | int nlocals; |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 131 | int stacksize; |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 132 | int flags; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 133 | PyObject* code; |
| 134 | PyObject* consts; |
| 135 | PyObject* names; |
| 136 | PyObject* varnames; |
| 137 | PyObject* filename; |
| 138 | PyObject* name; |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame^] | 139 | int firstlineno; |
| 140 | PyObject* lnotab; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame^] | 142 | if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS", |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 143 | &argcount, &nlocals, &stacksize, &flags, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 144 | &code, |
| 145 | &PyTuple_Type, &consts, |
| 146 | &PyTuple_Type, &names, |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 147 | &PyTuple_Type, &varnames, |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame^] | 148 | &filename, &name, |
| 149 | &firstlineno, &lnotab)) |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 150 | return NULL; |
Guido van Rossum | e9fd28d | 1997-01-17 21:12:06 +0000 | [diff] [blame] | 151 | return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 152 | code, consts, names, varnames, |
Guido van Rossum | 463e55a | 1997-01-24 03:58:52 +0000 | [diff] [blame^] | 153 | filename, name, firstlineno, lnotab); |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 154 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 156 | static char new_module_doc[] = |
| 157 | "Create a module object from (NAME)."; |
| 158 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 159 | static PyObject * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 160 | new_module(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 161 | PyObject* unused; |
| 162 | PyObject* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 163 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 164 | char *name; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 165 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 166 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 167 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 168 | return PyModule_New(name); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 169 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 171 | static char new_class_doc[] = |
| 172 | "Create a class object from (NAME, BASE_CLASSES, DICT)."; |
| 173 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 174 | static PyObject * |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 175 | new_class(unused, args) |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 176 | PyObject* unused; |
| 177 | PyObject* args; |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 178 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 179 | PyObject * name; |
| 180 | PyObject * classes; |
| 181 | PyObject * dict; |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 182 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 183 | if (!PyArg_ParseTuple(args, "SO!O!", &name, &PyTuple_Type, &classes, |
| 184 | &PyDict_Type, &dict)) |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 185 | return NULL; |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 186 | return PyClass_New(classes, dict, name); |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 189 | static PyMethodDef new_methods[] = { |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 190 | {"instance", new_instance, 1, new_instance_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 191 | {"instancemethod", new_instancemethod, 1, new_im_doc}, |
| 192 | {"function", new_function, 1, new_function_doc}, |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 193 | {"code", new_code, 1, new_code_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 194 | {"module", new_module, 1, new_module_doc}, |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 195 | {"classobj", new_class, 1, new_class_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 196 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 199 | char new_doc[] = |
| 200 | "Functions to create new objects used by the interpreter.\n\ |
| 201 | \n\ |
| 202 | You need to know a great deal about the interpreter to use this!"; |
| 203 | |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 204 | void |
| 205 | initnew() |
| 206 | { |
Barry Warsaw | a6b7c71 | 1996-12-10 16:25:56 +0000 | [diff] [blame] | 207 | Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL, |
| 208 | PYTHON_API_VERSION); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 209 | } |