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 | |
| 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 | |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
| 28 | #include "compile.h" |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 30 | static char new_im_doc[] = |
| 31 | "Create a instance method object from (FUNCTION, INSTANCE, CLASS)."; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 33 | static object * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 34 | new_instancemethod(unused, args) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 35 | object* unused; |
| 36 | object* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 37 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 38 | object* func; |
| 39 | object* self; |
| 40 | object* classObj; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 42 | if (!newgetargs(args, "O!O!O!", |
| 43 | &Functype, &func, |
| 44 | &Instancetype, &self, |
| 45 | &Classtype, &classObj)) |
| 46 | return NULL; |
| 47 | return newinstancemethodobject(func, self, classObj); |
| 48 | } |
| 49 | |
Guido van Rossum | b158909 | 1995-07-28 16:44:53 +0000 | [diff] [blame] | 50 | /* XXX These internal interfaces have changed -- who'll fix this code? */ |
| 51 | #if 0 |
| 52 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 53 | static char new_function_doc[] = |
| 54 | "Create a function object from (CODE, GLOBALS, [NAME, ARGCOUNT, ARGDEFS])."; |
| 55 | |
| 56 | static object * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 57 | new_function(unused, args) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 58 | object* unused; |
| 59 | object* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 60 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 61 | object* code; |
| 62 | object* globals; |
| 63 | object* name = None; |
| 64 | int argcount = -1; |
| 65 | object* argdefs = None; |
| 66 | funcobject* newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 68 | if (!newgetargs(args, "O!O!|SiO!", |
| 69 | &Codetype, &code, |
| 70 | &Mappingtype, &globals, |
| 71 | &name, |
| 72 | &argcount, |
| 73 | &Tupletype, &argdefs)) |
| 74 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 76 | newfunc = (funcobject *)newfuncobject(code, globals); |
| 77 | if (newfunc == NULL) |
| 78 | return NULL; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 80 | if (name != None) { |
| 81 | XINCREF(name); |
| 82 | XDECREF(newfunc->func_name); |
| 83 | newfunc->func_name = name; |
| 84 | } |
| 85 | newfunc->func_argcount = argcount; |
| 86 | if (argdefs != NULL) { |
| 87 | XINCREF(argdefs); |
| 88 | XDECREF(newfunc->func_argdefs); |
| 89 | newfunc->func_argdefs = argdefs; |
| 90 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 92 | return (object *)newfunc; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 93 | } |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 94 | #endif |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 96 | static char new_code_doc[] = |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 97 | "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] | 98 | |
| 99 | static object * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 100 | new_code(unused, args) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 101 | object* unused; |
| 102 | object* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 104 | int argcount; |
| 105 | int nlocals; |
| 106 | int flags; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 107 | object* code; |
| 108 | object* consts; |
| 109 | object* names; |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 110 | object* varnames; |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 111 | object* filename; |
| 112 | object* name; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 114 | #if 0 |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 115 | if (!newgetargs(args, "SO!O!SS", |
| 116 | &code, &Tupletype, &consts, &Tupletype, &names, |
| 117 | &filename, &name)) |
| 118 | return NULL; |
| 119 | return (object *)newcodeobject(code, consts, names, filename, name); |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 120 | #else |
| 121 | if (!newgetargs(args, "iiiSO!O!O!SS", |
| 122 | &argcount, &nlocals, &flags, /* These are new */ |
| 123 | &code, &Tupletype, &consts, &Tupletype, &names, |
| 124 | &Tupletype, &varnames, /* These are new */ |
| 125 | &filename, &name)) |
| 126 | return NULL; |
| 127 | return (object *)newcodeobject(argcount, nlocals, flags, |
| 128 | code, consts, names, varnames, filename, name); |
Guido van Rossum | b158909 | 1995-07-28 16:44:53 +0000 | [diff] [blame] | 129 | #endif |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 130 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 132 | static char new_module_doc[] = |
| 133 | "Create a module object from (NAME)."; |
| 134 | |
| 135 | static object * |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 136 | new_module(unused, args) |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 137 | object* unused; |
| 138 | object* args; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 139 | { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 140 | char *name; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 142 | if (!newgetargs(args, "s", &name)) |
| 143 | return NULL; |
| 144 | return newmoduleobject(name); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 145 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame^] | 147 | static char new_class_doc[] = |
| 148 | "Create a class object from (NAME, BASE_CLASSES, DICT)."; |
| 149 | |
| 150 | static object * |
| 151 | new_class(unused, args) |
| 152 | object* unused; |
| 153 | object* args; |
| 154 | { |
| 155 | object * name; |
| 156 | object * classes; |
| 157 | object * dict; |
| 158 | |
| 159 | if (!newgetargs(args, "SO!O!", &name, &Tupletype, &classes, |
| 160 | &Mappingtype, &dict)) |
| 161 | return NULL; |
| 162 | return newclassobject(classes, dict, name); |
| 163 | } |
| 164 | |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 165 | static struct methodlist new_methods[] = { |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 166 | {"instancemethod", new_instancemethod, 1, new_im_doc}, |
Guido van Rossum | b158909 | 1995-07-28 16:44:53 +0000 | [diff] [blame] | 167 | #if 0 |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 168 | {"function", new_function, 1, new_function_doc}, |
Guido van Rossum | b158909 | 1995-07-28 16:44:53 +0000 | [diff] [blame] | 169 | #endif |
Guido van Rossum | 896fc7e | 1995-09-30 17:01:02 +0000 | [diff] [blame] | 170 | {"code", new_code, 1, new_code_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 171 | {"module", new_module, 1, new_module_doc}, |
Guido van Rossum | b28911c | 1996-01-12 01:38:22 +0000 | [diff] [blame^] | 172 | {"classobj", new_class, 1, new_class_doc}, |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 173 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
Guido van Rossum | f9888eb | 1995-01-07 11:50:36 +0000 | [diff] [blame] | 176 | char new_doc[] = |
| 177 | "Functions to create new objects used by the interpreter.\n\ |
| 178 | \n\ |
| 179 | You need to know a great deal about the interpreter to use this!"; |
| 180 | |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 181 | void |
| 182 | initnew() |
| 183 | { |
Guido van Rossum | 37431fb | 1995-01-09 17:49:26 +0000 | [diff] [blame] | 184 | initmodule4("new", new_methods, new_doc, (object *)NULL, |
| 185 | PYTHON_API_VERSION); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 186 | } |