blob: 0f1e1760e0e78e359209ad62a0f84ca28046e85e [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
Guido van Rossumb1589091995-07-28 16:44:53 +000082/* XXX These internal interfaces have changed -- who'll fix this code? */
83#if 0
84
Guido van Rossumf9888eb1995-01-07 11:50:36 +000085static char new_function_doc[] =
86"Create a function object from (CODE, GLOBALS, [NAME, ARGCOUNT, ARGDEFS]).";
87
88static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000089new_function(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000090 object* unused;
91 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000092{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000093 object* code;
94 object* globals;
95 object* name = None;
96 int argcount = -1;
97 object* argdefs = None;
98 funcobject* newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000099
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000100 if (!newgetargs(args, "O!O!|SiO!",
101 &Codetype, &code,
102 &Mappingtype, &globals,
103 &name,
104 &argcount,
105 &Tupletype, &argdefs))
106 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +0000107
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000108 newfunc = (funcobject *)newfuncobject(code, globals);
109 if (newfunc == NULL)
110 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +0000111
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000112 if (name != None) {
113 XINCREF(name);
114 XDECREF(newfunc->func_name);
115 newfunc->func_name = name;
116 }
117 newfunc->func_argcount = argcount;
118 if (argdefs != NULL) {
119 XINCREF(argdefs);
120 XDECREF(newfunc->func_argdefs);
121 newfunc->func_argdefs = argdefs;
122 }
Guido van Rossum34162a11994-05-23 12:37:57 +0000123
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000124 return (object *)newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +0000125}
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000126#endif
Guido van Rossum34162a11994-05-23 12:37:57 +0000127
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000128static char new_code_doc[] =
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000129"Create a code object from (ARGCOUNT, NLOCALS, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000130
131static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000132new_code(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000133 object* unused;
134 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000135{
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000136 int argcount;
137 int nlocals;
138 int flags;
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000139 object* code;
140 object* consts;
141 object* names;
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000142 object* varnames;
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000143 object* filename;
144 object* name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000145
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000146#if 0
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000147 if (!newgetargs(args, "SO!O!SS",
148 &code, &Tupletype, &consts, &Tupletype, &names,
149 &filename, &name))
150 return NULL;
151 return (object *)newcodeobject(code, consts, names, filename, name);
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000152#else
153 if (!newgetargs(args, "iiiSO!O!O!SS",
154 &argcount, &nlocals, &flags, /* These are new */
155 &code, &Tupletype, &consts, &Tupletype, &names,
156 &Tupletype, &varnames, /* These are new */
157 &filename, &name))
158 return NULL;
159 return (object *)newcodeobject(argcount, nlocals, flags,
160 code, consts, names, varnames, filename, name);
Guido van Rossumb1589091995-07-28 16:44:53 +0000161#endif
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000162}
Guido van Rossum34162a11994-05-23 12:37:57 +0000163
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000164static char new_module_doc[] =
165"Create a module object from (NAME).";
166
167static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000168new_module(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000169 object* unused;
170 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000171{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000172 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000173
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000174 if (!newgetargs(args, "s", &name))
175 return NULL;
176 return newmoduleobject(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000177}
Guido van Rossum34162a11994-05-23 12:37:57 +0000178
Guido van Rossumb28911c1996-01-12 01:38:22 +0000179static char new_class_doc[] =
180"Create a class object from (NAME, BASE_CLASSES, DICT).";
181
182static object *
183new_class(unused, args)
184 object* unused;
185 object* args;
186{
187 object * name;
188 object * classes;
189 object * dict;
190
191 if (!newgetargs(args, "SO!O!", &name, &Tupletype, &classes,
192 &Mappingtype, &dict))
193 return NULL;
194 return newclassobject(classes, dict, name);
195}
196
Guido van Rossum34162a11994-05-23 12:37:57 +0000197static struct methodlist new_methods[] = {
Guido van Rossum26b310a1996-06-17 16:56:56 +0000198 {"instance", new_instance, 1, new_instance_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000199 {"instancemethod", new_instancemethod, 1, new_im_doc},
Guido van Rossumb1589091995-07-28 16:44:53 +0000200#if 0
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000201 {"function", new_function, 1, new_function_doc},
Guido van Rossumb1589091995-07-28 16:44:53 +0000202#endif
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000203 {"code", new_code, 1, new_code_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000204 {"module", new_module, 1, new_module_doc},
Guido van Rossumb28911c1996-01-12 01:38:22 +0000205 {"classobj", new_class, 1, new_class_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000206 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000207};
208
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000209char new_doc[] =
210"Functions to create new objects used by the interpreter.\n\
211\n\
212You need to know a great deal about the interpreter to use this!";
213
Guido van Rossum34162a11994-05-23 12:37:57 +0000214void
215initnew()
216{
Guido van Rossum37431fb1995-01-09 17:49:26 +0000217 initmodule4("new", new_methods, new_doc, (object *)NULL,
218 PYTHON_API_VERSION);
Guido van Rossum34162a11994-05-23 12:37:57 +0000219}