Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Module new -- create new objects of various types */ |
| 33 | |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 34 | #include "allobjects.h" |
| 35 | #include "compile.h" |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 37 | static char new_instance_doc[] = |
| 38 | "Create an instance object from (CLASS, DICT) without calling its __init__()."; |
| 39 | |
| 40 | static object * |
| 41 | new_instance(unused, args) |
| 42 | object* unused; |
| 43 | object* args; |
| 44 | { |
| 45 | object* klass; |
| 46 | object *dict; |
| 47 | instanceobject *inst; |
| 48 | if (!newgetargs(args, "O!O!", |
| 49 | &Classtype, &klass, |
| 50 | &Dicttype, &dict)) |
| 51 | return NULL; |
| 52 | inst = NEWOBJ(instanceobject, &Instancetype); |
| 53 | if (inst == NULL) |
| 54 | return NULL; |
| 55 | INCREF(klass); |
| 56 | INCREF(dict); |
| 57 | inst->in_class = (classobject *)klass; |
| 58 | inst->in_dict = dict; |
| 59 | return (object *)inst; |
| 60 | } |
| 61 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 62 | static char new_im_doc[] = |
| 63 | "Create a instance method object from (FUNCTION, INSTANCE, CLASS)."; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 65 | static object * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 66 | new_instancemethod(unused, args) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 67 | object* unused; |
| 68 | object* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 69 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 70 | object* func; |
| 71 | object* self; |
| 72 | object* classObj; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 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); |
| 80 | } |
| 81 | |
| 82 | static char new_function_doc[] = |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame^] | 83 | "Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS])."; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 84 | |
| 85 | static object * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 86 | new_function(unused, args) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 87 | object* unused; |
| 88 | object* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 89 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 90 | object* code; |
| 91 | object* globals; |
| 92 | object* name = None; |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame^] | 93 | object* defaults = None; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 94 | funcobject* newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame^] | 96 | if (!newgetargs(args, "O!O!|SO!", |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 97 | &Codetype, &code, |
| 98 | &Mappingtype, &globals, |
| 99 | &name, |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame^] | 100 | &Tupletype, &defaults)) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 101 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 103 | newfunc = (funcobject *)newfuncobject(code, globals); |
| 104 | if (newfunc == NULL) |
| 105 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 107 | if (name != None) { |
| 108 | XINCREF(name); |
| 109 | XDECREF(newfunc->func_name); |
| 110 | newfunc->func_name = name; |
| 111 | } |
Guido van Rossum | b916faf | 1996-11-21 16:02:12 +0000 | [diff] [blame^] | 112 | if (defaults != NULL) { |
| 113 | XINCREF(defaults); |
| 114 | XDECREF(newfunc->func_defaults); |
| 115 | newfunc->func_defaults = defaults; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 116 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 117 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 118 | return (object *)newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 119 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 121 | static char new_code_doc[] = |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 122 | "Create a code object from (ARGCOUNT, NLOCALS, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME)."; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 123 | |
| 124 | static object * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 125 | new_code(unused, args) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 126 | object* unused; |
| 127 | object* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 128 | { |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 129 | int argcount; |
| 130 | int nlocals; |
| 131 | int flags; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 132 | object* code; |
| 133 | object* consts; |
| 134 | object* names; |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 135 | object* varnames; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 136 | object* filename; |
| 137 | object* name; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 139 | if (!newgetargs(args, "iiiSO!O!O!SS", |
| 140 | &argcount, &nlocals, &flags, /* These are new */ |
| 141 | &code, &Tupletype, &consts, &Tupletype, &names, |
| 142 | &Tupletype, &varnames, /* These are new */ |
| 143 | &filename, &name)) |
| 144 | return NULL; |
| 145 | return (object *)newcodeobject(argcount, nlocals, flags, |
| 146 | code, consts, names, varnames, filename, name); |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 147 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 149 | static char new_module_doc[] = |
| 150 | "Create a module object from (NAME)."; |
| 151 | |
| 152 | static object * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 153 | new_module(unused, args) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 154 | object* unused; |
| 155 | object* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 156 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 157 | char *name; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 159 | if (!newgetargs(args, "s", &name)) |
| 160 | return NULL; |
| 161 | return newmoduleobject(name); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 162 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 182 | static struct methodlist new_methods[] = { |
Guido van Rossum | 26b310a | 1996-06-17 16:56:56 +0000 | [diff] [blame] | 183 | {"instance", new_instance, 1, new_instance_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 184 | {"instancemethod", new_instancemethod, 1, new_im_doc}, |
| 185 | {"function", new_function, 1, new_function_doc}, |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 186 | {"code", new_code, 1, new_code_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 187 | {"module", new_module, 1, new_module_doc}, |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame] | 188 | {"classobj", new_class, 1, new_class_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 189 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 197 | void |
| 198 | initnew() |
| 199 | { |
Guido van Rossum | 37431fb | 1995-01-09 17:49:26 +0000 | [diff] [blame] | 200 | initmodule4("new", new_methods, new_doc, (object *)NULL, |
| 201 | PYTHON_API_VERSION); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 202 | } |