blob: 0591d3ccc0ce9fb357ad1dcbafc1f17d2e7af4c1 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF 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 Rossum34162a11994-05-23 12:37:57 +000027#include "allobjects.h"
28#include "compile.h"
Guido van Rossum34162a11994-05-23 12:37:57 +000029
Guido van Rossum26b310a1996-06-17 16:56:56 +000030static char new_instance_doc[] =
31"Create an instance object from (CLASS, DICT) without calling its __init__().";
32
33static object *
34new_instance(unused, args)
35 object* unused;
36 object* args;
37{
38 object* klass;
39 object *dict;
40 instanceobject *inst;
41 if (!newgetargs(args, "O!O!",
42 &Classtype, &klass,
43 &Dicttype, &dict))
44 return NULL;
45 inst = NEWOBJ(instanceobject, &Instancetype);
46 if (inst == NULL)
47 return NULL;
48 INCREF(klass);
49 INCREF(dict);
50 inst->in_class = (classobject *)klass;
51 inst->in_dict = dict;
52 return (object *)inst;
53}
54
Guido van Rossumf9888eb1995-01-07 11:50:36 +000055static char new_im_doc[] =
56"Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
Guido van Rossum34162a11994-05-23 12:37:57 +000057
Guido van Rossumf9888eb1995-01-07 11:50:36 +000058static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000059new_instancemethod(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000060 object* unused;
61 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000062{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000063 object* func;
64 object* self;
65 object* classObj;
Guido van Rossum34162a11994-05-23 12:37:57 +000066
Guido van Rossumf9888eb1995-01-07 11:50:36 +000067 if (!newgetargs(args, "O!O!O!",
68 &Functype, &func,
69 &Instancetype, &self,
70 &Classtype, &classObj))
71 return NULL;
72 return newinstancemethodobject(func, self, classObj);
73}
74
Guido van Rossumb1589091995-07-28 16:44:53 +000075/* XXX These internal interfaces have changed -- who'll fix this code? */
76#if 0
77
Guido van Rossumf9888eb1995-01-07 11:50:36 +000078static char new_function_doc[] =
79"Create a function object from (CODE, GLOBALS, [NAME, ARGCOUNT, ARGDEFS]).";
80
81static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000082new_function(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000083 object* unused;
84 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000085{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000086 object* code;
87 object* globals;
88 object* name = None;
89 int argcount = -1;
90 object* argdefs = None;
91 funcobject* newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000092
Guido van Rossumf9888eb1995-01-07 11:50:36 +000093 if (!newgetargs(args, "O!O!|SiO!",
94 &Codetype, &code,
95 &Mappingtype, &globals,
96 &name,
97 &argcount,
98 &Tupletype, &argdefs))
99 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +0000100
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000101 newfunc = (funcobject *)newfuncobject(code, globals);
102 if (newfunc == NULL)
103 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +0000104
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000105 if (name != None) {
106 XINCREF(name);
107 XDECREF(newfunc->func_name);
108 newfunc->func_name = name;
109 }
110 newfunc->func_argcount = argcount;
111 if (argdefs != NULL) {
112 XINCREF(argdefs);
113 XDECREF(newfunc->func_argdefs);
114 newfunc->func_argdefs = argdefs;
115 }
Guido van Rossum34162a11994-05-23 12:37:57 +0000116
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000117 return (object *)newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +0000118}
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000119#endif
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 0
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000140 if (!newgetargs(args, "SO!O!SS",
141 &code, &Tupletype, &consts, &Tupletype, &names,
142 &filename, &name))
143 return NULL;
144 return (object *)newcodeobject(code, consts, names, filename, name);
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000145#else
146 if (!newgetargs(args, "iiiSO!O!O!SS",
147 &argcount, &nlocals, &flags, /* These are new */
148 &code, &Tupletype, &consts, &Tupletype, &names,
149 &Tupletype, &varnames, /* These are new */
150 &filename, &name))
151 return NULL;
152 return (object *)newcodeobject(argcount, nlocals, flags,
153 code, consts, names, varnames, filename, name);
Guido van Rossumb1589091995-07-28 16:44:53 +0000154#endif
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000155}
Guido van Rossum34162a11994-05-23 12:37:57 +0000156
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000157static char new_module_doc[] =
158"Create a module object from (NAME).";
159
160static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000161new_module(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000162 object* unused;
163 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000164{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000165 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000166
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000167 if (!newgetargs(args, "s", &name))
168 return NULL;
169 return newmoduleobject(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000170}
Guido van Rossum34162a11994-05-23 12:37:57 +0000171
Guido van Rossumb28911c1996-01-12 01:38:22 +0000172static char new_class_doc[] =
173"Create a class object from (NAME, BASE_CLASSES, DICT).";
174
175static object *
176new_class(unused, args)
177 object* unused;
178 object* args;
179{
180 object * name;
181 object * classes;
182 object * dict;
183
184 if (!newgetargs(args, "SO!O!", &name, &Tupletype, &classes,
185 &Mappingtype, &dict))
186 return NULL;
187 return newclassobject(classes, dict, name);
188}
189
Guido van Rossum34162a11994-05-23 12:37:57 +0000190static struct methodlist new_methods[] = {
Guido van Rossum26b310a1996-06-17 16:56:56 +0000191 {"instance", new_instance, 1, new_instance_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000192 {"instancemethod", new_instancemethod, 1, new_im_doc},
Guido van Rossumb1589091995-07-28 16:44:53 +0000193#if 0
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000194 {"function", new_function, 1, new_function_doc},
Guido van Rossumb1589091995-07-28 16:44:53 +0000195#endif
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000196 {"code", new_code, 1, new_code_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000197 {"module", new_module, 1, new_module_doc},
Guido van Rossumb28911c1996-01-12 01:38:22 +0000198 {"classobj", new_class, 1, new_class_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000199 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000200};
201
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000202char new_doc[] =
203"Functions to create new objects used by the interpreter.\n\
204\n\
205You need to know a great deal about the interpreter to use this!";
206
Guido van Rossum34162a11994-05-23 12:37:57 +0000207void
208initnew()
209{
Guido van Rossum37431fb1995-01-09 17:49:26 +0000210 initmodule4("new", new_methods, new_doc, (object *)NULL,
211 PYTHON_API_VERSION);
Guido van Rossum34162a11994-05-23 12:37:57 +0000212}