blob: 52328a967d0c12b51476e4451c7ddf530e2fc032 [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf9888eb1995-01-07 11:50:36 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf9888eb1995-01-07 11:50:36 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf9888eb1995-01-07 11:50:36 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf9888eb1995-01-07 11:50:36 +000029
30******************************************************************/
31
32/* Module new -- create new objects of various types */
33
Barry Warsawa6b7c711996-12-10 16:25:56 +000034#include "Python.h"
Guido van Rossum34162a11994-05-23 12:37:57 +000035#include "compile.h"
Guido van Rossum34162a11994-05-23 12:37:57 +000036
Guido van Rossum26b310a1996-06-17 16:56:56 +000037static char new_instance_doc[] =
38"Create an instance object from (CLASS, DICT) without calling its __init__().";
39
Barry Warsawa6b7c711996-12-10 16:25:56 +000040static PyObject *
Guido van Rossum26b310a1996-06-17 16:56:56 +000041new_instance(unused, args)
Barry Warsawa6b7c711996-12-10 16:25:56 +000042 PyObject* unused;
43 PyObject* args;
Guido van Rossum26b310a1996-06-17 16:56:56 +000044{
Barry Warsawa6b7c711996-12-10 16:25:56 +000045 PyObject* klass;
46 PyObject *dict;
47 PyInstanceObject *inst;
48 if (!PyArg_ParseTuple(args, "O!O!",
49 &PyClass_Type, &klass,
50 &PyDict_Type, &dict))
Guido van Rossum26b310a1996-06-17 16:56:56 +000051 return NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +000052 inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type);
Guido van Rossum26b310a1996-06-17 16:56:56 +000053 if (inst == NULL)
54 return NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +000055 Py_INCREF(klass);
56 Py_INCREF(dict);
57 inst->in_class = (PyClassObject *)klass;
Guido van Rossum26b310a1996-06-17 16:56:56 +000058 inst->in_dict = dict;
Barry Warsawa6b7c711996-12-10 16:25:56 +000059 return (PyObject *)inst;
Guido van Rossum26b310a1996-06-17 16:56:56 +000060}
61
Guido van Rossumf9888eb1995-01-07 11:50:36 +000062static char new_im_doc[] =
63"Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
Guido van Rossum34162a11994-05-23 12:37:57 +000064
Barry Warsawa6b7c711996-12-10 16:25:56 +000065static PyObject *
Guido van Rossum34162a11994-05-23 12:37:57 +000066new_instancemethod(unused, args)
Barry Warsawa6b7c711996-12-10 16:25:56 +000067 PyObject* unused;
68 PyObject* args;
Guido van Rossum34162a11994-05-23 12:37:57 +000069{
Barry Warsawa6b7c711996-12-10 16:25:56 +000070 PyObject* func;
71 PyObject* self;
72 PyObject* classObj;
Guido van Rossum34162a11994-05-23 12:37:57 +000073
Guido van Rossum7859f871998-07-08 14:58:16 +000074 if (!PyArg_ParseTuple(args, "OOO!",
75 &func,
76 &self,
Barry Warsawa6b7c711996-12-10 16:25:56 +000077 &PyClass_Type, &classObj))
Guido van Rossumf9888eb1995-01-07 11:50:36 +000078 return NULL;
Guido van Rossum7859f871998-07-08 14:58:16 +000079 if (!PyCallable_Check(func)) {
80 PyErr_SetString(PyExc_TypeError,
81 "first argument must be callable");
82 return NULL;
83 }
84 if (self == Py_None)
85 self = NULL;
86 else if (!PyInstance_Check(self)) {
87 PyErr_SetString(PyExc_TypeError,
88 "second argument must be instance or None");
89 return NULL;
90 }
Barry Warsawa6b7c711996-12-10 16:25:56 +000091 return PyMethod_New(func, self, classObj);
Guido van Rossumf9888eb1995-01-07 11:50:36 +000092}
93
94static char new_function_doc[] =
Guido van Rossumb916faf1996-11-21 16:02:12 +000095"Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS]).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +000096
Barry Warsawa6b7c711996-12-10 16:25:56 +000097static PyObject *
Guido van Rossum34162a11994-05-23 12:37:57 +000098new_function(unused, args)
Barry Warsawa6b7c711996-12-10 16:25:56 +000099 PyObject* unused;
100 PyObject* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000101{
Barry Warsawa6b7c711996-12-10 16:25:56 +0000102 PyObject* code;
103 PyObject* globals;
104 PyObject* name = Py_None;
105 PyObject* defaults = Py_None;
106 PyFunctionObject* newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +0000107
Barry Warsawa6b7c711996-12-10 16:25:56 +0000108 if (!PyArg_ParseTuple(args, "O!O!|SO!",
109 &PyCode_Type, &code,
110 &PyDict_Type, &globals,
111 &name,
112 &PyTuple_Type, &defaults))
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000113 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +0000114
Barry Warsawa6b7c711996-12-10 16:25:56 +0000115 newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000116 if (newfunc == NULL)
117 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +0000118
Barry Warsawa6b7c711996-12-10 16:25:56 +0000119 if (name != Py_None) {
120 Py_XINCREF(name);
121 Py_XDECREF(newfunc->func_name);
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000122 newfunc->func_name = name;
123 }
Guido van Rossumb88679b1998-05-22 00:57:31 +0000124 if (defaults != Py_None) {
Barry Warsawa6b7c711996-12-10 16:25:56 +0000125 Py_XINCREF(defaults);
126 Py_XDECREF(newfunc->func_defaults);
Guido van Rossumb916faf1996-11-21 16:02:12 +0000127 newfunc->func_defaults = defaults;
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000128 }
Guido van Rossum34162a11994-05-23 12:37:57 +0000129
Barry Warsawa6b7c711996-12-10 16:25:56 +0000130 return (PyObject *)newfunc;
Guido van Rossum34162a11994-05-23 12:37:57 +0000131}
Guido van Rossum34162a11994-05-23 12:37:57 +0000132
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000133static char new_code_doc[] =
Guido van Rossum463e55a1997-01-24 03:58:52 +0000134"Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB).";
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000135
Barry Warsawa6b7c711996-12-10 16:25:56 +0000136static PyObject *
Guido van Rossum34162a11994-05-23 12:37:57 +0000137new_code(unused, args)
Barry Warsawa6b7c711996-12-10 16:25:56 +0000138 PyObject* unused;
139 PyObject* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000140{
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000141 int argcount;
142 int nlocals;
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000143 int stacksize;
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000144 int flags;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000145 PyObject* code;
146 PyObject* consts;
147 PyObject* names;
148 PyObject* varnames;
149 PyObject* filename;
150 PyObject* name;
Guido van Rossum463e55a1997-01-24 03:58:52 +0000151 int firstlineno;
152 PyObject* lnotab;
Guido van Rossum34162a11994-05-23 12:37:57 +0000153
Guido van Rossum463e55a1997-01-24 03:58:52 +0000154 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS",
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000155 &argcount, &nlocals, &stacksize, &flags,
Barry Warsawa6b7c711996-12-10 16:25:56 +0000156 &code,
157 &PyTuple_Type, &consts,
158 &PyTuple_Type, &names,
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000159 &PyTuple_Type, &varnames,
Guido van Rossum463e55a1997-01-24 03:58:52 +0000160 &filename, &name,
161 &firstlineno, &lnotab))
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000162 return NULL;
Guido van Rossume9fd28d1997-01-17 21:12:06 +0000163 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
Barry Warsawa6b7c711996-12-10 16:25:56 +0000164 code, consts, names, varnames,
Guido van Rossum463e55a1997-01-24 03:58:52 +0000165 filename, name, firstlineno, lnotab);
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000166}
Guido van Rossum34162a11994-05-23 12:37:57 +0000167
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000168static char new_module_doc[] =
169"Create a module object from (NAME).";
170
Barry Warsawa6b7c711996-12-10 16:25:56 +0000171static PyObject *
Guido van Rossum34162a11994-05-23 12:37:57 +0000172new_module(unused, args)
Barry Warsawa6b7c711996-12-10 16:25:56 +0000173 PyObject* unused;
174 PyObject* args;
Guido van Rossum34162a11994-05-23 12:37:57 +0000175{
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000176 char *name;
Guido van Rossum34162a11994-05-23 12:37:57 +0000177
Barry Warsawa6b7c711996-12-10 16:25:56 +0000178 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000179 return NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000180 return PyModule_New(name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000181}
Guido van Rossum34162a11994-05-23 12:37:57 +0000182
Guido van Rossumb28911c1996-01-12 01:38:22 +0000183static char new_class_doc[] =
184"Create a class object from (NAME, BASE_CLASSES, DICT).";
185
Barry Warsawa6b7c711996-12-10 16:25:56 +0000186static PyObject *
Guido van Rossumb28911c1996-01-12 01:38:22 +0000187new_class(unused, args)
Barry Warsawa6b7c711996-12-10 16:25:56 +0000188 PyObject* unused;
189 PyObject* args;
Guido van Rossumb28911c1996-01-12 01:38:22 +0000190{
Barry Warsawa6b7c711996-12-10 16:25:56 +0000191 PyObject * name;
192 PyObject * classes;
193 PyObject * dict;
Guido van Rossumb28911c1996-01-12 01:38:22 +0000194
Barry Warsawa6b7c711996-12-10 16:25:56 +0000195 if (!PyArg_ParseTuple(args, "SO!O!", &name, &PyTuple_Type, &classes,
196 &PyDict_Type, &dict))
Guido van Rossumb28911c1996-01-12 01:38:22 +0000197 return NULL;
Barry Warsawa6b7c711996-12-10 16:25:56 +0000198 return PyClass_New(classes, dict, name);
Guido van Rossumb28911c1996-01-12 01:38:22 +0000199}
200
Barry Warsawa6b7c711996-12-10 16:25:56 +0000201static PyMethodDef new_methods[] = {
Guido van Rossum26b310a1996-06-17 16:56:56 +0000202 {"instance", new_instance, 1, new_instance_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000203 {"instancemethod", new_instancemethod, 1, new_im_doc},
204 {"function", new_function, 1, new_function_doc},
Guido van Rossum896fc7e1995-09-30 17:01:02 +0000205 {"code", new_code, 1, new_code_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000206 {"module", new_module, 1, new_module_doc},
Guido van Rossumb28911c1996-01-12 01:38:22 +0000207 {"classobj", new_class, 1, new_class_doc},
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000208 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000209};
210
Guido van Rossumf9888eb1995-01-07 11:50:36 +0000211char new_doc[] =
212"Functions to create new objects used by the interpreter.\n\
213\n\
214You need to know a great deal about the interpreter to use this!";
215
Guido van Rossum34162a11994-05-23 12:37:57 +0000216void
217initnew()
218{
Barry Warsawa6b7c711996-12-10 16:25:56 +0000219 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
220 PYTHON_API_VERSION);
Guido van Rossum34162a11994-05-23 12:37:57 +0000221}