blob: 98e5f723c8ea8bad04b39f24383b0b72d9f22569 [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 Rossum896fc7e1995-09-30 17:01:02 +000094#endif
Guido van Rossum34162a11994-05-23 12:37:57 +000095
Guido van Rossumf9888eb1995-01-07 11:50:36 +000096static char new_code_doc[] =
Guido van Rossum896fc7e1995-09-30 17:01:02 +000097"Create a code object from (ARGCOUNT, NLOCALS, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +000098
99static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000100new_code(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000101 object* unused;
102 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000103{
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000104 int argcount;
105 int nlocals;
106 int flags;
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000107 object* code;
108 object* consts;
109 object* names;
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000110 object* varnames;
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000111 object* filename;
112 object* name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000113
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000114#if 0
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000115 if (!newgetargs(args, "SO!O!SS",
116 &code, &Tupletype, &consts, &Tupletype, &names,
117 &filename, &name))
118 return NULL;
119 return (object *)newcodeobject(code, consts, names, filename, name);
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000120#else
121 if (!newgetargs(args, "iiiSO!O!O!SS",
122 &argcount, &nlocals, &flags, /* These are new */
123 &code, &Tupletype, &consts, &Tupletype, &names,
124 &Tupletype, &varnames, /* These are new */
125 &filename, &name))
126 return NULL;
127 return (object *)newcodeobject(argcount, nlocals, flags,
128 code, consts, names, varnames, filename, name);
Guido van Rossumb1589091995-07-28 16:44:53 +0000129#endif
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000130}
Guido van Rossum34162a11994-05-23 12:37:57 +0000131
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000132static char new_module_doc[] =
133"Create a module object from (NAME).";
134
135static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000136new_module(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000137 object* unused;
138 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000139{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000140 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000141
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000142 if (!newgetargs(args, "s", &name))
143 return NULL;
144 return newmoduleobject(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000145}
Guido van Rossum34162a11994-05-23 12:37:57 +0000146
147static struct methodlist new_methods[] = {
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000148 {"instancemethod", new_instancemethod, 1, new_im_doc},
Guido van Rossumb1589091995-07-28 16:44:53 +0000149#if 0
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000150 {"function", new_function, 1, new_function_doc},
Guido van Rossumb1589091995-07-28 16:44:53 +0000151#endif
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000152 {"code", new_code, 1, new_code_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000153 {"module", new_module, 1, new_module_doc},
154 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000155};
156
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000157char new_doc[] =
158"Functions to create new objects used by the interpreter.\n\
159\n\
160You need to know a great deal about the interpreter to use this!";
161
Guido van Rossum34162a11994-05-23 12:37:57 +0000162void
163initnew()
164{
Guido van Rossum37431fb1995-01-09 17:49:26 +0000165 initmodule4("new", new_methods, new_doc, (object *)NULL,
166 PYTHON_API_VERSION);
Guido van Rossum34162a11994-05-23 12:37:57 +0000167}