blob: 92d7870ab069bc581b78f7acfbca287aa5b92f5b [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 Rossumf9888eb1995-01-07 11:50:36 +000030static char new_im_doc[] =
31"Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
Guido van Rossum34162a11994-05-23 12:37:57 +000032
Guido van Rossumf9888eb1995-01-07 11:50:36 +000033static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000034new_instancemethod(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000035 object* unused;
36 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000037{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000038 object* func;
39 object* self;
40 object* classObj;
Guido van Rossum34162a11994-05-23 12:37:57 +000041
Guido van Rossumf9888eb1995-01-07 11:50:36 +000042 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 Rossumb1589091995-07-28 16:44:53 +000050/* XXX These internal interfaces have changed -- who'll fix this code? */
51#if 0
52
Guido van Rossumf9888eb1995-01-07 11:50:36 +000053static char new_function_doc[] =
54"Create a function object from (CODE, GLOBALS, [NAME, ARGCOUNT, ARGDEFS]).";
55
56static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000057new_function(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000058 object* unused;
59 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000060{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000061 object* code;
62 object* globals;
63 object* name = None;
64 int argcount = -1;
65 object* argdefs = None;
66 funcobject* newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000067
Guido van Rossumf9888eb1995-01-07 11:50:36 +000068 if (!newgetargs(args, "O!O!|SiO!",
69 &Codetype, &code,
70 &Mappingtype, &globals,
71 &name,
72 &argcount,
73 &Tupletype, &argdefs))
74 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +000075
Guido van Rossumf9888eb1995-01-07 11:50:36 +000076 newfunc = (funcobject *)newfuncobject(code, globals);
77 if (newfunc == NULL)
78 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +000079
Guido van Rossumf9888eb1995-01-07 11:50:36 +000080 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 Rossum34162a11994-05-23 12:37:57 +000091
Guido van Rossumf9888eb1995-01-07 11:50:36 +000092 return (object *)newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000093}
Guido van Rossum34162a11994-05-23 12:37:57 +000094
Guido van Rossumf9888eb1995-01-07 11:50:36 +000095static char new_code_doc[] =
96"Create a code object from (CODESTRING, CONSTANTS, NAMES, FILENAME, NAME).";
97
98static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000099new_code(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000100 object* unused;
101 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000102{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000103 object* code;
104 object* consts;
105 object* names;
106 object* filename;
107 object* name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000108
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000109 if (!newgetargs(args, "SO!O!SS",
110 &code, &Tupletype, &consts, &Tupletype, &names,
111 &filename, &name))
112 return NULL;
113 return (object *)newcodeobject(code, consts, names, filename, name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000114}
Guido van Rossumb1589091995-07-28 16:44:53 +0000115#endif
Guido van Rossum34162a11994-05-23 12:37:57 +0000116
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000117static char new_module_doc[] =
118"Create a module object from (NAME).";
119
120static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000121new_module(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000122 object* unused;
123 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000124{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000125 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000126
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000127 if (!newgetargs(args, "s", &name))
128 return NULL;
129 return newmoduleobject(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000130}
Guido van Rossum34162a11994-05-23 12:37:57 +0000131
132static struct methodlist new_methods[] = {
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000133 {"instancemethod", new_instancemethod, 1, new_im_doc},
Guido van Rossumb1589091995-07-28 16:44:53 +0000134#if 0
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000135 {"function", new_function, 1, new_function_doc},
136 {"code", new_code, 1, new_code_doc},
Guido van Rossumb1589091995-07-28 16:44:53 +0000137#endif
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000138 {"module", new_module, 1, new_module_doc},
139 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000140};
141
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000142char new_doc[] =
143"Functions to create new objects used by the interpreter.\n\
144\n\
145You need to know a great deal about the interpreter to use this!";
146
Guido van Rossum34162a11994-05-23 12:37:57 +0000147void
148initnew()
149{
Guido van Rossum37431fb1995-01-09 17:49:26 +0000150 initmodule4("new", new_methods, new_doc, (object *)NULL,
151 PYTHON_API_VERSION);
Guido van Rossum34162a11994-05-23 12:37:57 +0000152}