Mercurial > cpython-lru_cache-weakref
annotate Modules/newmodule.c @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | d030ea533e63 |
| children | d5023d0c5502 |
| 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 | |
| 1650 | 34 #include "allobjects.h" |
| 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 |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
40 static object * |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
41 new_instance(unused, args) |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
42 object* unused; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
43 object* args; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
44 { |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
45 object* klass; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
46 object *dict; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
47 instanceobject *inst; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
48 if (!newgetargs(args, "O!O!", |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
49 &Classtype, &klass, |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
50 &Dicttype, &dict)) |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
51 return NULL; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
52 inst = NEWOBJ(instanceobject, &Instancetype); |
|
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; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
55 INCREF(klass); |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
56 INCREF(dict); |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
57 inst->in_class = (classobject *)klass; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
58 inst->in_dict = dict; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
59 return (object *)inst; |
|
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 |
| 2077 | 65 static object * |
| 1650 | 66 new_instancemethod(unused, args) |
| 2077 | 67 object* unused; |
| 68 object* args; | |
| 1650 | 69 { |
| 2077 | 70 object* func; |
| 71 object* self; | |
| 72 object* classObj; | |
| 73 | |
| 74 if (!newgetargs(args, "O!O!O!", | |
| 75 &Functype, &func, | |
| 76 &Instancetype, &self, | |
| 77 &Classtype, &classObj)) | |
| 78 return NULL; | |
| 79 return newinstancemethodobject(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 |
| 85 static object * | |
| 1650 | 86 new_function(unused, args) |
| 2077 | 87 object* unused; |
| 88 object* args; | |
| 1650 | 89 { |
| 2077 | 90 object* code; |
| 91 object* globals; | |
| 92 object* name = None; | |
|
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
|
93 object* defaults = None; |
| 2077 | 94 funcobject* newfunc; |
| 1650 | 95 |
|
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
|
96 if (!newgetargs(args, "O!O!|SO!", |
| 2077 | 97 &Codetype, &code, |
| 98 &Mappingtype, &globals, | |
| 99 &name, | |
|
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
|
100 &Tupletype, &defaults)) |
| 2077 | 101 return NULL; |
| 1650 | 102 |
| 2077 | 103 newfunc = (funcobject *)newfuncobject(code, globals); |
| 104 if (newfunc == NULL) | |
| 105 return NULL; | |
| 1650 | 106 |
| 2077 | 107 if (name != None) { |
| 108 XINCREF(name); | |
| 109 XDECREF(newfunc->func_name); | |
| 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) { |
|
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
|
113 XINCREF(defaults); |
|
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
|
114 XDECREF(newfunc->func_defaults); |
|
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 |
| 2077 | 118 return (object *)newfunc; |
| 119 } | |
| 120 | |
| 121 static char new_code_doc[] = | |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
122 "Create a code object from (ARGCOUNT, NLOCALS, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME)."; |
| 2077 | 123 |
| 124 static object * | |
| 1650 | 125 new_code(unused, args) |
| 2077 | 126 object* unused; |
| 127 object* 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; |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
131 int flags; |
| 2077 | 132 object* code; |
| 133 object* consts; | |
| 134 object* names; | |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
135 object* varnames; |
| 2077 | 136 object* filename; |
| 137 object* name; | |
| 1650 | 138 |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
139 if (!newgetargs(args, "iiiSO!O!O!SS", |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
140 &argcount, &nlocals, &flags, /* These are new */ |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
141 &code, &Tupletype, &consts, &Tupletype, &names, |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
142 &Tupletype, &varnames, /* These are new */ |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
143 &filename, &name)) |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
144 return NULL; |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
145 return (object *)newcodeobject(argcount, nlocals, flags, |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
146 code, consts, names, varnames, filename, name); |
| 1650 | 147 } |
| 2077 | 148 |
| 149 static char new_module_doc[] = | |
| 150 "Create a module object from (NAME)."; | |
| 1650 | 151 |
| 2077 | 152 static object * |
| 1650 | 153 new_module(unused, args) |
| 2077 | 154 object* unused; |
| 155 object* args; | |
| 1650 | 156 { |
| 2077 | 157 char *name; |
| 1650 | 158 |
| 2077 | 159 if (!newgetargs(args, "s", &name)) |
| 160 return NULL; | |
| 161 return newmoduleobject(name); | |
| 1650 | 162 } |
| 163 | |
| 3337 | 164 static char new_class_doc[] = |
| 165 "Create a class object from (NAME, BASE_CLASSES, DICT)."; | |
| 166 | |
| 167 static object * | |
| 168 new_class(unused, args) | |
| 169 object* unused; | |
| 170 object* args; | |
| 171 { | |
| 172 object * name; | |
| 173 object * classes; | |
| 174 object * dict; | |
| 175 | |
| 176 if (!newgetargs(args, "SO!O!", &name, &Tupletype, &classes, | |
| 177 &Mappingtype, &dict)) | |
| 178 return NULL; | |
| 179 return newclassobject(classes, dict, name); | |
| 180 } | |
| 181 | |
| 1650 | 182 static struct methodlist new_methods[] = { |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
183 {"instance", new_instance, 1, new_instance_doc}, |
| 2077 | 184 {"instancemethod", new_instancemethod, 1, new_im_doc}, |
| 185 {"function", new_function, 1, new_function_doc}, | |
| 186 {"code", new_code, 1, new_code_doc}, | |
| 187 {"module", new_module, 1, new_module_doc}, | |
| 3337 | 188 {"classobj", new_class, 1, new_class_doc}, |
| 2077 | 189 {NULL, NULL} /* sentinel */ |
| 1650 | 190 }; |
| 191 | |
| 2077 | 192 char new_doc[] = |
| 193 "Functions to create new objects used by the interpreter.\n\ | |
| 194 \n\ | |
| 195 You need to know a great deal about the interpreter to use this!"; | |
| 196 | |
| 1650 | 197 void |
| 198 initnew() | |
| 199 { | |
|
2096
d5c935b9a7b5
initmodule3 -> initmodule3
Guido van Rossum <guido@python.org>
parents:
2077
diff
changeset
|
200 initmodule4("new", new_methods, new_doc, (object *)NULL, |
|
d5c935b9a7b5
initmodule3 -> initmodule3
Guido van Rossum <guido@python.org>
parents:
2077
diff
changeset
|
201 PYTHON_API_VERSION); |
| 1650 | 202 } |
