cpython/Modules/newmodule.c

236 lines
6.2 KiB
C
Raw Normal View History

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 */
1996-12-10 16:25:56 +00:00
#include "Python.h"
1994-05-23 12:37:57 +00:00
#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__().";
1996-12-10 16:25:56 +00:00
static PyObject *
1996-06-17 16:56:56 +00:00
new_instance(unused, args)
1996-12-10 16:25:56 +00:00
PyObject* unused;
PyObject* args;
1996-06-17 16:56:56 +00:00
{
1996-12-10 16:25:56 +00:00
PyObject* klass;
PyObject *dict;
PyInstanceObject *inst;
if (!PyArg_ParseTuple(args, "O!O!:instance",
1996-12-10 16:25:56 +00:00
&PyClass_Type, &klass,
&PyDict_Type, &dict))
1996-06-17 16:56:56 +00:00
return NULL;
inst = PyObject_New(PyInstanceObject, &PyInstance_Type);
1996-06-17 16:56:56 +00:00
if (inst == NULL)
return NULL;
1996-12-10 16:25:56 +00:00
Py_INCREF(klass);
Py_INCREF(dict);
inst->in_class = (PyClassObject *)klass;
1996-06-17 16:56:56 +00:00
inst->in_dict = dict;
PyObject_GC_Init(inst);
1996-12-10 16:25:56 +00:00
return (PyObject *)inst;
1996-06-17 16:56:56 +00:00
}
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
1996-12-10 16:25:56 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
new_instancemethod(unused, args)
1996-12-10 16:25:56 +00:00
PyObject* unused;
PyObject* args;
1994-05-23 12:37:57 +00:00
{
1996-12-10 16:25:56 +00:00
PyObject* func;
PyObject* self;
PyObject* classObj;
if (!PyArg_ParseTuple(args, "OOO!:instancemethod",
&func,
&self,
1996-12-10 16:25:56 +00:00
&PyClass_Type, &classObj))
1995-01-07 11:50:36 +00:00
return NULL;
if (!PyCallable_Check(func)) {
PyErr_SetString(PyExc_TypeError,
"first argument must be callable");
return NULL;
}
if (self == Py_None)
self = NULL;
else if (!PyInstance_Check(self)) {
PyErr_SetString(PyExc_TypeError,
"second argument must be instance or None");
return NULL;
}
1996-12-10 16:25:56 +00:00
return PyMethod_New(func, self, classObj);
1994-05-23 12:37:57 +00:00
}
1995-01-07 11:50:36 +00:00
static char new_function_doc[] =
"Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS]).";
1995-01-07 11:50:36 +00:00
1996-12-10 16:25:56 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
new_function(unused, args)
1996-12-10 16:25:56 +00:00
PyObject* unused;
PyObject* args;
1994-05-23 12:37:57 +00:00
{
1996-12-10 16:25:56 +00:00
PyObject* code;
PyObject* globals;
PyObject* name = Py_None;
PyObject* defaults = Py_None;
PyFunctionObject* newfunc;
if (!PyArg_ParseTuple(args, "O!O!|SO!:function",
1996-12-10 16:25:56 +00:00
&PyCode_Type, &code,
&PyDict_Type, &globals,
&name,
&PyTuple_Type, &defaults))
1995-01-07 11:50:36 +00:00
return NULL;
1994-05-23 12:37:57 +00:00
1996-12-10 16:25:56 +00:00
newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
1995-01-07 11:50:36 +00:00
if (newfunc == NULL)
return NULL;
1994-05-23 12:37:57 +00:00
1996-12-10 16:25:56 +00:00
if (name != Py_None) {
Py_XINCREF(name);
Py_XDECREF(newfunc->func_name);
1995-01-07 11:50:36 +00:00
newfunc->func_name = name;
}
if (defaults != Py_None) {
1996-12-10 16:25:56 +00:00
Py_XINCREF(defaults);
Py_XDECREF(newfunc->func_defaults);
newfunc->func_defaults = defaults;
1995-01-07 11:50:36 +00:00
}
1994-05-23 12:37:57 +00:00
1996-12-10 16:25:56 +00:00
return (PyObject *)newfunc;
1994-05-23 12:37:57 +00:00
}
1995-01-07 11:50:36 +00:00
static char new_code_doc[] =
"Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB).";
1995-01-07 11:50:36 +00:00
1996-12-10 16:25:56 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
new_code(unused, args)
1996-12-10 16:25:56 +00:00
PyObject* unused;
PyObject* args;
1994-05-23 12:37:57 +00:00
{
1995-09-30 17:01:02 +00:00
int argcount;
int nlocals;
1997-01-17 21:12:06 +00:00
int stacksize;
1995-09-30 17:01:02 +00:00
int flags;
1996-12-10 16:25:56 +00:00
PyObject* code;
PyObject* consts;
PyObject* names;
PyObject* varnames;
PyObject* filename;
PyObject* name;
int firstlineno;
PyObject* lnotab;
PyBufferProcs *pb;
if (!PyArg_ParseTuple(args, "iiiiOO!O!O!SSiS:code",
1997-01-17 21:12:06 +00:00
&argcount, &nlocals, &stacksize, &flags,
1996-12-10 16:25:56 +00:00
&code,
&PyTuple_Type, &consts,
&PyTuple_Type, &names,
1997-01-17 21:12:06 +00:00
&PyTuple_Type, &varnames,
&filename, &name,
&firstlineno, &lnotab))
1995-09-30 17:01:02 +00:00
return NULL;
pb = code->ob_type->tp_as_buffer;
if (pb == NULL ||
pb->bf_getreadbuffer == NULL ||
pb->bf_getsegcount == NULL ||
(*pb->bf_getsegcount)(code, NULL) != 1)
{
PyErr_SetString(PyExc_TypeError,
"bytecode object must be a single-segment read-only buffer");
return NULL;
}
1997-01-17 21:12:06 +00:00
return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
1996-12-10 16:25:56 +00:00
code, consts, names, varnames,
filename, name, firstlineno, lnotab);
1995-09-30 17:01:02 +00:00
}
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).";
1996-12-10 16:25:56 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
new_module(unused, args)
1996-12-10 16:25:56 +00:00
PyObject* unused;
PyObject* 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
if (!PyArg_ParseTuple(args, "s:module", &name))
1995-01-07 11:50:36 +00:00
return NULL;
1996-12-10 16:25:56 +00:00
return PyModule_New(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).";
1996-12-10 16:25:56 +00:00
static PyObject *
1996-01-12 01:38:22 +00:00
new_class(unused, args)
1996-12-10 16:25:56 +00:00
PyObject* unused;
PyObject* args;
1996-01-12 01:38:22 +00:00
{
1996-12-10 16:25:56 +00:00
PyObject * name;
PyObject * classes;
PyObject * dict;
1996-01-12 01:38:22 +00:00
if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes,
1996-12-10 16:25:56 +00:00
&PyDict_Type, &dict))
1996-01-12 01:38:22 +00:00
return NULL;
1996-12-10 16:25:56 +00:00
return PyClass_New(classes, dict, name);
1996-01-12 01:38:22 +00:00
}
1996-12-10 16:25:56 +00:00
static PyMethodDef 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!";
DL_EXPORT(void)
1994-05-23 12:37:57 +00:00
initnew()
{
1996-12-10 16:25:56 +00:00
Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
PYTHON_API_VERSION);
1994-05-23 12:37:57 +00:00
}