Simplify and speedup uses of Py_BuildValue():

* Py_BuildValue("(OOO)",a,b,c)  -->  PyTuple_Pack(3,a,b,c)
* Py_BuildValue("()",a)         -->  PyTuple_New(0)
* Py_BuildValue("O", a)         -->  Py_INCREF(a)
This commit is contained in:
Raymond Hettinger 2003-10-12 19:09:37 +00:00
parent cb2da43db8
commit 8ae4689657
25 changed files with 71 additions and 75 deletions

View file

@ -750,7 +750,7 @@ instance_getattr(register PyInstanceObject *inst, PyObject *name)
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
return NULL;
PyErr_Clear();
args = Py_BuildValue("(OO)", inst, name);
args = PyTuple_Pack(2, inst, name);
if (args == NULL)
return NULL;
res = PyEval_CallObject(func, args);
@ -847,9 +847,9 @@ instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v)
if (func == NULL)
return instance_setattr1(inst, name, v);
if (v == NULL)
args = Py_BuildValue("(OO)", inst, name);
args = PyTuple_Pack(2, inst, name);
else
args = Py_BuildValue("(OOO)", inst, name, v);
args = PyTuple_Pack(3, inst, name, v);
if (args == NULL)
return -1;
res = PyEval_CallObject(func, args);
@ -1038,7 +1038,7 @@ instance_subscript(PyInstanceObject *inst, PyObject *key)
func = instance_getattr(inst, getitemstr);
if (func == NULL)
return NULL;
arg = Py_BuildValue("(O)", key);
arg = PyTuple_Pack(1, key);
if (arg == NULL) {
Py_DECREF(func);
return NULL;
@ -1069,9 +1069,9 @@ instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value)
if (func == NULL)
return -1;
if (value == NULL)
arg = Py_BuildValue("(O)", key);
arg = PyTuple_Pack(1, key);
else
arg = Py_BuildValue("(OO)", key, value);
arg = PyTuple_Pack(2, key, value);
if (arg == NULL) {
Py_DECREF(func);
return -1;
@ -1281,7 +1281,7 @@ instance_contains(PyInstanceObject *inst, PyObject *member)
if (func) {
PyObject *res;
int ret;
PyObject *arg = Py_BuildValue("(O)", member);
PyObject *arg = PyTuple_Pack(1, member);
if(arg == NULL) {
Py_DECREF(func);
return -1;
@ -1346,7 +1346,7 @@ generic_binary_op(PyObject *v, PyObject *w, char *opname)
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
args = Py_BuildValue("(O)", w);
args = PyTuple_Pack(1, w);
if (args == NULL) {
Py_DECREF(func);
return NULL;
@ -1389,7 +1389,7 @@ half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc,
return generic_binary_op(v, w, opname);
}
args = Py_BuildValue("(O)", w);
args = PyTuple_Pack(1, w);
if (args == NULL) {
Py_DECREF(coercefunc);
return NULL;
@ -1474,7 +1474,7 @@ instance_coerce(PyObject **pv, PyObject **pw)
return 1;
}
/* Has __coerce__ method: call it */
args = Py_BuildValue("(O)", w);
args = PyTuple_Pack(1, w);
if (args == NULL) {
return -1;
}
@ -1587,7 +1587,7 @@ half_cmp(PyObject *v, PyObject *w)
return 2;
}
args = Py_BuildValue("(O)", w);
args = PyTuple_Pack(1, w);
if (args == NULL) {
Py_DECREF(cmp_func);
return -2;
@ -1747,7 +1747,7 @@ instance_pow(PyObject *v, PyObject *w, PyObject *z)
func = PyObject_GetAttrString(v, "__pow__");
if (func == NULL)
return NULL;
args = Py_BuildValue("(OO)", w, z);
args = PyTuple_Pack(2, w, z);
if (args == NULL) {
Py_DECREF(func);
return NULL;
@ -1786,7 +1786,7 @@ instance_ipow(PyObject *v, PyObject *w, PyObject *z)
PyErr_Clear();
return instance_pow(v, w, z);
}
args = Py_BuildValue("(OO)", w, z);
args = PyTuple_Pack(2, w, z);
if (args == NULL) {
Py_DECREF(func);
return NULL;
@ -1859,7 +1859,7 @@ half_richcompare(PyObject *v, PyObject *w, int op)
return res;
}
args = Py_BuildValue("(O)", w);
args = PyTuple_Pack(1, w);
if (args == NULL) {
Py_DECREF(method);
return NULL;

View file

@ -439,7 +439,7 @@ complex_divmod(PyComplexObject *v, PyComplexObject *w)
mod = c_diff(v->cval, c_prod(w->cval, div));
d = PyComplex_FromCComplex(div);
m = PyComplex_FromCComplex(mod);
z = Py_BuildValue("(OO)", d, m);
z = PyTuple_Pack(2, d, m);
Py_XDECREF(d);
Py_XDECREF(m);
return z;
@ -865,7 +865,7 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (f == NULL)
PyErr_Clear();
else {
PyObject *args = Py_BuildValue("()");
PyObject *args = PyTuple_New(0);
if (args == NULL)
return NULL;
r = PyEval_CallObject(f, args);

View file

@ -1227,7 +1227,7 @@ PyFile_GetLine(PyObject *f, int n)
if (reader == NULL)
return NULL;
if (n <= 0)
args = Py_BuildValue("()");
args = PyTuple_New(0);
else
args = Py_BuildValue("(i)", n);
if (args == NULL) {
@ -2104,7 +2104,7 @@ PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Py_DECREF(writer);
return -1;
}
args = Py_BuildValue("(O)", value);
args = PyTuple_Pack(1, value);
if (args == NULL) {
Py_DECREF(value);
Py_DECREF(writer);

View file

@ -163,7 +163,7 @@ mro_subclasses(PyTypeObject *type, PyObject* temp)
}
else {
PyObject* tuple;
tuple = Py_BuildValue("OO", subclass, old_mro);
tuple = PyTuple_Pack(2, subclass, old_mro);
Py_DECREF(old_mro);
if (!tuple)
return -1;
@ -258,8 +258,8 @@ type_set_bases(PyTypeObject *type, PyObject *value, void *context)
for (i = 0; i < PyList_Size(temp); i++) {
PyTypeObject* cls;
PyObject* mro;
PyArg_ParseTuple(PyList_GET_ITEM(temp, i),
"OO", &cls, &mro);
PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
"", 2, 2, &cls, &mro);
Py_DECREF(cls->tp_mro);
cls->tp_mro = mro;
Py_INCREF(cls->tp_mro);
@ -1606,7 +1606,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
/* Adjust for empty tuple bases */
if (nbases == 0) {
bases = Py_BuildValue("(O)", &PyBaseObject_Type);
bases = PyTuple_Pack(1, &PyBaseObject_Type);
if (bases == NULL)
return NULL;
nbases = 1;
@ -1650,7 +1650,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
/* Make it into a tuple */
if (PyString_Check(slots))
slots = Py_BuildValue("(O)", slots);
slots = PyTuple_Pack(1, slots);
else
slots = PySequence_Tuple(slots);
if (slots == NULL) {
@ -2644,8 +2644,7 @@ reduce_2(PyObject *obj)
PyTuple_SET_ITEM(args2, i+1, v);
}
res = Py_BuildValue("(OOOOO)",
newobj, args2, state, listitems, dictitems);
res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
end:
Py_XDECREF(cls);
@ -3142,7 +3141,7 @@ PyType_Ready(PyTypeObject *type)
if (base == NULL)
bases = PyTuple_New(0);
else
bases = Py_BuildValue("(O)", base);
bases = PyTuple_Pack(1, base);
if (bases == NULL)
goto error;
type->tp_bases = bases;
@ -4127,7 +4126,7 @@ slot_sq_contains(PyObject *self, PyObject *value)
func = lookup_maybe(self, "__contains__", &contains_str);
if (func != NULL) {
args = Py_BuildValue("(O)", value);
args = PyTuple_Pack(1, value);
if (args == NULL)
res = NULL;
else {
@ -4342,7 +4341,7 @@ half_compare(PyObject *self, PyObject *other)
PyErr_Clear();
}
else {
args = Py_BuildValue("(O)", other);
args = PyTuple_Pack(1, other);
if (args == NULL)
res = NULL;
else {
@ -4571,7 +4570,7 @@ half_richcompare(PyObject *self, PyObject *other, int op)
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
args = Py_BuildValue("(O)", other);
args = PyTuple_Pack(1, other);
if (args == NULL)
res = NULL;
else {