annotate Modules/newmodule.c @ 16798:258dcafe2d0b legacy-trunk

Gustavo Niemeyer <niemeyer@conectiva.com>: Fixed recno support (keys are integers rather than strings). Work around DB bug that cause stdin to be closed by rnopen() when the DB file needed to exist but did not (no longer segfaults). This closes SF tracker patch #403445. Also wrapped some long lines and added whitespace around operators -- FLD.
author Fred Drake <fdrake@acm.org>
date Tue, 27 Feb 2001 18:56:46 +0000
parents 2ab28b213d70
children 2bf2690966ff
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
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
43 if (!PyArg_ParseTuple(args, "OOO!:instancemethod",
8651
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
44 &func,
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
45 &self,
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
46 &PyClass_Type, &classObj))
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
47 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
48 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
49 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
50 "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
51 return NULL;
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
52 }
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
53 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
54 self = NULL;
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
55 else if (!PyInstance_Check(self)) {
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
56 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
57 "second argument must be instance or None");
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
58 return NULL;
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8409
diff changeset
59 }
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
60 return PyMethod_New(func, self, classObj);
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 }
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
63 static char new_function_doc[] =
15150
486f3ff14d95 Fix the docstring for new.function().
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
64 "Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).";
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
65
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
66 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
67 new_function(PyObject* unused, PyObject* args)
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 {
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
69 PyObject* code;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
70 PyObject* globals;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
71 PyObject* name = Py_None;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
72 PyObject* defaults = Py_None;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
73 PyFunctionObject* newfunc;
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
15405
af1e9e64ed9d Allow new.function() called with explicit 3rd arg of None, as
Guido van Rossum <guido@python.org>
parents: 15150
diff changeset
75 if (!PyArg_ParseTuple(args, "O!O!|OO!:function",
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
76 &PyCode_Type, &code,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
77 &PyDict_Type, &globals,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
78 &name,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
79 &PyTuple_Type, &defaults))
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
80 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
81 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
82 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
83 "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
84 return NULL;
af1e9e64ed9d Allow new.function() called with explicit 3rd arg of None, as
Guido van Rossum <guido@python.org>
parents: 15150
diff changeset
85 }
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
87 newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
88 if (newfunc == NULL)
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
89 return NULL;
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
91 if (name != Py_None) {
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
92 Py_XINCREF(name);
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
93 Py_XDECREF(newfunc->func_name);
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
94 newfunc->func_name = name;
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
95 }
8409
f21867f8035b I think there was a tiny bug in new_function() -- the 'defaults'
Guido van Rossum <guido@python.org>
parents: 5044
diff changeset
96 if (defaults != Py_None) {
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
97 Py_XINCREF(defaults);
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
98 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
99 newfunc->func_defaults = defaults;
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
100 }
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
102 return (PyObject *)newfunc;
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
103 }
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
104
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
105 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
106 "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
107 "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
108 "CELLVARS).";
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
109
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
110 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
111 new_code(PyObject* unused, PyObject* args)
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 {
3135
999fcce496f8 re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents: 2871
diff changeset
113 int argcount;
999fcce496f8 re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents: 2871
diff changeset
114 int nlocals;
4997
58bbaf284ad6 Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents: 4661
diff changeset
115 int stacksize;
3135
999fcce496f8 re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents: 2871
diff changeset
116 int flags;
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
117 PyObject* code;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
118 PyObject* consts;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
119 PyObject* names;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
120 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
121 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
122 PyObject* cellvars = NULL;
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
123 PyObject* filename;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
124 PyObject* name;
5044
ee3ac9e0a032 Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents: 4997
diff changeset
125 int firstlineno;
ee3ac9e0a032 Two more arguments to newcodeobject -- first lineno and lineno table.
Guido van Rossum <guido@python.org>
parents: 4997
diff changeset
126 PyObject* lnotab;
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
127 PyBufferProcs *pb;
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
128
16391
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
129 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
130 &argcount, &nlocals, &stacksize, &flags,
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
131 &code,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
132 &PyTuple_Type, &consts,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
133 &PyTuple_Type, &names,
4997
58bbaf284ad6 Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents: 4661
diff changeset
134 &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
135 &filename, &name,
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
136 &firstlineno, &lnotab,
16259
9386b32bff1d PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15405
diff changeset
137 &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
138 &PyTuple_Type, &cellvars))
3135
999fcce496f8 re-enable new.code(...) with new args
Guido van Rossum <guido@python.org>
parents: 2871
diff changeset
139 return NULL;
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
140
16391
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
141 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
142 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
143 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
144 return NULL;
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
145 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
146 freevars = empty;
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
147 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
148 }
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
149 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
150 cellvars = empty;
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
151 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
152 }
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
153 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
154 }
2ab28b213d70 move extra arguments to the back of the new.code() arglist
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16315
diff changeset
155
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
156 pb = code->ob_type->tp_as_buffer;
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
157 if (pb == NULL ||
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
158 pb->bf_getreadbuffer == NULL ||
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
159 pb->bf_getsegcount == NULL ||
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
160 (*pb->bf_getsegcount)(code, NULL) != 1)
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
161 {
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
162 PyErr_SetString(PyExc_TypeError,
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
163 "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
164 return NULL;
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
165 }
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 8651
diff changeset
166
4997
58bbaf284ad6 Add stacksize argument to new.code().
Guido van Rossum <guido@python.org>
parents: 4661
diff changeset
167 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
168 code, consts, names, varnames,
16259
9386b32bff1d PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15405
diff changeset
169 freevars, cellvars, filename, name,
9386b32bff1d PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15405
diff changeset
170 firstlineno, lnotab);
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 }
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
172
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
173 static char new_module_doc[] =
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
174 "Create a module object from (NAME).";
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
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_module(PyObject* unused, PyObject* args)
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 {
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
179 char *name;
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
181 if (!PyArg_ParseTuple(args, "s:module", &name))
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
182 return NULL;
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
183 return PyModule_New(name);
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 }
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
186 static char new_class_doc[] =
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
187 "Create a class object from (NAME, BASE_CLASSES, DICT).";
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 PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
190 new_class(PyObject* unused, PyObject* args)
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
191 {
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
192 PyObject * name;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
193 PyObject * classes;
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
194 PyObject * dict;
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
195
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
196 if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes,
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
197 &PyDict_Type, &dict))
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
198 return NULL;
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
199 return PyClass_New(classes, dict, name);
3337
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
200 }
201de8455c2b added new.classobj()
Guido van Rossum <guido@python.org>
parents: 3135
diff changeset
201
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
202 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
203 {"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
204 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
205 {"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
206 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
207 {"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
208 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
209 {"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
210 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
211 {"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
212 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
213 {"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
214 METH_VARARGS, new_class_doc},
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
215 {NULL, NULL} /* sentinel */
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 };
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
2077
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
218 char new_doc[] =
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
219 "Functions to create new objects used by the interpreter.\n\
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
220 \n\
3848094f259c totally revamped
Guido van Rossum <guido@python.org>
parents: 2008
diff changeset
221 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
222
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
223 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13499
diff changeset
224 initnew(void)
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 {
4661
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
226 Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
d5023d0c5502 Renamed.
Barry Warsaw <barry@python.org>
parents: 4562
diff changeset
227 PYTHON_API_VERSION);
1650
51a06fdfff78 Added some new modules
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 }