Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 1 | /* newmodule.c */ |
| 2 | #include "allobjects.h" |
| 3 | #include "compile.h" |
| 4 | #include "modsupport.h" |
| 5 | |
| 6 | |
| 7 | object* |
| 8 | new_instancemethod(unused, args) |
| 9 | object* unused; |
| 10 | object* args; |
| 11 | { |
| 12 | object* func; |
| 13 | object* self; |
| 14 | object* classObj; |
| 15 | |
| 16 | if (!getargs(args, "(OOO)", &func, &self, &classObj)) { |
| 17 | return NULL; |
| 18 | } else if (!is_funcobject(func) || !is_instanceobject(self) || !is_classobject(classObj)) { |
| 19 | err_setstr(TypeError, "expected a function, instance and classobject as args"); |
| 20 | return NULL; |
| 21 | } |
| 22 | return newinstancemethodobject(func, self, classObj); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | object* |
| 27 | new_function(unused, args) |
| 28 | object* unused; |
| 29 | object* args; |
| 30 | { |
| 31 | object* code; |
| 32 | object* globals; |
| 33 | object* name; |
| 34 | object* newfunc; |
| 35 | object* argcount; |
| 36 | object* argdefs; |
| 37 | |
| 38 | if (!getargs(args, "(OOOOO)", &code, &globals, &name, &argcount, &argdefs)) { |
| 39 | return NULL; |
| 40 | } else if (!is_codeobject(code) || !is_mappingobject(globals) || !is_stringobject(name) || !is_intobject(argcount) || !is_tupleobject(argdefs)) { |
| 41 | err_setstr(TypeError, "expected a code object, a dict for globals, a string name, an integer default argument count and a tuple of default argument definitions."); |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | newfunc = newfuncobject(code, globals); |
| 46 | |
| 47 | ((funcobject *)newfunc)->func_name = name; |
| 48 | XINCREF( name ); |
| 49 | XDECREF( ((codeobject*)(((funcobject *)(newfunc))->func_code))->co_name ); |
| 50 | |
| 51 | ((funcobject *)newfunc)->func_argcount = getintvalue(argcount); |
| 52 | ((funcobject *)newfunc)->func_argdefs = argdefs; |
| 53 | XINCREF( argdefs ); |
| 54 | |
| 55 | return newfunc; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | object* |
| 60 | new_code(unused, args) |
| 61 | object* unused; |
| 62 | object* args; |
| 63 | { |
| 64 | object* code; |
| 65 | object* consts; |
| 66 | object* names; |
| 67 | object* filename; |
| 68 | object* name; |
| 69 | |
| 70 | if (!getargs(args, "(OOOOO)", &code, &consts, &names, &filename, &name)) { |
| 71 | return NULL; |
| 72 | } else if (!is_stringobject(code) || !is_listobject(consts) || \ |
| 73 | !is_listobject(names) || !is_stringobject(filename) || \ |
| 74 | !is_stringobject(name)) { |
| 75 | err_setstr(TypeError, "expected a string of compiled code, a list of constants, \ |
| 76 | a list of names used, a string filename, and a string name \ |
| 77 | as args"); |
| 78 | return NULL; |
| 79 | } |
| 80 | return (object *)newcodeobject(code, consts, names, filename, name); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | object* |
| 85 | new_module(unused, args) |
| 86 | object* unused; |
| 87 | object* args; |
| 88 | { |
| 89 | object* name; |
| 90 | |
| 91 | if (!getargs(args, "S", &name)) { |
| 92 | err_setstr(TypeError, "expected a string name as args"); |
| 93 | return NULL; |
| 94 | } |
| 95 | return newmoduleobject(getstringvalue(name)); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static struct methodlist new_methods[] = { |
| 100 | { "instancemethod", new_instancemethod }, |
| 101 | { "function", new_function }, |
| 102 | { "code", new_code }, |
| 103 | { "module", new_module }, |
| 104 | {NULL, NULL} /* sentinel */ |
| 105 | }; |
| 106 | |
| 107 | void |
| 108 | initnew() |
| 109 | { |
| 110 | initmodule("new", new_methods); |
| 111 | } |