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

@ -1907,7 +1907,7 @@ deepcopy(PyObject** object, PyObject* memo)
copy = call(
"copy", "deepcopy",
Py_BuildValue("OO", *object, memo)
PyTuple_Pack(2, *object, memo)
);
if (!copy)
return 0;
@ -1968,7 +1968,7 @@ join_list(PyObject* list, PyObject* pattern)
#else
result = call(
"string", "join",
Py_BuildValue("OO", list, joiner)
PyTuple_Pack(2, list, joiner)
);
#endif
Py_DECREF(joiner);
@ -2255,7 +2255,7 @@ pattern_subx(PatternObject* self, PyObject* template, PyObject* string,
/* not a literal; hand it over to the template compiler */
filter = call(
SRE_MODULE, "_subx",
Py_BuildValue("OO", self, template)
PyTuple_Pack(2, self, template)
);
if (!filter)
return NULL;
@ -2321,7 +2321,7 @@ pattern_subx(PatternObject* self, PyObject* template, PyObject* string,
match = pattern_new_match(self, &state, 1);
if (!match)
goto error;
args = Py_BuildValue("(O)", match);
args = PyTuple_Pack(1, match);
if (!args) {
Py_DECREF(match);
goto error;
@ -2610,7 +2610,7 @@ match_expand(MatchObject* self, PyObject* args)
/* delegate to Python code */
return call(
SRE_MODULE, "_expand",
Py_BuildValue("OOO", self->pattern, self, template)
PyTuple_Pack(3, self->pattern, self, template)
);
}

View File

@ -901,7 +901,7 @@ alp_GetFrameTime(alpobject *self, PyObject *args)
Py_XDECREF(v1);
return NULL;
}
ret = Py_BuildValue("(OO)", v0, v1);
ret = PyTuple_Pack(2, v0, v1);
Py_DECREF(v0);
Py_DECREF(v1);
return ret;

View File

@ -1770,7 +1770,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_DECREF(v);
}
} else if (initial != NULL && PyString_Check(initial)) {
PyObject *t_initial = Py_BuildValue("(O)",
PyObject *t_initial = PyTuple_Pack(1,
initial);
PyObject *v =
array_fromstring((arrayobject *)a,

View File

@ -3627,7 +3627,7 @@ Instance_New(PyObject *cls, PyObject *args)
PyObject *tp, *v, *tb;
PyErr_Fetch(&tp, &v, &tb);
if ((r=Py_BuildValue("OOO",v,cls,args))) {
if ((r=PyTuple_Pack(3,v,cls,args))) {
Py_XDECREF(v);
v=r;
}

View File

@ -439,7 +439,7 @@ O_writelines(Oobject *self, PyObject *args) {
tmp = PyObject_CallFunction(joiner, "O", args);
UNLESS (tmp) return NULL;
args = Py_BuildValue("(O)", tmp);
args = PyTuple_Pack(1, tmp);
Py_DECREF(tmp);
UNLESS (args) return NULL;

View File

@ -3372,9 +3372,9 @@ time_getstate(PyDateTime_Time *self)
_PyDateTime_TIME_DATASIZE);
if (basestate != NULL) {
if (! HASTZINFO(self) || self->tzinfo == Py_None)
result = Py_BuildValue("(O)", basestate);
result = PyTuple_Pack(1, basestate);
else
result = Py_BuildValue("OO", basestate, self->tzinfo);
result = PyTuple_Pack(2, basestate, self->tzinfo);
Py_DECREF(basestate);
}
return result;
@ -4350,9 +4350,9 @@ datetime_getstate(PyDateTime_DateTime *self)
_PyDateTime_DATETIME_DATASIZE);
if (basestate != NULL) {
if (! HASTZINFO(self) || self->tzinfo == Py_None)
result = Py_BuildValue("(O)", basestate);
result = PyTuple_Pack(1, basestate);
else
result = Py_BuildValue("OO", basestate, self->tzinfo);
result = PyTuple_Pack(2, basestate, self->tzinfo);
Py_DECREF(basestate);
}
return result;

View File

@ -1714,7 +1714,7 @@ forms_do_or_check_forms(PyObject *dummy, FL_OBJECT *(*func)(void))
Py_INCREF(g);
return ((PyObject *) g);
}
arg = Py_BuildValue("(OO)", (PyObject *)g, g->ob_callback_arg);
arg = PyTuple_Pack(2, (PyObject *)g, g->ob_callback_arg);
if (arg == NULL)
return NULL;
res = PyEval_CallObject(g->ob_callback, arg);

View File

@ -259,23 +259,19 @@ math_log(PyObject *self, PyObject *args)
if (base == NULL)
return loghelper(args, log, "d:log", arg);
newargs = PyTuple_New(1);
newargs = PyTuple_Pack(1, arg);
if (newargs == NULL)
return NULL;
Py_INCREF(arg);
PyTuple_SET_ITEM(newargs, 0, arg);
num = loghelper(newargs, log, "d:log", arg);
Py_DECREF(newargs);
if (num == NULL)
return NULL;
newargs = PyTuple_New(1);
newargs = PyTuple_Pack(1, base);
if (newargs == NULL) {
Py_DECREF(num);
return NULL;
}
Py_INCREF(base);
PyTuple_SET_ITEM(newargs, 0, base);
den = loghelper(newargs, log, "d:log", base);
Py_DECREF(newargs);
if (den == NULL) {

View File

@ -3376,10 +3376,10 @@ _PyPopen(char *cmdstring, int mode, int n, int bufsize)
{
if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
PyFile_SetBufSize(p_f[0], bufsize);
f = Py_BuildValue("OOO", p_f[0], p_f[1], p_f[2]);
f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
}
else
f = Py_BuildValue("OO", p_f[0], p_f[1]);
f = PyTuple_Pack(2, p_f[0], p_f[1]);
/*
* Insert the files we've created into the process dictionary
@ -4069,7 +4069,7 @@ _PyPopen(char *cmdstring, int mode, int n)
if (n != 4)
CloseHandle(hChildStderrRdDup);
f = Py_BuildValue("OO",p1,p2);
f = PyTuple_Pack(2,p1,p2);
Py_XDECREF(p1);
Py_XDECREF(p2);
file_count = 2;
@ -4101,7 +4101,7 @@ _PyPopen(char *cmdstring, int mode, int n)
PyFile_SetBufSize(p1, 0);
PyFile_SetBufSize(p2, 0);
PyFile_SetBufSize(p3, 0);
f = Py_BuildValue("OOO",p1,p2,p3);
f = PyTuple_Pack(3,p1,p2,p3);
Py_XDECREF(p1);
Py_XDECREF(p2);
Py_XDECREF(p3);

View File

@ -336,7 +336,7 @@ trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
value = Py_None;
Py_INCREF(value);
}
arg = Py_BuildValue("(OOO)", type, value, traceback);
arg = PyTuple_Pack(3, type, value, traceback);
if (arg == NULL) {
PyErr_Restore(type, value, traceback);
return 0;

View File

@ -551,7 +551,7 @@ static PyObject *cache_prog;
static int
update_cache(PyObject *pat)
{
PyObject *tuple = Py_BuildValue("(O)", pat);
PyObject *tuple = PyTuple_Pack(1, pat);
int status = 0;
if (!tuple)

View File

@ -284,7 +284,7 @@ select_select(PyObject *self, PyObject *args)
/* optimization */
ifdlist = PyList_New(0);
if (ifdlist) {
ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist);
ret = PyTuple_Pack(3, ifdlist, ifdlist, ifdlist);
Py_DECREF(ifdlist);
}
}
@ -299,7 +299,7 @@ select_select(PyObject *self, PyObject *args)
if (PyErr_Occurred())
ret = NULL;
else
ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist);
ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Py_DECREF(ifdlist);
Py_DECREF(ofdlist);

