1995-01-07 11:50:36 +00:00
|
|
|
/***********************************************************
|
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1995-01-07 11:50:36 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1995-01-07 11:50:36 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
1995-01-07 11:50:36 +00:00
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1995-01-07 11:50:36 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Module new -- create new objects of various types */
|
|
|
|
|
1994-05-23 12:37:57 +00:00
|
|
|
#include "allobjects.h"
|
|
|
|
#include "compile.h"
|
|
|
|
|
1996-06-17 16:56:56 +00:00
|
|
|
static char new_instance_doc[] =
|
|
|
|
"Create an instance object from (CLASS, DICT) without calling its __init__().";
|
|
|
|
|
|
|
|
static object *
|
|
|
|
new_instance(unused, args)
|
|
|
|
object* unused;
|
|
|
|
object* args;
|
|
|
|
{
|
|
|
|
object* klass;
|
|
|
|
object *dict;
|
|
|
|
instanceobject *inst;
|
|
|
|
if (!newgetargs(args, "O!O!",
|
|
|
|
&Classtype, &klass,
|
|
|
|
&Dicttype, &dict))
|
|
|
|
return NULL;
|
|
|
|
inst = NEWOBJ(instanceobject, &Instancetype);
|
|
|
|
if (inst == NULL)
|
|
|
|
return NULL;
|
|
|
|
INCREF(klass);
|
|
|
|
INCREF(dict);
|
|
|
|
inst->in_class = (classobject *)klass;
|
|
|
|
inst->in_dict = dict;
|
|
|
|
return (object *)inst;
|
|
|
|
}
|
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
static char new_im_doc[] =
|
|
|
|
"Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
|
1994-05-23 12:37:57 +00:00
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
static object *
|
1994-05-23 12:37:57 +00:00
|
|
|
new_instancemethod(unused, args)
|
1995-01-07 11:50:36 +00:00
|
|
|
object* unused;
|
|
|
|
object* args;
|
1994-05-23 12:37:57 +00:00
|
|
|
{
|
1995-01-07 11:50:36 +00:00
|
|
|
object* func;
|
|
|
|
object* self;
|
|
|
|
object* classObj;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "O!O!O!",
|
|
|
|
&Functype, &func,
|
|
|
|
&Instancetype, &self,
|
|
|
|
&Classtype, &classObj))
|
|
|
|
return NULL;
|
|
|
|
return newinstancemethodobject(func, self, classObj);
|
1994-05-23 12:37:57 +00:00
|
|
|
}
|
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
static char new_function_doc[] =
|
1996-11-21 16:02:12 +00:00
|
|
|
"Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS]).";
|
1995-01-07 11:50:36 +00:00
|
|
|
|
|
|
|
static object *
|
1994-05-23 12:37:57 +00:00
|
|
|
new_function(unused, args)
|
1995-01-07 11:50:36 +00:00
|
|
|
object* unused;
|
|
|
|
object* args;
|
1994-05-23 12:37:57 +00:00
|
|
|
{
|
1995-01-07 11:50:36 +00:00
|
|
|
object* code;
|
|
|
|
object* globals;
|
|
|
|
object* name = None;
|
1996-11-21 16:02:12 +00:00
|
|
|
object* defaults = None;
|
1995-01-07 11:50:36 +00:00
|
|
|
funcobject* newfunc;
|
1994-05-23 12:37:57 +00:00
|
|
|
|
1996-11-21 16:02:12 +00:00
|
|
|
if (!newgetargs(args, "O!O!|SO!",
|
1995-01-07 11:50:36 +00:00
|
|
|
&Codetype, &code,
|
|
|
|
&Mappingtype, &globals,
|
|
|
|
&name,
|
1996-11-21 16:02:12 +00:00
|
|
|
&Tupletype, &defaults))
|
1995-01-07 11:50:36 +00:00
|
|
|
return NULL;
|
1994-05-23 12:37:57 +00:00
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
newfunc = (funcobject *)newfuncobject(code, globals);
|
|
|
|
if (newfunc == NULL)
|
|
|
|
return NULL;
|
1994-05-23 12:37:57 +00:00
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
if (name != None) {
|
|
|
|
XINCREF(name);
|
|
|
|
XDECREF(newfunc->func_name);
|
|
|
|
newfunc->func_name = name;
|
|
|
|
}
|
1996-11-21 16:02:12 +00:00
|
|
|
if (defaults != NULL) {
|
|
|
|
XINCREF(defaults);
|
|
|
|
XDECREF(newfunc->func_defaults);
|
|
|
|
newfunc->func_defaults = defaults;
|
1995-01-07 11:50:36 +00:00
|
|
|
}
|
1994-05-23 12:37:57 +00:00
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
return (object *)newfunc;
|
1994-05-23 12:37:57 +00:00
|
|
|
}
|
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
static char new_code_doc[] =
|
1995-09-30 17:01:02 +00:00
|
|
|
"Create a code object from (ARGCOUNT, NLOCALS, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME).";
|
1995-01-07 11:50:36 +00:00
|
|
|
|
|
|
|
static object *
|
1994-05-23 12:37:57 +00:00
|
|
|
new_code(unused, args)
|
1995-01-07 11:50:36 +00:00
|
|
|
object* unused;
|
|
|
|
object* args;
|
1994-05-23 12:37:57 +00:00
|
|
|
{
|
1995-09-30 17:01:02 +00:00
|
|
|
int argcount;
|
|
|
|
int nlocals;
|
|
|
|
int flags;
|
1995-01-07 11:50:36 +00:00
|
|
|
object* code;
|
|
|
|
object* consts;
|
|
|
|
object* names;
|
1995-09-30 17:01:02 +00:00
|
|
|
object* varnames;
|
1995-01-07 11:50:36 +00:00
|
|
|
object* filename;
|
|
|
|
object* name;
|
1994-05-23 12:37:57 +00:00
|
|
|
|
1995-09-30 17:01:02 +00:00
|
|
|
if (!newgetargs(args, "iiiSO!O!O!SS",
|
|
|
|
&argcount, &nlocals, &flags, /* These are new */
|
|
|
|
&code, &Tupletype, &consts, &Tupletype, &names,
|
|
|
|
&Tupletype, &varnames, /* These are new */
|
|
|
|
&filename, &name))
|
|
|
|
return NULL;
|
|
|
|
return (object *)newcodeobject(argcount, nlocals, flags,
|
|
|
|
code, consts, names, varnames, filename, name);
|
|
|
|
}
|
1994-05-23 12:37:57 +00:00
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
static char new_module_doc[] =
|
|
|
|
"Create a module object from (NAME).";
|
|
|
|
|
|
|
|
static object *
|
1994-05-23 12:37:57 +00:00
|
|
|
new_module(unused, args)
|
1995-01-07 11:50:36 +00:00
|
|
|
object* unused;
|
|
|
|
object* args;
|
1994-05-23 12:37:57 +00:00
|
|
|
{
|
1995-01-07 11:50:36 +00:00
|
|
|
char *name;
|
1994-05-23 12:37:57 +00:00
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
if (!newgetargs(args, "s", &name))
|
|
|
|
return NULL;
|
|
|
|
return newmoduleobject(name);
|
1994-05-23 12:37:57 +00:00
|
|
|
}
|
|
|
|
|
1996-01-12 01:38:22 +00:00
|
|
|
static char new_class_doc[] =
|
|
|
|
"Create a class object from (NAME, BASE_CLASSES, DICT).";
|
|
|
|
|
|
|
|
static object *
|
|
|
|
new_class(unused, args)
|
|
|
|
object* unused;
|
|
|
|
object* args;
|
|
|
|
{
|
|
|
|
object * name;
|
|
|
|
object * classes;
|
|
|
|
object * dict;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "SO!O!", &name, &Tupletype, &classes,
|
|
|
|
&Mappingtype, &dict))
|
|
|
|
return NULL;
|
|
|
|
return newclassobject(classes, dict, name);
|
|
|
|
}
|
|
|
|
|
1994-05-23 12:37:57 +00:00
|
|
|
static struct methodlist new_methods[] = {
|
1996-06-17 16:56:56 +00:00
|
|
|
{"instance", new_instance, 1, new_instance_doc},
|
1995-01-07 11:50:36 +00:00
|
|
|
{"instancemethod", new_instancemethod, 1, new_im_doc},
|
|
|
|
{"function", new_function, 1, new_function_doc},
|
1995-09-30 17:01:02 +00:00
|
|
|
{"code", new_code, 1, new_code_doc},
|
1995-01-07 11:50:36 +00:00
|
|
|
{"module", new_module, 1, new_module_doc},
|
1996-01-12 01:38:22 +00:00
|
|
|
{"classobj", new_class, 1, new_class_doc},
|
1995-01-07 11:50:36 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
1994-05-23 12:37:57 +00:00
|
|
|
};
|
|
|
|
|
1995-01-07 11:50:36 +00:00
|
|
|
char new_doc[] =
|
|
|
|
"Functions to create new objects used by the interpreter.\n\
|
|
|
|
\n\
|
|
|
|
You need to know a great deal about the interpreter to use this!";
|
|
|
|
|
1994-05-23 12:37:57 +00:00
|
|
|
void
|
|
|
|
initnew()
|
|
|
|
{
|
1995-01-09 17:49:26 +00:00
|
|
|
initmodule4("new", new_methods, new_doc, (object *)NULL,
|
|
|
|
PYTHON_API_VERSION);
|
1994-05-23 12:37:57 +00:00
|
|
|
}
|