2 /* Module new -- create new objects of various types */
7 static char new_instance_doc
[] =
8 "Create an instance object from (CLASS, DICT) without calling its __init__().";
11 new_instance(PyObject
* unused
, PyObject
* args
)
15 PyInstanceObject
*inst
;
16 if (!PyArg_ParseTuple(args
, "O!O!:instance",
17 &PyClass_Type
, &klass
,
20 inst
= PyObject_New(PyInstanceObject
, &PyInstance_Type
);
25 inst
->in_class
= (PyClassObject
*)klass
;
27 PyObject_GC_Init(inst
);
28 return (PyObject
*)inst
;
31 static char new_im_doc
[] =
32 "Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
35 new_instancemethod(PyObject
* unused
, PyObject
* args
)
41 if (!PyArg_ParseTuple(args
, "OOO!:instancemethod",
44 &PyClass_Type
, &classObj
))
46 if (!PyCallable_Check(func
)) {
47 PyErr_SetString(PyExc_TypeError
,
48 "first argument must be callable");
53 else if (!PyInstance_Check(self
)) {
54 PyErr_SetString(PyExc_TypeError
,
55 "second argument must be instance or None");
58 return PyMethod_New(func
, self
, classObj
);
61 static char new_function_doc
[] =
62 "Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).";
65 new_function(PyObject
* unused
, PyObject
* args
)
69 PyObject
* name
= Py_None
;
70 PyObject
* defaults
= Py_None
;
71 PyFunctionObject
* newfunc
;
73 if (!PyArg_ParseTuple(args
, "O!O!|OO!:function",
75 &PyDict_Type
, &globals
,
77 &PyTuple_Type
, &defaults
))
79 if (name
!= Py_None
&& !PyString_Check(name
)) {
80 PyErr_SetString(PyExc_TypeError
,
81 "arg 3 (name) must be None or string");
85 newfunc
= (PyFunctionObject
*)PyFunction_New(code
, globals
);
89 if (name
!= Py_None
) {
91 Py_XDECREF(newfunc
->func_name
);
92 newfunc
->func_name
= name
;
94 if (defaults
!= Py_None
) {
96 Py_XDECREF(newfunc
->func_defaults
);
97 newfunc
->func_defaults
= defaults
;
100 return (PyObject
*)newfunc
;
103 static char new_code_doc
[] =
104 "Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB).";
107 new_code(PyObject
* unused
, PyObject
* args
)
123 if (!PyArg_ParseTuple(args
, "iiiiOO!O!O!SSiS:code",
124 &argcount
, &nlocals
, &stacksize
, &flags
,
126 &PyTuple_Type
, &consts
,
127 &PyTuple_Type
, &names
,
128 &PyTuple_Type
, &varnames
,
130 &firstlineno
, &lnotab
))
133 pb
= code
->ob_type
->tp_as_buffer
;
135 pb
->bf_getreadbuffer
== NULL
||
136 pb
->bf_getsegcount
== NULL
||
137 (*pb
->bf_getsegcount
)(code
, NULL
) != 1)
139 PyErr_SetString(PyExc_TypeError
,
140 "bytecode object must be a single-segment read-only buffer");
144 return (PyObject
*)PyCode_New(argcount
, nlocals
, stacksize
, flags
,
145 code
, consts
, names
, varnames
,
146 filename
, name
, firstlineno
, lnotab
);
149 static char new_module_doc
[] =
150 "Create a module object from (NAME).";
153 new_module(PyObject
* unused
, PyObject
* args
)
157 if (!PyArg_ParseTuple(args
, "s:module", &name
))
159 return PyModule_New(name
);
162 static char new_class_doc
[] =
163 "Create a class object from (NAME, BASE_CLASSES, DICT).";
166 new_class(PyObject
* unused
, PyObject
* args
)
172 if (!PyArg_ParseTuple(args
, "SO!O!:class", &name
, &PyTuple_Type
, &classes
,
173 &PyDict_Type
, &dict
))
175 return PyClass_New(classes
, dict
, name
);
178 static PyMethodDef new_methods
[] = {
179 {"instance", new_instance
,
180 METH_VARARGS
, new_instance_doc
},
181 {"instancemethod", new_instancemethod
,
182 METH_VARARGS
, new_im_doc
},
183 {"function", new_function
,
184 METH_VARARGS
, new_function_doc
},
186 METH_VARARGS
, new_code_doc
},
187 {"module", new_module
,
188 METH_VARARGS
, new_module_doc
},
189 {"classobj", new_class
,
190 METH_VARARGS
, new_class_doc
},
191 {NULL
, NULL
} /* sentinel */
195 "Functions to create new objects used by the interpreter.\n\
197 You need to know a great deal about the interpreter to use this!";
202 Py_InitModule4("new", new_methods
, new_doc
, (PyObject
*)NULL
,