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[] =
|
2001-01-28 03:55:09 +00:00
|
|
|
"Create an instance object from (CLASS [, DICT]) without calling its\n\
|
|
|
|
__init__() method. DICT must be a dictionary or None.";
|
1996-06-17 16:56:56 +00:00
|
|
|
|
1996-12-10 16:25:56 +00:00
|
|
|
static PyObject *
|
2000-07-10 11:56:03 +00:00
|
|
|
new_instance(PyObject* unused, PyObject* args)
|
1996-06-17 16:56:56 +00:00
|
|
|
{
|
2001-01-28 03:55:09 +00:00
|
|
|
PyObject *klass;
|
|
|
|
PyObject *dict = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O!|O:instance",
|
|
|
|
&PyClass_Type, &klass, &dict))
|
1996-06-17 16:56:56 +00:00
|
|
|
return NULL;
|
2001-01-28 03:55:09 +00:00
|
|
|
|
|
|
|
if (dict == Py_None)
|
|
|
|
dict = NULL;
|
|
|
|
else if (dict == NULL)
|
|
|
|
/* do nothing */;
|
|
|
|
else if (!PyDict_Check(dict)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"new.instance() second arg must be dictionary or None");
|
1996-06-17 16:56:56 +00:00
|
|
|
return NULL;
|
2001-01-28 03:55:09 +00:00
|
|
|
}
|
|
|
|
return PyInstance_NewRaw(klass, dict);
|
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 *
|
2000-07-10 11:56:03 +00:00
|
|
|
new_instancemethod(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;
|
|
|
|
|
2002-01-15 19:21:05 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "OOO:instancemethod",
|
|
|
|
&func, &self, &classObj))
|
1995-01-07 11:50:36 +00:00
|
|
|
return NULL;
|
1998-07-08 14:58:16 +00:00
|
|
|
if (!PyCallable_Check(func)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"first argument must be callable");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (self == Py_None)
|
|
|
|
self = 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[] =
|
2000-10-10 22:07:18 +00:00
|
|
|
"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 *
|
2000-07-10 11:56:03 +00:00
|
|
|
new_function(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;
|
|
|
|
|
2000-11-13 20:29:20 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "O!O!|OO!: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;
|
2000-11-13 20:29:20 +00:00
|
|
|
if (name != Py_None && !PyString_Check(name)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 3 (name) must be None or string");
|
|
|
|
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;
|
|
|
|
}
|
1998-05-22 00:57:31 +00:00
|
|
|
if (defaults != Py_None) {
|
1996-12-10 16:25:56 +00:00
|
|
|
Py_XINCREF(defaults);
|
|
|
|
Py_XDECREF(newfunc->func_defaults);
|
1996-11-21 16:02:12 +00:00
|
|
|
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[] =
|
2001-02-01 19:50:29 +00:00
|
|
|
"Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING,\n"
|
|
|
|
"CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB, FREEVARS,\n"
|
|
|
|
"CELLVARS).";
|
1995-01-07 11:50:36 +00:00
|
|
|
|
1996-12-10 16:25:56 +00:00
|
|
|
static PyObject *
|
2000-07-10 11:56:03 +00:00
|
|
|
new_code(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;
|
2001-02-01 19:50:29 +00:00
|
|
|
PyObject* freevars = NULL;
|
|
|
|
PyObject* cellvars = NULL;
|
1996-12-10 16:25:56 +00:00
|
|
|
PyObject* filename;
|
|
|
|
PyObject* name;
|
1997-01-24 03:58:52 +00:00
|
|
|
int firstlineno;
|
|
|
|
PyObject* lnotab;
|
1998-10-07 19:42:25 +00:00
|
|
|
|
2001-02-01 19:50:29 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!: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,
|
1997-01-24 03:58:52 +00:00
|
|
|
&filename, &name,
|
2001-02-01 19:50:29 +00:00
|
|
|
&firstlineno, &lnotab,
|
|
|
|
&PyTuple_Type, &freevars,
|
|
|
|
&PyTuple_Type, &cellvars))
|
1995-09-30 17:01:02 +00:00
|
|
|
return NULL;
|
1998-10-07 19:42:25 +00:00
|
|
|
|
2001-02-01 19:50:29 +00:00
|
|
|
if (freevars == NULL || cellvars == NULL) {
|
|
|
|
PyObject *empty = PyTuple_New(0);
|
|
|
|
if (empty == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (freevars == NULL) {
|
|
|
|
freevars = empty;
|
|
|
|
Py_INCREF(freevars);
|
|
|
|
}
|
|
|
|
if (cellvars == NULL) {
|
|
|
|
cellvars = empty;
|
|
|
|
Py_INCREF(cellvars);
|
|
|
|
}
|
|
|
|
Py_DECREF(empty);
|
|
|
|
}
|
|
|
|
|
2001-11-09 22:02:48 +00:00
|
|
|
if (!PyObject_CheckReadBuffer(code)) {
|
1998-10-07 19:42:25 +00:00
|
|
|
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,
|
2001-01-25 20:07:56 +00:00
|
|
|
freevars, cellvars, 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 *
|
2000-07-10 11:56:03 +00:00
|
|
|
new_module(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
|
|
|
|
2000-02-29 13:59:29 +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 *
|
2000-07-10 11:56:03 +00:00
|
|
|
new_class(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
|
|
|
|
2000-02-29 13:59:29 +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[] = {
|
2000-08-03 02:06:16 +00:00
|
|
|
{"instance", new_instance,
|
|
|
|
METH_VARARGS, new_instance_doc},
|
|
|
|
{"instancemethod", new_instancemethod,
|
|
|
|
METH_VARARGS, new_im_doc},
|
|
|
|
{"function", new_function,
|
|
|
|
METH_VARARGS, new_function_doc},
|
|
|
|
{"code", new_code,
|
|
|
|
METH_VARARGS, new_code_doc},
|
|
|
|
{"module", new_module,
|
|
|
|
METH_VARARGS, new_module_doc},
|
|
|
|
{"classobj", new_class,
|
|
|
|
METH_VARARGS, new_class_doc},
|
1995-01-07 11:50:36 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
1994-05-23 12:37:57 +00:00
|
|
|
};
|
|
|
|
|
2001-10-21 22:26:43 +00:00
|
|
|
static char new_doc[] =
|
1995-01-07 11:50:36 +00:00
|
|
|
"Functions to create new objects used by the interpreter.\n\
|
|
|
|
\n\
|
|
|
|
You need to know a great deal about the interpreter to use this!";
|
|
|
|
|
1998-12-04 18:50:17 +00:00
|
|
|
DL_EXPORT(void)
|
2000-07-21 06:00:07 +00:00
|
|
|
initnew(void)
|
1994-05-23 12:37:57 +00:00
|
|
|
{
|
2001-08-24 06:29:12 +00:00
|
|
|
Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
|
|
|
|
PYTHON_API_VERSION);
|
1994-05-23 12:37:57 +00:00
|
|
|
}
|