Mercurial > cpython-lru_cache-weakref
annotate Modules/newmodule.c @ 8831:00a6bb65ff29 legacy-trunk
Getting rid of this ancient stuff.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 05 Aug 1998 21:10:09 +0000 |
| parents | 4ba9b70ca56e |
| children | 0a67db47e1d0 |
| 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 |
|
8651
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
74 if (!PyArg_ParseTuple(args, "OOO!", |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
75 &func, |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
76 &self, |
| 4661 | 77 &PyClass_Type, &classObj)) |
| 2077 | 78 return NULL; |
|
8651
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
79 if (!PyCallable_Check(func)) { |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
80 PyErr_SetString(PyExc_TypeError, |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
81 "first argument must be callable"); |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
82 return NULL; |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
83 } |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
84 if (self == Py_None) |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
85 self = NULL; |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
86 else if (!PyInstance_Check(self)) { |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
87 PyErr_SetString(PyExc_TypeError, |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
88 "second argument must be instance or None"); |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
89 return NULL; |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8409
diff
changeset
|
90 } |
| 4661 | 91 return PyMethod_New(func, self, classObj); |
| 1650 | 92 } |
| 93 | |
| 2077 | 94 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
|
95 "Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS])."; |
| 2077 | 96 |
| 4661 | 97 static PyObject * |
| 1650 | 98 new_function(unused, args) |
| 4661 | 99 PyObject* unused; |
| 100 PyObject* args; | |
| 1650 | 101 { |
| 4661 | 102 PyObject* code; |
| 103 PyObject* globals; | |
| 104 PyObject* name = Py_None; | |
| 105 PyObject* defaults = Py_None; | |
| 106 PyFunctionObject* newfunc; | |
| 1650 | 107 |
| 4661 | 108 if (!PyArg_ParseTuple(args, "O!O!|SO!", |
| 109 &PyCode_Type, &code, | |
| 110 &PyDict_Type, &globals, | |
| 111 &name, | |
| 112 &PyTuple_Type, &defaults)) | |
| 2077 | 113 return NULL; |
| 1650 | 114 |
| 4661 | 115 newfunc = (PyFunctionObject *)PyFunction_New(code, globals); |
| 2077 | 116 if (newfunc == NULL) |
| 117 return NULL; | |
| 1650 | 118 |
| 4661 | 119 if (name != Py_None) { |
| 120 Py_XINCREF(name); | |
| 121 Py_XDECREF(newfunc->func_name); | |
| 2077 | 122 newfunc->func_name = name; |
| 123 } | |
|
8409
f21867f8035b
I think there was a tiny bug in new_function() -- the 'defaults'
Guido van Rossum <guido@python.org>
parents:
5044
diff
changeset
|
124 if (defaults != Py_None) { |
| 4661 | 125 Py_XINCREF(defaults); |
| 126 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
|
127 newfunc->func_defaults = defaults; |
| 2077 | 128 } |
| 1650 | 129 |
| 4661 | 130 return (PyObject *)newfunc; |
| 2077 | 131 } |
| 132 | |
| 133 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
|
134 "Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB)."; |
| 2077 | 135 |
| 4661 | 136 static PyObject * |
| 1650 | 137 new_code(unused, args) |
| 4661 | 138 PyObject* unused; |
| 139 PyObject* args; | |
| 1650 | 140 { |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
141 int argcount; |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
142 int nlocals; |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
143 int stacksize; |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
144 int flags; |
| 4661 | 145 PyObject* code; |
| 146 PyObject* consts; | |
| 147 PyObject* names; | |
| 148 PyObject* varnames; | |
| 149 PyObject* filename; | |
| 150 PyObject* name; | |
|
5044
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
151 int firstlineno; |
|
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
152 PyObject* lnotab; |
| 1650 | 153 |
|
5044
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
154 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
|
155 &argcount, &nlocals, &stacksize, &flags, |
| 4661 | 156 &code, |
| 157 &PyTuple_Type, &consts, | |
| 158 &PyTuple_Type, &names, | |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
159 &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
|
160 &filename, &name, |
|
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
161 &firstlineno, &lnotab)) |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
162 return NULL; |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
163 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
| 4661 | 164 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
|
165 filename, name, firstlineno, lnotab); |
| 1650 | 166 } |
| 2077 | 167 |
| 168 static char new_module_doc[] = | |
| 169 "Create a module object from (NAME)."; | |
| 1650 | 170 |
| 4661 | 171 static PyObject * |
| 1650 | 172 new_module(unused, args) |
| 4661 | 173 PyObject* unused; |
| 174 PyObject* args; | |
| 1650 | 175 { |
| 2077 | 176 char *name; |
| 1650 | 177 |
| 4661 | 178 if (!PyArg_ParseTuple(args, "s", &name)) |
| 2077 | 179 return NULL; |
| 4661 | 180 return PyModule_New(name); |
| 1650 | 181 } |
| 182 | |
| 3337 | 183 static char new_class_doc[] = |
| 184 "Create a class object from (NAME, BASE_CLASSES, DICT)."; | |
| 185 | |
| 4661 | 186 static PyObject * |
| 3337 | 187 new_class(unused, args) |
| 4661 | 188 PyObject* unused; |
| 189 PyObject* args; | |
| 3337 | 190 { |
| 4661 | 191 PyObject * name; |
| 192 PyObject * classes; | |
| 193 PyObject * dict; | |
| 3337 | 194 |
| 4661 | 195 if (!PyArg_ParseTuple(args, "SO!O!", &name, &PyTuple_Type, &classes, |
| 196 &PyDict_Type, &dict)) | |
| 3337 | 197 return NULL; |
| 4661 | 198 return PyClass_New(classes, dict, name); |
| 3337 | 199 } |
| 200 | |
| 4661 | 201 static PyMethodDef new_methods[] = { |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
202 {"instance", new_instance, 1, new_instance_doc}, |
| 2077 | 203 {"instancemethod", new_instancemethod, 1, new_im_doc}, |
| 204 {"function", new_function, 1, new_function_doc}, | |
| 205 {"code", new_code, 1, new_code_doc}, | |
| 206 {"module", new_module, 1, new_module_doc}, | |
| 3337 | 207 {"classobj", new_class, 1, new_class_doc}, |
| 2077 | 208 {NULL, NULL} /* sentinel */ |
| 1650 | 209 }; |
| 210 | |
| 2077 | 211 char new_doc[] = |
| 212 "Functions to create new objects used by the interpreter.\n\ | |
| 213 \n\ | |
| 214 You need to know a great deal about the interpreter to use this!"; | |
| 215 | |
| 1650 | 216 void |
| 217 initnew() | |
| 218 { | |
| 4661 | 219 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL, |
| 220 PYTHON_API_VERSION); | |
| 1650 | 221 } |
