blob: 5a6e32c8037267de73cfc52f47b31087b42b8b6f [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
50static char new_function_doc[] =
51"Create a function object from (CODE, GLOBALS, [NAME, ARGCOUNT, ARGDEFS]).";
52
53static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000054new_function(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000055 object* unused;
56 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000057{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000058 object* code;
59 object* globals;
60 object* name = None;
61 int argcount = -1;
62 object* argdefs = None;
63 funcobject* newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000064
Guido van Rossumf9888eb1995-01-07 11:50:36 +000065 if (!newgetargs(args, "O!O!|SiO!",
66 &Codetype, &code,
67 &Mappingtype, &globals,
68 &name,
69 &argcount,
70 &Tupletype, &argdefs))
71 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +000072
Guido van Rossumf9888eb1995-01-07 11:50:36 +000073 newfunc = (funcobject *)newfuncobject(code, globals);
74 if (newfunc == NULL)
75 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +000076
Guido van Rossumf9888eb1995-01-07 11:50:36 +000077 if (name != None) {
78 XINCREF(name);
79 XDECREF(newfunc->func_name);
80 newfunc->func_name = name;
81 }
82 newfunc->func_argcount = argcount;
83 if (argdefs != NULL) {
84 XINCREF(argdefs);
85 XDECREF(newfunc->func_argdefs);
86 newfunc->func_argdefs = argdefs;
87 }
Guido van Rossum34162a11994-05-23 12:37:57 +000088
Guido van Rossumf9888eb1995-01-07 11:50:36 +000089 return (object *)newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000090}
Guido van Rossum34162a11994-05-23 12:37:57 +000091
Guido van Rossumf9888eb1995-01-07 11:50:36 +000092static char new_code_doc[] =
93"Create a code object from (CODESTRING, CONSTANTS, NAMES, FILENAME, NAME).";
94
95static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000096new_code(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000097 object* unused;
98 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000099{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000100 object* code;
101 object* consts;
102 object* names;
103 object* filename;
104 object* name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000105
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000106 if (!newgetargs(args, "SO!O!SS",
107 &code, &Tupletype, &consts, &Tupletype, &names,
108 &filename, &name))
109 return NULL;
110 return (object *)newcodeobject(code, consts, names, filename, name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000111}
Guido van Rossum34162a11994-05-23 12:37:57 +0000112
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000113static char new_module_doc[] =
114"Create a module object from (NAME).";
115
116static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000117new_module(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000118 object* unused;
119 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000120{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000121 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000122
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000123 if (!newgetargs(args, "s", &name))
124 return NULL;
125 return newmoduleobject(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000126}
Guido van Rossum34162a11994-05-23 12:37:57 +0000127
128static struct methodlist new_methods[] = {
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000129 {"instancemethod", new_instancemethod, 1, new_im_doc},
130 {"function", new_function, 1, new_function_doc},
131 {"code", new_code, 1, new_code_doc},
132 {"module", new_module, 1, new_module_doc},
133 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000134};
135
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000136char new_doc[] =
137"Functions to create new objects used by the interpreter.\n\
138\n\
139You need to know a great deal about the interpreter to use this!";
140
Guido van Rossum34162a11994-05-23 12:37:57 +0000141void
142initnew()
143{
Guido van Rossum37431fb1995-01-09 17:49:26 +0000144 initmodule4("new", new_methods, new_doc, (object *)NULL,
145 PYTHON_API_VERSION);
Guido van Rossum34162a11994-05-23 12:37:57 +0000146}