View File

@ -1161,7 +1161,7 @@ sock_accept(PySocketSockObject *s)
if (addr == NULL)
goto finally;
res = Py_BuildValue("OO", sock, addr);
res = PyTuple_Pack(2, sock, addr);
finally:
Py_XDECREF(sock);
@ -1911,7 +1911,7 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args)
addrlen)))
goto finally;
ret = Py_BuildValue("OO", buf, addr);
ret = PyTuple_Pack(2, buf, addr);
finally:
Py_XDECREF(addr);

View File

@ -157,7 +157,7 @@ svc_GetFields(captureobject *self, PyObject *args)
if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize,
fieldsize)))
goto finally;
ret = Py_BuildValue("(OO)", f1, f2);
ret = PyTuple_Pack(2, f1, f2);
finally:
Py_XDECREF(f1);

View File

@ -31,7 +31,8 @@ symtable_symtable(PyObject *self, PyObject *args)
st = Py_SymtableString(str, filename, start);
if (st == NULL)
return NULL;
t = Py_BuildValue("O", st->st_symbols);
t = st->st_symbols;
Py_INCREF(t);
PyMem_Free((void *)st->st_future);
PySymtable_Free(st);
return t;

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 {

View File

@ -323,7 +323,7 @@ builtin_coerce(PyObject *self, PyObject *args)
return NULL;
if (PyNumber_Coerce(&v, &w) < 0)
return NULL;
res = Py_BuildValue("(OO)", v, w);
res = PyTuple_Pack(2, v, w);
Py_DECREF(v);
Py_DECREF(w);
return res;
@ -2185,7 +2185,7 @@ filtertuple(PyObject *func, PyObject *tuple)
good = item;
}
else {
PyObject *arg = Py_BuildValue("(O)", item);
PyObject *arg = PyTuple_Pack(1, item);
if (arg == NULL) {
Py_DECREF(item);
goto Fail_1;
@ -2252,7 +2252,7 @@ filterstring(PyObject *func, PyObject *strobj)
ok = 1;
} else {
PyObject *arg, *good;
arg = Py_BuildValue("(O)", item);
arg = PyTuple_Pack(1, item);
if (arg == NULL) {
Py_DECREF(item);
goto Fail_1;
@ -2346,7 +2346,7 @@ filterunicode(PyObject *func, PyObject *strobj)
if (func == Py_None) {
ok = 1;
} else {
arg = Py_BuildValue("(O)", item);
arg = PyTuple_Pack(1, item);
if (arg == NULL) {
Py_DECREF(item);
goto Fail_1;

View File

@ -1473,7 +1473,7 @@ eval_frame(PyFrameObject *f)
x = NULL;
}
if (err == 0) {
x = Py_BuildValue("(O)", v);
x = PyTuple_Pack(1, v);
if (x == NULL)
err = -1;
}
@ -1981,7 +1981,7 @@ eval_frame(PyFrameObject *f)
break;
}
u = TOP();
w = Py_BuildValue("(OOOO)",
w = PyTuple_Pack(4,
w,
f->f_globals,
f->f_locals == NULL ?
@ -2999,7 +2999,7 @@ call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
value = Py_None;
Py_INCREF(value);
}
arg = Py_BuildValue("(OOO)", type, value, traceback);
arg = PyTuple_Pack(3, type, value, traceback);
if (arg == NULL) {
PyErr_Restore(type, value, traceback);
return;

View File

@ -610,7 +610,7 @@ com_error(struct compiling *c, PyObject *exc, char *msg)
Py_None, line);
if (t == NULL)
goto exit;
w = Py_BuildValue("(OO)", v, t);
w = PyTuple_Pack(2, v, t);
if (w == NULL)
goto exit;
PyErr_SetObject(exc, w);
@ -969,7 +969,7 @@ com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
PyObject *w, *t, *np=NULL;
long n;
t = Py_BuildValue("(OO)", v, v->ob_type);
t = PyTuple_Pack(2, v, v->ob_type);
if (t == NULL)
goto fail;
w = PyDict_GetItem(dict, t);

View File

@ -159,13 +159,13 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
PyObject *args, *res;
if (value == Py_None)
args = Py_BuildValue("()");
args = PyTuple_New(0);
else if (PyTuple_Check(value)) {
Py_INCREF(value);
args = value;
}
else
args = Py_BuildValue("(O)", value);
args = PyTuple_Pack(1, value);
if (args == NULL)
goto finally;
@ -560,7 +560,7 @@ PyErr_NewException(char *name, PyObject *base, PyObject *dict)
classname = PyString_FromString(dot+1);
if (classname == NULL)
goto failure;
bases = Py_BuildValue("(O)", base);
bases = PyTuple_Pack(1, base);
if (bases == NULL)
goto failure;
result = PyClass_New(bases, dict, classname);

View File

@ -1821,7 +1821,7 @@ _PyExc_Init(void)
}
/* Now we need to pre-allocate a MemoryError instance */
args = Py_BuildValue("()");
args = PyTuple_New(0);
if (!args ||
!(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
{

View File

@ -1037,7 +1037,7 @@ PyErr_PrintEx(int set_sys_last_vars)
}
hook = PySys_GetObject("excepthook");
if (hook) {
PyObject *args = Py_BuildValue("(OOO)",
PyObject *args = PyTuple_Pack(3,
exception, v ? v : Py_None, tb ? tb : Py_None);
PyObject *result = PyEval_CallObject(hook, args);
if (result == NULL) {