Mercurial > cpython-lru_cache-weakref
annotate Modules/newmodule.c @ 3720:b341def4eef8 legacy-trunk
Ignore latex intermediate products.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 26 Jun 1996 19:30:01 +0000 |
| parents | bddbaa51aa5a |
| children | 65d2d8e67da3 |
| rev | line source |
|---|---|
| 2077 | 1 /*********************************************************** |
| 2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, | |
| 3 The Netherlands. | |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 25 /* Module new -- create new objects of various types */ | |
| 26 | |
| 1650 | 27 #include "allobjects.h" |
| 28 #include "compile.h" | |
| 29 | |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
30 static char new_instance_doc[] = |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
31 "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
|
32 |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
33 static object * |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
34 new_instance(unused, args) |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
35 object* unused; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
36 object* args; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
37 { |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
38 object* klass; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
39 object *dict; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
40 instanceobject *inst; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
41 if (!newgetargs(args, "O!O!", |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
42 &Classtype, &klass, |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
43 &Dicttype, &dict)) |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
44 return NULL; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
45 inst = NEWOBJ(instanceobject, &Instancetype); |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
46 if (inst == NULL) |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
47 return NULL; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
48 INCREF(klass); |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
49 INCREF(dict); |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
50 inst->in_class = (classobject *)klass; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
51 inst->in_dict = dict; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
52 return (object *)inst; |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
53 } |
|
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
54 |
| 2077 | 55 static char new_im_doc[] = |
| 56 "Create a instance method object from (FUNCTION, INSTANCE, CLASS)."; | |
| 1650 | 57 |
| 2077 | 58 static object * |
| 1650 | 59 new_instancemethod(unused, args) |
| 2077 | 60 object* unused; |
| 61 object* args; | |
| 1650 | 62 { |
| 2077 | 63 object* func; |
| 64 object* self; | |
| 65 object* classObj; | |
| 66 | |
| 67 if (!newgetargs(args, "O!O!O!", | |
| 68 &Functype, &func, | |
| 69 &Instancetype, &self, | |
| 70 &Classtype, &classObj)) | |
| 71 return NULL; | |
| 72 return newinstancemethodobject(func, self, classObj); | |
| 1650 | 73 } |
| 74 | |
|
2871
0534025cefe5
\temporary 'fix' for brokenness
Guido van Rossum <guido@python.org>
parents:
2150
diff
changeset
|
75 /* XXX These internal interfaces have changed -- who'll fix this code? */ |
|
0534025cefe5
\temporary 'fix' for brokenness
Guido van Rossum <guido@python.org>
parents:
2150
diff
changeset
|
76 #if 0 |
|
0534025cefe5
\temporary 'fix' for brokenness
Guido van Rossum <guido@python.org>
parents:
2150
diff
changeset
|
77 |
| 2077 | 78 static char new_function_doc[] = |
| 79 "Create a function object from (CODE, GLOBALS, [NAME, ARGCOUNT, ARGDEFS])."; | |
| 80 | |
| 81 static object * | |
| 1650 | 82 new_function(unused, args) |
| 2077 | 83 object* unused; |
| 84 object* args; | |
| 1650 | 85 { |
| 2077 | 86 object* code; |
| 87 object* globals; | |
| 88 object* name = None; | |
| 89 int argcount = -1; | |
| 90 object* argdefs = None; | |
| 91 funcobject* newfunc; | |
| 1650 | 92 |
| 2077 | 93 if (!newgetargs(args, "O!O!|SiO!", |
| 94 &Codetype, &code, | |
| 95 &Mappingtype, &globals, | |
| 96 &name, | |
| 97 &argcount, | |
| 98 &Tupletype, &argdefs)) | |
| 99 return NULL; | |
| 1650 | 100 |
| 2077 | 101 newfunc = (funcobject *)newfuncobject(code, globals); |
| 102 if (newfunc == NULL) | |
| 103 return NULL; | |
| 1650 | 104 |
| 2077 | 105 if (name != None) { |
| 106 XINCREF(name); | |
| 107 XDECREF(newfunc->func_name); | |
| 108 newfunc->func_name = name; | |
| 109 } | |
| 110 newfunc->func_argcount = argcount; | |
| 111 if (argdefs != NULL) { | |
| 112 XINCREF(argdefs); | |
| 113 XDECREF(newfunc->func_argdefs); | |
| 114 newfunc->func_argdefs = argdefs; | |
| 115 } | |
| 1650 | 116 |
| 2077 | 117 return (object *)newfunc; |
| 118 } | |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
119 #endif |
| 2077 | 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 0 |
| 2077 | 140 if (!newgetargs(args, "SO!O!SS", |
| 141 &code, &Tupletype, &consts, &Tupletype, &names, | |
| 142 &filename, &name)) | |
| 143 return NULL; | |
| 144 return (object *)newcodeobject(code, consts, names, filename, name); | |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
145 #else |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
146 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
|
147 &argcount, &nlocals, &flags, /* These are new */ |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
148 &code, &Tupletype, &consts, &Tupletype, &names, |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
149 &Tupletype, &varnames, /* These are new */ |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
150 &filename, &name)) |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
151 return NULL; |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
152 return (object *)newcodeobject(argcount, nlocals, flags, |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
153 code, consts, names, varnames, filename, name); |
|
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
154 #endif |
| 1650 | 155 } |
| 2077 | 156 |
| 157 static char new_module_doc[] = | |
| 158 "Create a module object from (NAME)."; | |
| 1650 | 159 |
| 2077 | 160 static object * |
| 1650 | 161 new_module(unused, args) |
| 2077 | 162 object* unused; |
| 163 object* args; | |
| 1650 | 164 { |
| 2077 | 165 char *name; |
| 1650 | 166 |
| 2077 | 167 if (!newgetargs(args, "s", &name)) |
| 168 return NULL; | |
| 169 return newmoduleobject(name); | |
| 1650 | 170 } |
| 171 | |
| 3337 | 172 static char new_class_doc[] = |
| 173 "Create a class object from (NAME, BASE_CLASSES, DICT)."; | |
| 174 | |
| 175 static object * | |
| 176 new_class(unused, args) | |
| 177 object* unused; | |
| 178 object* args; | |
| 179 { | |
| 180 object * name; | |
| 181 object * classes; | |
| 182 object * dict; | |
| 183 | |
| 184 if (!newgetargs(args, "SO!O!", &name, &Tupletype, &classes, | |
| 185 &Mappingtype, &dict)) | |
| 186 return NULL; | |
| 187 return newclassobject(classes, dict, name); | |
| 188 } | |
| 189 | |
| 1650 | 190 static struct methodlist new_methods[] = { |
|
3678
bddbaa51aa5a
Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents:
3337
diff
changeset
|
191 {"instance", new_instance, 1, new_instance_doc}, |
| 2077 | 192 {"instancemethod", new_instancemethod, 1, new_im_doc}, |
|
2871
0534025cefe5
\temporary 'fix' for brokenness
Guido van Rossum <guido@python.org>
parents:
2150
diff
changeset
|
193 #if 0 |
| 2077 | 194 {"function", new_function, 1, new_function_doc}, |
|
3135
999fcce496f8
re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents:
2871
diff
changeset
|
195 #endif |
| 2077 | 196 {"code", new_code, 1, new_code_doc}, |
| 197 {"module", new_module, 1, new_module_doc}, | |
| 3337 | 198 {"classobj", new_class, 1, new_class_doc}, |
| 2077 | 199 {NULL, NULL} /* sentinel */ |
| 1650 | 200 }; |
| 201 | |
| 2077 | 202 char new_doc[] = |
| 203 "Functions to create new objects used by the interpreter.\n\ | |
| 204 \n\ | |
| 205 You need to know a great deal about the interpreter to use this!"; | |
| 206 | |
| 1650 | 207 void |
| 208 initnew() | |
| 209 { | |
|
2096
d5c935b9a7b5
initmodule3 -> initmodule3
Guido van Rossum <guido@python.org>
parents:
2077
diff
changeset
|
210 initmodule4("new", new_methods, new_doc, (object *)NULL, |
|
d5c935b9a7b5
initmodule3 -> initmodule3
Guido van Rossum <guido@python.org>
parents:
2077
diff
changeset
|
211 PYTHON_API_VERSION); |
| 1650 | 212 } |
