Mercurial > cpython-lru_cache-weakref
annotate Modules/newmodule.c @ 12357:5b59ea41db4e legacy-trunk
Install the docs (with fewer rules).
Add descriptions for the system variables.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 10 Apr 2000 19:36:27 +0000 |
| parents | dc65c5f6ad7c |
| children | 1c7c2dd1beb1 |
| 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; | |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
48 if (!PyArg_ParseTuple(args, "O!O!:instance", |
| 4661 | 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 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
74 if (!PyArg_ParseTuple(args, "OOO!:instancemethod", |
|
8651
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 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
108 if (!PyArg_ParseTuple(args, "O!O!|SO!:function", |
| 4661 | 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; |
|
9293
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
153 PyBufferProcs *pb; |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
154 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
155 if (!PyArg_ParseTuple(args, "iiiiOO!O!O!SSiS:code", |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
156 &argcount, &nlocals, &stacksize, &flags, |
| 4661 | 157 &code, |
| 158 &PyTuple_Type, &consts, | |
| 159 &PyTuple_Type, &names, | |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
160 &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
|
161 &filename, &name, |
|
ee3ac9e0a032
Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents:
4997
diff
changeset
|
162 &firstlineno, &lnotab)) |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
163 return NULL; |
|
9293
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
164 |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
165 pb = code->ob_type->tp_as_buffer; |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
166 if (pb == NULL || |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
167 pb->bf_getreadbuffer == NULL || |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
168 pb->bf_getsegcount == NULL || |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
169 (*pb->bf_getsegcount)(code, NULL) != 1) |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
170 { |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
171 PyErr_SetString(PyExc_TypeError, |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
172 "bytecode object must be a single-segment read-only buffer"); |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
173 return NULL; |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
174 } |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8651
diff
changeset
|
175 |
|
4997
58bbaf284ad6
Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents:
4661
diff
changeset
|
176 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
| 4661 | 177 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
|
178 filename, name, firstlineno, lnotab); |
| 1650 | 179 } |
| 2077 | 180 |
| 181 static char new_module_doc[] = | |
| 182 "Create a module object from (NAME)."; | |
| 1650 | 183 |
| 4661 | 184 static PyObject * |
| 1650 | 185 new_module(unused, args) |
| 4661 | 186 PyObject* unused; |
| 187 PyObject* args; | |
| 1650 | 188 { |
| 2077 | 189 char *name; |
| 1650 | 190 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
191 if (!PyArg_ParseTuple(args, "s:module", &name)) |
| 2077 | 192 return NULL; |
| 4661 | 193 return PyModule_New(name); |
| 1650 | 194 } |
| 195 | |
| 3337 | 196 static char new_class_doc[] = |
| 197 "Create a class object from (NAME, BASE_CLASSES, DICT)."; | |
| 198 | |
| 4661 | 199 static PyObject * |
| 3337 | 200 new_class(unused, args) |
| 4661 | 201 PyObject* unused; |
| 202 PyObject* args; | |
| 3337 | 203 { |
| 4661 | 204 PyObject * name; |
| 205 PyObject * classes; | |
| 206 PyObject * dict; | |
| 3337 | 207 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
208 if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes, |
| 4661 | 209 &PyDict_Type, &dict)) |
| 3337 | 210 return NULL; |
| 4661 | 211 return PyClass_New(classes, dict, name); |
| 3337 | 212 } |
| 213 | |
| 4661 | 214 static PyMethodDef new_methods[] = { |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
215 {"instance", new_instance, 1, new_instance_doc}, |
| 2077 | 216 {"instancemethod", new_instancemethod, 1, new_im_doc}, |
| 217 {"function", new_function, 1, new_function_doc}, | |
| 218 {"code", new_code, 1, new_code_doc}, | |
| 219 {"module", new_module, 1, new_module_doc}, | |
| 3337 | 220 {"classobj", new_class, 1, new_class_doc}, |
| 2077 | 221 {NULL, NULL} /* sentinel */ |
| 1650 | 222 }; |
| 223 | |
| 2077 | 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 | |
|
9588
38023a425e48
Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents:
9293
diff
changeset
|
229 DL_EXPORT(void) |
| 1650 | 230 initnew() |
| 231 { | |
| 4661 | 232 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL, |
| 233 PYTHON_API_VERSION); | |
| 1650 | 234 } |
