blob: f237a3a14d136583e8788af978040f69f4d6a9b3 [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"
29#include "modsupport.h"
30
Guido van Rossumf9888eb1995-01-07 11:50:36 +000031static char new_im_doc[] =
32"Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
Guido van Rossum34162a11994-05-23 12:37:57 +000033
Guido van Rossumf9888eb1995-01-07 11:50:36 +000034static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000035new_instancemethod(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000036 object* unused;
37 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000038{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000039 object* func;
40 object* self;
41 object* classObj;
Guido van Rossum34162a11994-05-23 12:37:57 +000042
Guido van Rossumf9888eb1995-01-07 11:50:36 +000043 if (!newgetargs(args, "O!O!O!",
44 &Functype, &func,
45 &Instancetype, &self,
46 &Classtype, &classObj))
47 return NULL;
48 return newinstancemethodobject(func, self, classObj);
49}
50
51static char new_function_doc[] =
52"Create a function object from (CODE, GLOBALS, [NAME, ARGCOUNT, ARGDEFS]).";
53
54static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000055new_function(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000056 object* unused;
57 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000058{
Guido van Rossumf9888eb1995-01-07 11:50:36 +000059 object* code;
60 object* globals;
61 object* name = None;
62 int argcount = -1;
63 object* argdefs = None;
64 funcobject* newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000065
Guido van Rossumf9888eb1995-01-07 11:50:36 +000066 if (!newgetargs(args, "O!O!|SiO!",
67 &Codetype, &code,
68 &Mappingtype, &globals,
69 &name,
70 &argcount,
71 &Tupletype, &argdefs))
72 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +000073
Guido van Rossumf9888eb1995-01-07 11:50:36 +000074 newfunc = (funcobject *)newfuncobject(code, globals);
75 if (newfunc == NULL)
76 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +000077
Guido van Rossumf9888eb1995-01-07 11:50:36 +000078 if (name != None) {
79 XINCREF(name);
80 XDECREF(newfunc->func_name);
81 newfunc->func_name = name;
82 }
83 newfunc->func_argcount = argcount;
84 if (argdefs != NULL) {
85 XINCREF(argdefs);
86 XDECREF(newfunc->func_argdefs);
87 newfunc->func_argdefs = argdefs;
88 }
Guido van Rossum34162a11994-05-23 12:37:57 +000089
Guido van Rossumf9888eb1995-01-07 11:50:36 +000090 return (object *)newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +000091}
Guido van Rossum34162a11994-05-23 12:37:57 +000092
Guido van Rossumf9888eb1995-01-07 11:50:36 +000093static char new_code_doc[] =
94"Create a code object from (CODESTRING, CONSTANTS, NAMES, FILENAME, NAME).";
95
96static object *
Guido van Rossum34162a11994-05-23 12:37:57 +000097new_code(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +000098 object* unused;
99 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000100{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000101 object* code;
102 object* consts;
103 object* names;
104 object* filename;
105 object* name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000106
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000107 if (!newgetargs(args, "SO!O!SS",
108 &code, &Tupletype, &consts, &Tupletype, &names,
109 &filename, &name))
110 return NULL;
111 return (object *)newcodeobject(code, consts, names, filename, name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000112}
Guido van Rossum34162a11994-05-23 12:37:57 +0000113
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000114static char new_module_doc[] =
115"Create a module object from (NAME).";
116
117static object *
Guido van Rossum34162a11994-05-23 12:37:57 +0000118new_module(unused, args)
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000119 object* unused;
120 object* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000121{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000122 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000123
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000124 if (!newgetargs(args, "s", &name))
125 return NULL;
126 return newmoduleobject(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000127}
Guido van Rossum34162a11994-05-23 12:37:57 +0000128
129static struct methodlist new_methods[] = {
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000130 {"instancemethod", new_instancemethod, 1, new_im_doc},
131 {"function", new_function, 1, new_function_doc},
132 {"code", new_code, 1, new_code_doc},
133 {"module", new_module, 1, new_module_doc},
134 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000135};
136
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000137char new_doc[] =
138"Functions to create new objects used by the interpreter.\n\
139\n\
140You need to know a great deal about the interpreter to use this!";
141
Guido van Rossum34162a11994-05-23 12:37:57 +0000142void
143initnew()
144{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000145 initmodule3("new", new_methods, new_doc, (object *)NULL);
Guido van Rossum34162a11994-05-23 12:37:57 +0000146}