Allow comment characters (#) to be escaped:
[python/dscho.git] / Modules / newmodule.c
blob786ddc5f5996f8ed444f4a0806809d72f8007c05
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Module new -- create new objects of various types */
34 #include "Python.h"
35 #include "compile.h"
37 static char new_instance_doc[] =
38 "Create an instance object from (CLASS, DICT) without calling its __init__().";
40 static PyObject *
41 new_instance(unused, args)
42 PyObject* unused;
43 PyObject* args;
45 PyObject* klass;
46 PyObject *dict;
47 PyInstanceObject *inst;
48 if (!PyArg_ParseTuple(args, "O!O!",
49 &PyClass_Type, &klass,
50 &PyDict_Type, &dict))
51 return NULL;
52 inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type);
53 if (inst == NULL)
54 return NULL;
55 Py_INCREF(klass);
56 Py_INCREF(dict);
57 inst->in_class = (PyClassObject *)klass;
58 inst->in_dict = dict;
59 return (PyObject *)inst;
62 static char new_im_doc[] =
63 "Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
65 static PyObject *
66 new_instancemethod(unused, args)
67 PyObject* unused;
68 PyObject* args;
70 PyObject* func;
71 PyObject* self;
72 PyObject* classObj;
74 if (!PyArg_ParseTuple(args, "OOO!",
75 &func,
76 &self,
77 &PyClass_Type, &classObj))
78 return NULL;
79 if (!PyCallable_Check(func)) {
80 PyErr_SetString(PyExc_TypeError,
81 "first argument must be callable");
82 return NULL;
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;
91 return PyMethod_New(func, self, classObj);
94 static char new_function_doc[] =
95 "Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS]).";
97 static PyObject *
98 new_function(unused, args)
99 PyObject* unused;
100 PyObject* args;
102 PyObject* code;
103 PyObject* globals;
104 PyObject* name = Py_None;
105 PyObject* defaults = Py_None;
106 PyFunctionObject* newfunc;
108 if (!PyArg_ParseTuple(args, "O!O!|SO!",
109 &PyCode_Type, &code,
110 &PyDict_Type, &globals,
111 &name,
112 &PyTuple_Type, &defaults))
113 return NULL;
115 newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
116 if (newfunc == NULL)
117 return NULL;
119 if (name != Py_None) {
120 Py_XINCREF(name);
121 Py_XDECREF(newfunc->func_name);
122 newfunc->func_name = name;
124 if (defaults != Py_None) {
125 Py_XINCREF(defaults);
126 Py_XDECREF(newfunc->func_defaults);
127 newfunc->func_defaults = defaults;
130 return (PyObject *)newfunc;
133 static char new_code_doc[] =
134 "Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB).";
136 static PyObject *
137 new_code(unused, args)
138 PyObject* unused;
139 PyObject* args;
141 int argcount;
142 int nlocals;
143 int stacksize;
144 int flags;
145 PyObject* code;
146 PyObject* consts;
147 PyObject* names;
148 PyObject* varnames;
149 PyObject* filename;
150 PyObject* name;
151 int firstlineno;
152 PyObject* lnotab;
153 PyBufferProcs *pb;
155 if (!PyArg_ParseTuple(args, "iiiiOO!O!O!SSiS",
156 &argcount, &nlocals, &stacksize, &flags,
157 &code,
158 &PyTuple_Type, &consts,
159 &PyTuple_Type, &names,
160 &PyTuple_Type, &varnames,
161 &filename, &name,
162 &firstlineno, &lnotab))
163 return NULL;
165 pb = code->ob_type->tp_as_buffer;
166 if (pb == NULL ||
167 pb->bf_getreadbuffer == NULL ||
168 pb->bf_getsegcount == NULL ||
169 (*pb->bf_getsegcount)(code, NULL) != 1)
171 PyErr_SetString(PyExc_TypeError,
172 "bytecode object must be a single-segment read-only buffer");
173 return NULL;
176 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
177 code, consts, names, varnames,
178 filename, name, firstlineno, lnotab);
181 static char new_module_doc[] =
182 "Create a module object from (NAME).";
184 static PyObject *
185 new_module(unused, args)
186 PyObject* unused;
187 PyObject* args;
189 char *name;
191 if (!PyArg_ParseTuple(args, "s", &name))
192 return NULL;
193 return PyModule_New(name);
196 static char new_class_doc[] =
197 "Create a class object from (NAME, BASE_CLASSES, DICT).";
199 static PyObject *
200 new_class(unused, args)
201 PyObject* unused;
202 PyObject* args;
204 PyObject * name;
205 PyObject * classes;
206 PyObject * dict;
208 if (!PyArg_ParseTuple(args, "SO!O!", &name, &PyTuple_Type, &classes,
209 &PyDict_Type, &dict))
210 return NULL;
211 return PyClass_New(classes, dict, name);
214 static PyMethodDef new_methods[] = {
215 {"instance", new_instance, 1, new_instance_doc},
216 {"instancemethod", new_instancemethod, 1, new_im_doc},
217 {"function", new_function, 1, new_function_doc},
218 {"code", new_code, 1, new_code_doc},
219 {"module", new_module, 1, new_module_doc},
220 {"classobj", new_class, 1, new_class_doc},
221 {NULL, NULL} /* sentinel */
224 char new_doc[] =
225 "Functions to create new objects used by the interpreter.\n\
227 You need to know a great deal about the interpreter to use this!";
229 DL_EXPORT(void)
230 initnew()
232 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
233 PYTHON_API_VERSION);