annotate Modules/newmodule.c @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 14da7dd37c94
children ed1ba37a46b7 856c81f71213
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
1
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
2 /* Module new -- create new objects of various types */
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
3
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
4 #include "Python.h"
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #include "compile.h"
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
3678
bddbaa51aa5a Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents: 3337
diff changeset
7 static char new_instance_doc[] =
16315
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
8 "Create an instance object from (CLASS [, DICT]) without calling its\n\
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
9 __init__() method. DICT must be a dictionary or None.";
3678
bddbaa51aa5a Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents: 3337
diff changeset
10
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
11 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
12 new_instance(PyObject* unused, PyObject* args)
3678
bddbaa51aa5a Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents: 3337
diff changeset
13 {
16315
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
14 PyObject *klass;
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
15 PyObject *dict = NULL;
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
16
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
17 if (!PyArg_ParseTuple(args, "O!|O:instance",
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
18 &PyClass_Type, &klass, &dict))
3678
bddbaa51aa5a Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents: 3337
diff changeset
19 return NULL;
16315
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
20
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
21 if (dict == Py_None)
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
22 dict = NULL;
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
23 else if (dict == NULL)
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
24 /* do nothing */;
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
25 else if (!PyDict_Check(dict)) {
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
26 PyErr_SetString(PyExc_TypeError,
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
27 "new.instance() second arg must be dictionary or None");
3678
bddbaa51aa5a Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents: 3337
diff changeset
28 return NULL;
16315
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
29 }
28c60ef48fa3 new_instance(): Use PyInstance_NewRaw() instead of knowing too much
Fred Drake <fdrake@acm.org>
parents: 16259
diff changeset
30 return PyInstance_NewRaw(klass, dict);
3678
bddbaa51aa5a Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents: 3337
diff changeset
31 }
bddbaa51aa5a Added new.instance(class, dict).
Guido van Rossum <guido@python.org>
parents: 3337
diff changeset
32
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
33 static char new_im_doc[] =
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
34 "Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
36 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
37 new_instancemethod(PyObject* unused, PyObject* args)
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 {
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
39 PyObject* func;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
40 PyObject* self;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
41 PyObject* classObj;
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
42
21822
14da7dd37c94 It's merge time!
Michael W. Hudson <mwh@python.net>
parents: 20963
diff changeset
43 if (!PyArg_ParseTuple(args, "OOO:instancemethod",
14da7dd37c94 It's merge time!
Michael W. Hudson <mwh@python.net>
parents: 20963
diff changeset
44 &func, &self, &classObj))
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
45 return NULL;
8651
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
46 if (!PyCallable_Check(func)) {
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
47 PyErr_SetString(PyExc_TypeError,
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
48 "first argument must be callable");
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
49 return NULL;
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
50 }
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
51 if (self == Py_None)
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
52 self = NULL;
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
53 return PyMethod_New(func, self, classObj);
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 }
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
56 static char new_function_doc[] =
15150
486f3ff14d95 Fix the docstring for new.function().
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
57 "Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).";
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
58
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
59 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
60 new_function(PyObject* unused, PyObject* args)
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 {
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
62 PyObject* code;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
63 PyObject* globals;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
64 PyObject* name = Py_None;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
65 PyObject* defaults = Py_None;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
66 PyFunctionObject* newfunc;
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
15405
af1e9e64ed9d Allow new.function() called with explicit 3rd arg of None, as
Guido van Rossum <guido@python.org>
parents: 15150
diff changeset
68 if (!PyArg_ParseTuple(args, "O!O!|OO!:function",
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
69 &PyCode_Type, &code,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
70 &PyDict_Type, &globals,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
71 &name,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
72 &PyTuple_Type, &defaults))
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
73 return NULL;
15405
af1e9e64ed9d Allow new.function() called with explicit 3rd arg of None, as
Guido van Rossum <guido@python.org>
parents: 15150
diff changeset
74 if (name != Py_None && !PyString_Check(name)) {
af1e9e64ed9d Allow new.function() called with explicit 3rd arg of None, as
Guido van Rossum <guido@python.org>
parents: 15150
diff changeset
75 PyErr_SetString(PyExc_TypeError,
af1e9e64ed9d Allow new.function() called with explicit 3rd arg of None, as
Guido van Rossum <guido@python.org>
parents: 15150
diff changeset
76 "arg 3 (name) must be None or string");
af1e9e64ed9d Allow new.function() called with explicit 3rd arg of None, as
Guido van Rossum <guido@python.org>
parents: 15150
diff changeset
77 return NULL;
af1e9e64ed9d Allow new.function() called with explicit 3rd arg of None, as
Guido van Rossum <guido@python.org>
parents: 15150
diff changeset
78 }
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
80 newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
81 if (newfunc == NULL)
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
82 return NULL;
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
84 if (name != Py_None) {
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
85 Py_XINCREF(name);
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
86 Py_XDECREF(newfunc->func_name);
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
87 newfunc->func_name = name;
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
88 }
8409
f21867f8035b I think there was a tiny bug in new_function() -- the 'defaults'
Guido van Rossum <guido@python.org>
parents: 5044
diff changeset
89 if (defaults != Py_None) {
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
90 Py_XINCREF(defaults);
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
91 Py_XDECREF(newfunc->func_defaults);
4562
d030ea533e63 Upgraded new.function() contributed by Tommy. Also got rid of #if 0'ed code.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
92 newfunc->func_defaults = defaults;
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
93 }
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
95 return (PyObject *)newfunc;
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
96 }
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
97
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
98 static char new_code_doc[] =
16391
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
99 "Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING,\n"
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
100 "CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB, FREEVARS,\n"
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
101 "CELLVARS).";
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
102
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
103 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
104 new_code(PyObject* unused, PyObject* args)
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 {
3135
999fcce496f8 re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents: 2871
diff changeset
106 int argcount;
999fcce496f8 re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents: 2871
diff changeset
107 int nlocals;
4997
58bbaf284ad6 Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents: 4661
diff changeset
108 int stacksize;
3135
999fcce496f8 re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents: 2871
diff changeset
109 int flags;
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
110 PyObject* code;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
111 PyObject* consts;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
112 PyObject* names;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
113 PyObject* varnames;
16391
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
114 PyObject* freevars = NULL;
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
115 PyObject* cellvars = NULL;
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
116 PyObject* filename;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
117 PyObject* name;
5044
ee3ac9e0a032 Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents: 4997
diff changeset
118 int firstlineno;
ee3ac9e0a032 Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents: 4997
diff changeset
119 PyObject* lnotab;
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
120
16391
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
121 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
4997
58bbaf284ad6 Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents: 4661
diff changeset
122 &argcount, &nlocals, &stacksize, &flags,
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
123 &code,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
124 &PyTuple_Type, &consts,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
125 &PyTuple_Type, &names,
4997
58bbaf284ad6 Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents: 4661
diff changeset
126 &PyTuple_Type, &varnames,
16391
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
127 &filename, &name,
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
128 &firstlineno, &lnotab,
16259
9386b32bff1d PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15405
diff changeset
129 &PyTuple_Type, &freevars,
16391
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
130 &PyTuple_Type, &cellvars))
3135
999fcce496f8 re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents: 2871
diff changeset
131 return NULL;
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
132
16391
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
133 if (freevars == NULL || cellvars == NULL) {
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
134 PyObject *empty = PyTuple_New(0);
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
135 if (empty == NULL)
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
136 return NULL;
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
137 if (freevars == NULL) {
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
138 freevars = empty;
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
139 Py_INCREF(freevars);
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
140 }
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
141 if (cellvars == NULL) {
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
142 cellvars = empty;
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
143 Py_INCREF(cellvars);
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
144 }
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
145 Py_DECREF(empty);
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
146 }
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
147
20947
c555f1e7c4be Use PyObject_CheckReadBuffer().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20594
diff changeset
148 if (!PyObject_CheckReadBuffer(code)) {
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
149 PyErr_SetString(PyExc_TypeError,
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
150 "bytecode object must be a single-segment read-only buffer");
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
151 return NULL;
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
152 }
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
153
4997
58bbaf284ad6 Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents: 4661
diff changeset
154 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
155 code, consts, names, varnames,
16259
9386b32bff1d PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15405
diff changeset
156 freevars, cellvars, filename, name,
9386b32bff1d PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15405
diff changeset
157 firstlineno, lnotab);
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 }
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
159
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
160 static char new_module_doc[] =
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
161 "Create a module object from (NAME).";
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
163 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
164 new_module(PyObject* unused, PyObject* args)
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 {
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
166 char *name;
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
168 if (!PyArg_ParseTuple(args, "s:module", &name))
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
169 return NULL;
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
170 return PyModule_New(name);
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 }
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
173 static char new_class_doc[] =
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
174 "Create a class object from (NAME, BASE_CLASSES, DICT).";
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
175
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
176 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
177 new_class(PyObject* unused, PyObject* args)
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
178 {
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
179 PyObject * name;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
180 PyObject * classes;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
181 PyObject * dict;
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
182
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
183 if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes,
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
184 &PyDict_Type, &dict))
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
185 return NULL;
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
186 return PyClass_New(classes, dict, name);
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
187 }
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
188
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
189 static PyMethodDef new_methods[] = {
13817
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
190 {"instance", new_instance,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
191 METH_VARARGS, new_instance_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
192 {"instancemethod", new_instancemethod,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
193 METH_VARARGS, new_im_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
194 {"function", new_function,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
195 METH_VARARGS, new_function_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
196 {"code", new_code,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
197 METH_VARARGS, new_code_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
198 {"module", new_module,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
199 METH_VARARGS, new_module_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
200 {"classobj", new_class,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
201 METH_VARARGS, new_class_doc},
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
202 {NULL, NULL} /* sentinel */
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 };
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
20594
0d5e6fc0481d Adding missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19461
diff changeset
205 static char new_doc[] =
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
206 "Functions to create new objects used by the interpreter.\n\
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
207 \n\
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
208 You need to know a great deal about the interpreter to use this!";
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
209
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
210 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13499
diff changeset
211 initnew(void)
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 {
19461
f7aaf721a81b Back out trying to use the C values for CO_xxx.
Tim Peters <tim.peters@gmail.com>
parents: 19371
diff changeset
213 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
f7aaf721a81b Back out trying to use the C values for CO_xxx.
Tim Peters <tim.peters@gmail.com>
parents: 19371
diff changeset
214 PYTHON_API_VERSION);
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 }