1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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
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 */
37 static char new_instance_doc
[] =
38 "Create an instance object from (CLASS, DICT) without calling its __init__().";
41 new_instance(unused
, args
)
47 PyInstanceObject
*inst
;
48 if (!PyArg_ParseTuple(args
, "O!O!",
49 &PyClass_Type
, &klass
,
52 inst
= PyObject_NEW(PyInstanceObject
, &PyInstance_Type
);
57 inst
->in_class
= (PyClassObject
*)klass
;
59 return (PyObject
*)inst
;
62 static char new_im_doc
[] =
63 "Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
66 new_instancemethod(unused
, args
)
74 if (!PyArg_ParseTuple(args
, "OOO!",
77 &PyClass_Type
, &classObj
))
79 if (!PyCallable_Check(func
)) {
80 PyErr_SetString(PyExc_TypeError
,
81 "first argument must be callable");
86 else if (!PyInstance_Check(self
)) {
87 PyErr_SetString(PyExc_TypeError
,
88 "second argument must be instance or None");
91 return PyMethod_New(func
, self
, classObj
);
94 static char new_function_doc
[] =
95 "Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS]).";
98 new_function(unused
, args
)
104 PyObject
* name
= Py_None
;
105 PyObject
* defaults
= Py_None
;
106 PyFunctionObject
* newfunc
;
108 if (!PyArg_ParseTuple(args
, "O!O!|SO!",
110 &PyDict_Type
, &globals
,
112 &PyTuple_Type
, &defaults
))
115 newfunc
= (PyFunctionObject
*)PyFunction_New(code
, globals
);
119 if (name
!= Py_None
) {
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).";
137 new_code(unused
, args
)
155 if (!PyArg_ParseTuple(args
, "iiiiOO!O!O!SSiS",
156 &argcount
, &nlocals
, &stacksize
, &flags
,
158 &PyTuple_Type
, &consts
,
159 &PyTuple_Type
, &names
,
160 &PyTuple_Type
, &varnames
,
162 &firstlineno
, &lnotab
))
165 pb
= code
->ob_type
->tp_as_buffer
;
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");
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).";
185 new_module(unused
, args
)
191 if (!PyArg_ParseTuple(args
, "s", &name
))
193 return PyModule_New(name
);
196 static char new_class_doc
[] =
197 "Create a class object from (NAME, BASE_CLASSES, DICT).";
200 new_class(unused
, args
)
208 if (!PyArg_ParseTuple(args
, "SO!O!", &name
, &PyTuple_Type
, &classes
,
209 &PyDict_Type
, &dict
))
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 */
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!";
232 Py_InitModule4("new", new_methods
, new_doc
, (PyObject
*)NULL
,