Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fe0a82

Browse files
committed
move extra arguments to the back of the new.code() arglist
1 parent dabed75 commit 6fe0a82

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

Lib/test/test_new.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ def break_yolks(self):
6666

6767
# bogus test of new.code()
6868
print 'new.code()'
69-
d = new.code(3, 3, 3, 3, codestr, (), (), (), (), (),
69+
d = new.code(3, 3, 3, 3, codestr, (), (), (),
70+
"<string>", "<name>", 1, "", (), ())
71+
# test backwards-compatibility version with no freevars or cellvars
72+
d = new.code(3, 3, 3, 3, codestr, (), (), (),
7073
"<string>", "<name>", 1, "")
7174
if verbose:
7275
print d

Modules/newmodule.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ new_function(PyObject* unused, PyObject* args)
103103
}
104104

105105
static char new_code_doc[] =
106-
"Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FREEVARS, CELLVARS, FILENAME, NAME, FIRSTLINENO, LNOTAB).";
106+
"Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING,\n"
107+
"CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB, FREEVARS,\n"
108+
"CELLVARS).";
107109

108110
static PyObject *
109111
new_code(PyObject* unused, PyObject* args)
@@ -116,26 +118,41 @@ new_code(PyObject* unused, PyObject* args)
116118
PyObject* consts;
117119
PyObject* names;
118120
PyObject* varnames;
119-
PyObject* freevars;
120-
PyObject* cellvars;
121+
PyObject* freevars = NULL;
122+
PyObject* cellvars = NULL;
121123
PyObject* filename;
122124
PyObject* name;
123125
int firstlineno;
124126
PyObject* lnotab;
125127
PyBufferProcs *pb;
126128

127-
if (!PyArg_ParseTuple(args, "iiiiOO!O!O!O!O!SSiS:code",
129+
if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
128130
&argcount, &nlocals, &stacksize, &flags,
129131
&code,
130132
&PyTuple_Type, &consts,
131133
&PyTuple_Type, &names,
132134
&PyTuple_Type, &varnames,
133-
&PyTuple_Type, &freevars,
134-
&PyTuple_Type, &cellvars,
135135
&filename, &name,
136-
&firstlineno, &lnotab))
136+
&firstlineno, &lnotab,
137+
&PyTuple_Type, &freevars,
138+
&PyTuple_Type, &cellvars))
137139
return NULL;
138140

141+
if (freevars == NULL || cellvars == NULL) {
142+
PyObject *empty = PyTuple_New(0);
143+
if (empty == NULL)
144+
return NULL;
145+
if (freevars == NULL) {
146+
freevars = empty;
147+
Py_INCREF(freevars);
148+
}
149+
if (cellvars == NULL) {
150+
cellvars = empty;
151+
Py_INCREF(cellvars);
152+
}
153+
Py_DECREF(empty);
154+
}
155+
139156
pb = code->ob_type->tp_as_buffer;
140157
if (pb == NULL ||
141158
pb->bf_getreadbuffer == NULL ||

0 commit comments

Comments
 (0)