blob: 4fbfcc607738b811cb25f548bbe2f4b0c8063525 [file] [log] [blame]
Guido van Rossumf9888eb1995-01-07 11:50:36 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf9888eb1995-01-07 11:50:36 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf9888eb1995-01-07 11:50:36 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf9888eb1995-01-07 11:50:36 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf9888eb1995-01-07 11:50:36 +000029
30******************************************************************/
31
32/* Module new -- create new objects of various types */
33
Guido van Rossum34162a11994-05-23 12:37:57 +000034#include "allobjects.h"
35#include "compile.h"
Guido van Rossum34162a11994-05-23 12:37:57 +000036
Guido van Rossum26b310a1996-06-17 16:56:56 +000037static char new_instance_doc[] =
38"Create an instance object from (CLASS, DICT) without calling its __init__().";
39
40static object *
41new_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 Rossumf9888eb1995-01-07 11:50:36 +000062static char new_im_doc[] =
63"Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
Guido van Rossum34162a11994-05-23 12:37:57 +000064
Guido van Rossumf9888eb1995-01-07 11:50:36 +000065static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000066new_instancemethod(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000067 object* unused;
68 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000069{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000070 object* func;
71 object* self;
72 object* classObj;
Guido van Rossum34162a11994-05-23 12:37:57 +000073
Guido van Rossumf9888eb1995-01-07 11:50:36 +000074 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
82static char new_function_doc[] =
Guido van Rossumb916faf1996-11-21 16:02:12 +000083"Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS]).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +000084
85static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000086new_function(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000087 object* unused;
88 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000089{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000090 object* code;
91 object* globals;
92 object* name = None;
Guido van Rossumb916faf1996-11-21 16:02:12 +000093 object* defaults = None;
Guido van Rossumf9888eb1995-01-07 11:50:36 +000094 funcobject* newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000095
Guido van Rossumb916faf1996-11-21 16:02:12 +000096 if (!newgetargs(args, "O!O!|SO!",
Guido van Rossumf9888eb1995-01-07 11:50:36 +000097 &Codetype, &code,
98 &Mappingtype, &globals,
99 &name,
Guido van Rossumb916faf1996-11-21 16:02:12 +0000100 &Tupletype, &defaults))
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000101 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +0000102
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000103 newfunc = (funcobject *)newfuncobject(code, globals);
104 if (newfunc == NULL)
105 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +0000106
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000107 if (name != None) {
108 XINCREF(name);
109 XDECREF(newfunc->func_name);
110 newfunc->func_name = name;
111 }
Guido van Rossumb916faf1996-11-21 16:02:12 +0000112 if (defaults != NULL) {
113 XINCREF(defaults);
114 XDECREF(newfunc->func_defaults);
115 newfunc->func_defaults = defaults;
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000116 }
Guido van Rossum34162a11994-05-23 12:37:57 +0000117
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000118 return (object *)newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +0000119}
Guido van Rossum34162a11994-05-23 12:37:57 +0000120
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000121static char new_code_doc[] =
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000122"Create a code object from (ARGCOUNT, NLOCALS, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000123
124static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000125new_code(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000126 object* unused;
127 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000128{
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000129 int argcount;
130 int nlocals;
131 int flags;
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000132 object* code;
133 object* consts;
134 object* names;
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000135 object* varnames;
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000136 object* filename;
137 object* name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000138
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000139 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 Rossum896fc7e1995-09-30 17:01:02 +0000147}
Guido van Rossum34162a11994-05-23 12:37:57 +0000148
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000149static char new_module_doc[] =
150"Create a module object from (NAME).";
151
152static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000153new_module(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000154 object* unused;
155 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000156{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000157 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000158
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000159 if (!newgetargs(args, "s", &name))
160 return NULL;
161 return newmoduleobject(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000162}
Guido van Rossum34162a11994-05-23 12:37:57 +0000163
Guido van Rossumb28911c1996-01-12 01:38:22 +0000164static char new_class_doc[] =
165"Create a class object from (NAME, BASE_CLASSES, DICT).";
166
167static object *
168new_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 Rossum34162a11994-05-23 12:37:57 +0000182static struct methodlist new_methods[] = {
Guido van Rossum26b310a1996-06-17 16:56:56 +0000183 {"instance", new_instance, 1, new_instance_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000184 {"instancemethod", new_instancemethod, 1, new_im_doc},
185 {"function", new_function, 1, new_function_doc},
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000186 {"code", new_code, 1, new_code_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000187 {"module", new_module, 1, new_module_doc},
Guido van Rossumb28911c1996-01-12 01:38:22 +0000188 {"classobj", new_class, 1, new_class_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000189 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000190};
191
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000192char new_doc[] =
193"Functions to create new objects used by the interpreter.\n\
194\n\
195You need to know a great deal about the interpreter to use this!";
196
Guido van Rossum34162a11994-05-23 12:37:57 +0000197void
198initnew()
199{
Guido van Rossum37431fb1995-01-09 17:49:26 +0000200 initmodule4("new", new_methods, new_doc, (object *)NULL,
201 PYTHON_API_VERSION);
Guido van Rossum34162a11994-05-23 12:37:57 +0000202}