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)
diff --git a/Modules/_sre.c b/Modules/_sre.c
index a8a9774..5490bdc 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -1907,7 +1907,7 @@
copy = call(
"copy", "deepcopy",
- Py_BuildValue("OO", *object, memo)
+ PyTuple_Pack(2, *object, memo)
);
if (!copy)
return 0;
@@ -1968,7 +1968,7 @@
#else
result = call(
"string", "join",
- Py_BuildValue("OO", list, joiner)
+ PyTuple_Pack(2, list, joiner)
);
#endif
Py_DECREF(joiner);
@@ -2255,7 +2255,7 @@
/* 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 @@
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 @@
/* delegate to Python code */
return call(
SRE_MODULE, "_expand",
- Py_BuildValue("OOO", self->pattern, self, template)
+ PyTuple_Pack(3, self->pattern, self, template)
);
}
diff --git a/Modules/almodule.c b/Modules/almodule.c
index 6430600..12b265e 100644
--- a/Modules/almodule.c
+++ b/Modules/almodule.c
@@ -901,7 +901,7 @@
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;
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 228c8f4..9382927 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1770,7 +1770,7 @@
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,
diff --git a/Modules/cPickle.c b/Modules/cPickle.c
index c452dc1..4961c3b 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -3627,7 +3627,7 @@
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;
}
diff --git a/Modules/cStringIO.c b/Modules/cStringIO.c
index ac84ab0..ee11878 100644
--- a/Modules/cStringIO.c
+++ b/Modules/cStringIO.c
@@ -439,7 +439,7 @@
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;
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index d8aed17..0d553d4 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -3372,9 +3372,9 @@
_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 @@
_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;
diff --git a/Modules/flmodule.c b/Modules/flmodule.c
index 2fe118d..1ae2dc8 100644
--- a/Modules/flmodule.c
+++ b/Modules/flmodule.c
@@ -1714,7 +1714,7 @@
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);
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 44c6abb..5415253 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -259,23 +259,19 @@
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) {
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index fa8215a..f5787c3 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -3376,10 +3376,10 @@
{
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 @@
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 @@
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);
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index 6c40893..c1662fc 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -336,7 +336,7 @@
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;
diff --git a/Modules/regexmodule.c b/Modules/regexmodule.c
index db54161..9f84032 100644
--- a/Modules/regexmodule.c
+++ b/Modules/regexmodule.c
@@ -551,7 +551,7 @@
static int
update_cache(PyObject *pat)
{
- PyObject *tuple = Py_BuildValue("(O)", pat);
+ PyObject *tuple = PyTuple_Pack(1, pat);
int status = 0;
if (!tuple)
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 2b2d6a9..989885a 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -284,7 +284,7 @@
/* 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 @@
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);
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index a2a692ad..4a2fb58 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1161,7 +1161,7 @@
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 @@
addrlen)))
goto finally;
- ret = Py_BuildValue("OO", buf, addr);
+ ret = PyTuple_Pack(2, buf, addr);
finally:
Py_XDECREF(addr);
diff --git a/Modules/svmodule.c b/Modules/svmodule.c
index d66f1cf..9bd7968 100644
--- a/Modules/svmodule.c
+++ b/Modules/svmodule.c
@@ -157,7 +157,7 @@
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);
diff --git a/Modules/symtablemodule.c b/Modules/symtablemodule.c
index f605c22..909a404 100644
--- a/Modules/symtablemodule.c
+++ b/Modules/symtablemodule.c
@@ -31,7 +31,8 @@
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;
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 0df249d..b0e1934 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -750,7 +750,7 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
return res;
}
- args = Py_BuildValue("(O)", w);
+ args = PyTuple_Pack(1, w);
if (args == NULL) {
Py_DECREF(method);
return NULL;
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 96fa33d..c29d48d 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -439,7 +439,7 @@
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 @@
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);
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index d7c9da5..6f90fb9 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -1227,7 +1227,7 @@
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 @@
Py_DECREF(writer);
return -1;
}
- args = Py_BuildValue("(O)", value);
+ args = PyTuple_Pack(1, value);
if (args == NULL) {
Py_DECREF(value);
Py_DECREF(writer);
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index bb498e4..dcb43e5 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -163,7 +163,7 @@
}
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 @@
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 @@
/* 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 @@
/* 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 @@
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 @@
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 @@
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 @@
PyErr_Clear();
}
else {
- args = Py_BuildValue("(O)", other);
+ args = PyTuple_Pack(1, other);
if (args == NULL)
res = NULL;
else {
@@ -4571,7 +4570,7 @@
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
- args = Py_BuildValue("(O)", other);
+ args = PyTuple_Pack(1, other);
if (args == NULL)
res = NULL;
else {
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 5e74929..29804f5 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -323,7 +323,7 @@
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 @@
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 @@
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 @@
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;
diff --git a/Python/ceval.c b/Python/ceval.c
index 035520a..e6b7424 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1473,7 +1473,7 @@
x = NULL;
}
if (err == 0) {
- x = Py_BuildValue("(O)", v);
+ x = PyTuple_Pack(1, v);
if (x == NULL)
err = -1;
}
@@ -1981,7 +1981,7 @@
break;
}
u = TOP();
- w = Py_BuildValue("(OOOO)",
+ w = PyTuple_Pack(4,
w,
f->f_globals,
f->f_locals == NULL ?
@@ -2999,7 +2999,7 @@
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;
diff --git a/Python/compile.c b/Python/compile.c
index 73d9742..02c7873 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -610,7 +610,7 @@
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 @@
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);
diff --git a/Python/errors.c b/Python/errors.c
index a40844e..1788cdd 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -159,13 +159,13 @@
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 @@
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);
diff --git a/Python/exceptions.c b/Python/exceptions.c
index d489aa6..4642046 100644
--- a/Python/exceptions.c
+++ b/Python/exceptions.c
@@ -1821,7 +1821,7 @@
}
/* 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)))
{
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 018400c..21c2cac 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1037,7 +1037,7 @@
}
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) {