Mercurial > cpython-lru_cache-weakref
annotate Modules/newmodule.c @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | ee3ac9e0a032 |
| children | f21867f8035b |
| rev | line source |
|---|---|
| 2077 | 1 /*********************************************************** |
| 2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, | |
| 3 The Netherlands. | |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 2077 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 2077 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
15 permission. |
| 2077 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3678
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 2077 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 32 /* Module new -- create new objects of various types */ | |
| 33 | |
| 4661 | 34 #include "Python.h" |
| 1650 | 35 #include "compile.h" |
| 36 | |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
37 static char new_instance_doc[] = |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
38 "Create an instance object from (CLASS, DICT) without calling its __init__()."; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
39 |
| 4661 | 40 static PyObject * |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
41 new_instance(unused, args) |
| 4661 | 42 PyObject* unused; |
| 43 PyObject* args; | |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
44 { |
| 4661 | 45 PyObject* klass; |
| 46 PyObject *dict; | |
| 47 PyInstanceObject *inst; | |
| 48 if (!PyArg_ParseTuple(args, "O!O!", | |
| 49 &PyClass_Type, &klass, | |
| 50 &PyDict_Type, &dict)) | |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
51 return NULL; |
| 4661 | 52 inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type); |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
53 if (inst == NULL) |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
54 return NULL; |
| 4661 | 55 Py_INCREF(klass); |
| 56 Py_INCREF(dict); | |
| 57 inst->in_class = (PyClassObject *)klass; | |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
58 inst->in_dict = dict; |
| 4661 | 59 return (PyObject *)inst; |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
60 } |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
61 |
| 2077 | 62 static char new_im_doc[] = |
| 63 "Create a instance method object from (FUNCTION, INSTANCE, CLASS)."; | |
| 1650 | 64 |
| 4661 | 65 static PyObject * |
| 1650 | 66 new_instancemethod(unused, args) |
| 4661 | 67 PyObject* unused; |
| 68 PyObject* args; | |
| 1650 | 69 { |
| 4661 | 70 PyObject* func; |
| 71 PyObject* self; | |
| 72 PyObject* classObj; | |
| 2077 | 73 |
| 4661 | 74 if (!PyArg_ParseTuple(args, "O!O!O!", |
| 75 &PyFunction_Type, &func, | |
| 76 &PyInstance_Type, &self, | |
| 77 &PyClass_Type, &classObj)) | |
| 2077 | 78 return NULL; |
| 4661 | 79 return PyMethod_New(func, self, classObj); |
| 1650 | 80 } |
| 81 | |
| 2077 | 82 static char new_function_doc[] = |
|
4562
d030ea533e63
Upgraded new.function() contributed by Tommy. Also got rid of #if 0'ed code.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
83 "Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS])."; |
| 2077 | 84 |
| 4661 | 85 static PyObject * |
| 1650 | 86 new_function(unused, args) |
| 4661 | 87 PyObject* unused; |
| 88 PyObject* args; | |
| 1650 | 89 { |
| 4661 | 90 PyObject* code; |
| 91 PyObject* globals; | |
| 92 PyObject* name = Py_None; | |
| 93 PyObject* defaults = Py_None; | |
| 94 PyFunctionObject* newfunc; | |
| 1650 | 95 |
| 4661 | 96 if (!PyArg_ParseTuple(args, "O!O!|SO!", |
| 97 &PyCode_Type, &code, | |
| 98 &PyDict_Type, &globals, | |
| 99 &name, | |
| 100 &PyTuple_Type, &defaults)) | |
| 2077 | 101 return NULL; |
| 1650 | 102 |
| 4661 | 103 newfunc = (PyFunctionObject *)PyFunction_New(code, globals); |
| 2077 | 104 if (newfunc == NULL) |
| 105 return NULL; | |
| 1650 | 106 |
| 4661 | 107 if (name != Py_None) { |
| 108 Py_XINCREF(name); | |
| 109 Py_XDECREF(newfunc->func_name); | |
| 2077 | 110 newfunc->func_name = name; |
| 111 } | |
|
4562
d030ea533e63
Upgraded new.function() contributed by Tommy. Also got rid of #if 0'ed code.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
112 if (defaults != NULL) { |
| 4661 | 113 Py_XINCREF(defaults); |
| 114 Py_XDECREF(newfunc->func_defaults); | |
|
4562
d030ea533e63
Upgraded new.function() contributed by Tommy. Also got rid of #if 0'ed code.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
115 newfunc->func_defaults = defaults; |
| 2077 | 116 } |
| 1650 | 117 |
| 4661 | 118 return (PyObject *)newfunc; |
| 2077 | 119 } |
| 120 | |
| 121 static char new_code_doc[] = | |
|
5044
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
122 "Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB)."; |
| 2077 | 123 |
| 4661 | 124 static PyObject * |
| 1650 | 125 new_code(unused, args) |
| 4661 | 126 PyObject* unused; |
| 127 PyObject* args; | |
| 1650 | 128 { |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
129 int argcount; |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
130 int nlocals; |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
131 int stacksize; |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
132 int flags; |
| 4661 | 133 PyObject* code; |
| 134 PyObject* consts; | |
| 135 PyObject* names; | |
| 136 PyObject* varnames; | |
| 137 PyObject* filename; | |
| 138 PyObject* name; | |
|
5044
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
139 int firstlineno; |
|
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
140 PyObject* lnotab; |
| 1650 | 141 |
|
5044
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
142 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS", |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
143 &argcount, &nlocals, &stacksize, &flags, |
| 4661 | 144 &code, |
| 145 &PyTuple_Type, &consts, | |
| 146 &PyTuple_Type, &names, | |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
147 &PyTuple_Type, &varnames, |
|
5044
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
148 &filename, &name, |
|
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
149 &firstlineno, &lnotab)) |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
150 return NULL; |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
151 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
| 4661 | 152 code, consts, names, varnames, |
|
5044
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
153 filename, name, firstlineno, lnotab); |
| 1650 | 154 } |
| 2077 | 155 |
| 156 static char new_module_doc[] = | |
| 157 "Create a module object from (NAME)."; | |
| 1650 | 158 |
| 4661 | 159 static PyObject * |
| 1650 | 160 new_module(unused, args) |
| 4661 | 161 PyObject* unused; |
| 162 PyObject* args; | |
| 1650 | 163 { |
| 2077 | 164 char *name; |
| 1650 | 165 |
| 4661 | 166 if (!PyArg_ParseTuple(args, "s", &name)) |
| 2077 | 167 return NULL; |
| 4661 | 168 return PyModule_New(name); |
| 1650 | 169 } |
| 170 | |
| 3337 | 171 static char new_class_doc[] = |
| 172 "Create a class object from (NAME, BASE_CLASSES, DICT)."; | |
| 173 | |
| 4661 | 174 static PyObject * |
| 3337 | 175 new_class(unused, args) |
| 4661 | 176 PyObject* unused; |
| 177 PyObject* args; | |
| 3337 | 178 { |
| 4661 | 179 PyObject * name; |
| 180 PyObject * classes; | |
| 181 PyObject * dict; | |
| 3337 | 182 |
| 4661 | 183 if (!PyArg_ParseTuple(args, "SO!O!", &name, &PyTuple_Type, &classes, |
| 184 &PyDict_Type, &dict)) | |
| 3337 | 185 return NULL; |
| 4661 | 186 return PyClass_New(classes, dict, name); |
| 3337 | 187 } |
| 188 | |
| 4661 | 189 static PyMethodDef new_methods[] = { |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
190 {"instance", new_instance, 1, new_instance_doc}, |
| 2077 | 191 {"instancemethod", new_instancemethod, 1, new_im_doc}, |
| 192 {"function", new_function, 1, new_function_doc}, | |
| 193 {"code", new_code, 1, new_code_doc}, | |
| 194 {"module", new_module, 1, new_module_doc}, | |
| 3337 | 195 {"classobj", new_class, 1, new_class_doc}, |
| 2077 | 196 {NULL, NULL} /* sentinel */ |
| 1650 | 197 }; |
| 198 | |
| 2077 | 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 | |
| 1650 | 204 void |
| 205 initnew() | |
| 206 { | |
| 4661 | 207 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL, |
| 208 PYTHON_API_VERSION); | |
| 1650 | 209 } |
