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 a2a692a..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;