Skip to content

Commit 8ae4689

Browse files
committed
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)
1 parent cb2da43 commit 8ae4689

25 files changed

+71
-75
lines changed

Modules/_sre.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ deepcopy(PyObject** object, PyObject* memo)
19071907

19081908
copy = call(
19091909
"copy", "deepcopy",
1910-
Py_BuildValue("OO", *object, memo)
1910+
PyTuple_Pack(2, *object, memo)
19111911
);
19121912
if (!copy)
19131913
return 0;
@@ -1968,7 +1968,7 @@ join_list(PyObject* list, PyObject* pattern)
19681968
#else
19691969
result = call(
19701970
"string", "join",
1971-
Py_BuildValue("OO", list, joiner)
1971+
PyTuple_Pack(2, list, joiner)
19721972
);
19731973
#endif
19741974
Py_DECREF(joiner);
@@ -2255,7 +2255,7 @@ pattern_subx(PatternObject* self, PyObject* template, PyObject* string,
22552255
/* not a literal; hand it over to the template compiler */
22562256
filter = call(
22572257
SRE_MODULE, "_subx",
2258-
Py_BuildValue("OO", self, template)
2258+
PyTuple_Pack(2, self, template)
22592259
);
22602260
if (!filter)
22612261
return NULL;
@@ -2321,7 +2321,7 @@ pattern_subx(PatternObject* self, PyObject* template, PyObject* string,
23212321
match = pattern_new_match(self, &state, 1);
23222322
if (!match)
23232323
goto error;
2324-
args = Py_BuildValue("(O)", match);
2324+
args = PyTuple_Pack(1, match);
23252325
if (!args) {
23262326
Py_DECREF(match);
23272327
goto error;
@@ -2610,7 +2610,7 @@ match_expand(MatchObject* self, PyObject* args)
26102610
/* delegate to Python code */
26112611
return call(
26122612
SRE_MODULE, "_expand",
2613-
Py_BuildValue("OOO", self->pattern, self, template)
2613+
PyTuple_Pack(3, self->pattern, self, template)
26142614
);
26152615
}
26162616

Modules/almodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ alp_GetFrameTime(alpobject *self, PyObject *args)
901901
Py_XDECREF(v1);
902902
return NULL;
903903
}
904-
ret = Py_BuildValue("(OO)", v0, v1);
904+
ret = PyTuple_Pack(2, v0, v1);
905905
Py_DECREF(v0);
906906
Py_DECREF(v1);
907907
return ret;

Modules/arraymodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
17701770
Py_DECREF(v);
17711771
}
17721772
} else if (initial != NULL && PyString_Check(initial)) {
1773-
PyObject *t_initial = Py_BuildValue("(O)",
1773+
PyObject *t_initial = PyTuple_Pack(1,
17741774
initial);
17751775
PyObject *v =
17761776
array_fromstring((arrayobject *)a,

Modules/cPickle.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3627,7 +3627,7 @@ Instance_New(PyObject *cls, PyObject *args)
36273627
PyObject *tp, *v, *tb;
36283628

36293629
PyErr_Fetch(&tp, &v, &tb);
3630-
if ((r=Py_BuildValue("OOO",v,cls,args))) {
3630+
if ((r=PyTuple_Pack(3,v,cls,args))) {
36313631
Py_XDECREF(v);
36323632
v=r;
36333633
}

Modules/cStringIO.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ O_writelines(Oobject *self, PyObject *args) {
439439
tmp = PyObject_CallFunction(joiner, "O", args);
440440
UNLESS (tmp) return NULL;
441441

442-
args = Py_BuildValue("(O)", tmp);
442+
args = PyTuple_Pack(1, tmp);
443443
Py_DECREF(tmp);
444444
UNLESS (args) return NULL;
445445

Modules/datetimemodule.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -3372,9 +3372,9 @@ time_getstate(PyDateTime_Time *self)
33723372
_PyDateTime_TIME_DATASIZE);
33733373
if (basestate != NULL) {
33743374
if (! HASTZINFO(self) || self->tzinfo == Py_None)
3375-
result = Py_BuildValue("(O)", basestate);
3375+
result = PyTuple_Pack(1, basestate);
33763376
else
3377-
result = Py_BuildValue("OO", basestate, self->tzinfo);
3377+
result = PyTuple_Pack(2, basestate, self->tzinfo);
33783378
Py_DECREF(basestate);
33793379
}
33803380
return result;
@@ -4350,9 +4350,9 @@ datetime_getstate(PyDateTime_DateTime *self)
43504350
_PyDateTime_DATETIME_DATASIZE);
43514351
if (basestate != NULL) {
43524352
if (! HASTZINFO(self) || self->tzinfo == Py_None)
4353-
result = Py_BuildValue("(O)", basestate);
4353+
result = PyTuple_Pack(1, basestate);
43544354
else
4355-
result = Py_BuildValue("OO", basestate, self->tzinfo);
4355+
result = PyTuple_Pack(2, basestate, self->tzinfo);
43564356
Py_DECREF(basestate);
43574357
}
43584358
return result;

Modules/flmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ forms_do_or_check_forms(PyObject *dummy, FL_OBJECT *(*func)(void))
17141714
Py_INCREF(g);
17151715
return ((PyObject *) g);
17161716
}
1717-
arg = Py_BuildValue("(OO)", (PyObject *)g, g->ob_callback_arg);
1717+
arg = PyTuple_Pack(2, (PyObject *)g, g->ob_callback_arg);
17181718
if (arg == NULL)
17191719
return NULL;
17201720
res = PyEval_CallObject(g->ob_callback, arg);

Modules/mathmodule.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -259,23 +259,19 @@ math_log(PyObject *self, PyObject *args)
259259
if (base == NULL)
260260
return loghelper(args, log, "d:log", arg);
261261

262-
newargs = PyTuple_New(1);
262+
newargs = PyTuple_Pack(1, arg);
263263
if (newargs == NULL)
264264
return NULL;
265-
Py_INCREF(arg);
266-
PyTuple_SET_ITEM(newargs, 0, arg);
267265
num = loghelper(newargs, log, "d:log", arg);
268266
Py_DECREF(newargs);
269267
if (num == NULL)
270268
return NULL;
271269

272-
newargs = PyTuple_New(1);
270+
newargs = PyTuple_Pack(1, base);
273271
if (newargs == NULL) {
274272
Py_DECREF(num);
275273
return NULL;
276274
}
277-
Py_INCREF(base);
278-
PyTuple_SET_ITEM(newargs, 0, base);
279275
den = loghelper(newargs, log, "d:log", base);
280276
Py_DECREF(newargs);
281277
if (den == NULL) {

Modules/posixmodule.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -3376,10 +3376,10 @@ _PyPopen(char *cmdstring, int mode, int n, int bufsize)
33763376
{
33773377
if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
33783378
PyFile_SetBufSize(p_f[0], bufsize);
3379-
f = Py_BuildValue("OOO", p_f[0], p_f[1], p_f[2]);
3379+
f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
33803380
}
33813381
else
3382-
f = Py_BuildValue("OO", p_f[0], p_f[1]);
3382+
f = PyTuple_Pack(2, p_f[0], p_f[1]);
33833383

33843384
/*
33853385
* Insert the files we've created into the process dictionary
@@ -4069,7 +4069,7 @@ _PyPopen(char *cmdstring, int mode, int n)
40694069
if (n != 4)
40704070
CloseHandle(hChildStderrRdDup);
40714071

4072-
f = Py_BuildValue("OO",p1,p2);
4072+
f = PyTuple_Pack(2,p1,p2);
40734073
Py_XDECREF(p1);
40744074
Py_XDECREF(p2);
40754075
file_count = 2;
@@ -4101,7 +4101,7 @@ _PyPopen(char *cmdstring, int mode, int n)
41014101
PyFile_SetBufSize(p1, 0);
41024102
PyFile_SetBufSize(p2, 0);
41034103
PyFile_SetBufSize(p3, 0);
4104-
f = Py_BuildValue("OOO",p1,p2,p3);
4104+
f = PyTuple_Pack(3,p1,p2,p3);
41054105
Py_XDECREF(p1);
41064106
Py_XDECREF(p2);
41074107
Py_XDECREF(p3);

Modules/pyexpat.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
336336
value = Py_None;
337337
Py_INCREF(value);
338338
}
339-
arg = Py_BuildValue("(OOO)", type, value, traceback);
339+
arg = PyTuple_Pack(3, type, value, traceback);
340340
if (arg == NULL) {
341341
PyErr_Restore(type, value, traceback);
342342
return 0;

Modules/regexmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ static PyObject *cache_prog;
551551
static int
552552
update_cache(PyObject *pat)
553553
{
554-
PyObject *tuple = Py_BuildValue("(O)", pat);
554+
PyObject *tuple = PyTuple_Pack(1, pat);
555555
int status = 0;
556556

557557
if (!tuple)

Modules/selectmodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ select_select(PyObject *self, PyObject *args)
284284
/* optimization */
285285
ifdlist = PyList_New(0);
286286
if (ifdlist) {
287-
ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist);
287+
ret = PyTuple_Pack(3, ifdlist, ifdlist, ifdlist);
288288
Py_DECREF(ifdlist);
289289
}
290290
}
@@ -299,7 +299,7 @@ select_select(PyObject *self, PyObject *args)
299299
if (PyErr_Occurred())
300300
ret = NULL;
301301
else
302-
ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist);
302+
ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
303303

304304
Py_DECREF(ifdlist);
305305
Py_DECREF(ofdlist);

Modules/socketmodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ sock_accept(PySocketSockObject *s)
11611161
if (addr == NULL)
11621162
goto finally;
11631163

1164-
res = Py_BuildValue("OO", sock, addr);
1164+
res = PyTuple_Pack(2, sock, addr);
11651165

11661166
finally:
11671167
Py_XDECREF(sock);
@@ -1911,7 +1911,7 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args)
19111911
addrlen)))
19121912
goto finally;
19131913

1914-
ret = Py_BuildValue("OO", buf, addr);
1914+
ret = PyTuple_Pack(2, buf, addr);
19151915

19161916
finally:
19171917
Py_XDECREF(addr);

Modules/svmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ svc_GetFields(captureobject *self, PyObject *args)
157157
if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize,
158158
fieldsize)))
159159
goto finally;
160-
ret = Py_BuildValue("(OO)", f1, f2);
160+
ret = PyTuple_Pack(2, f1, f2);
161161

162162
finally:
163163
Py_XDECREF(f1);

Modules/symtablemodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ symtable_symtable(PyObject *self, PyObject *args)
3131
st = Py_SymtableString(str, filename, start);
3232
if (st == NULL)
3333
return NULL;
34-
t = Py_BuildValue("O", st->st_symbols);
34+
t = st->st_symbols;
35+
Py_INCREF(t);
3536
PyMem_Free((void *)st->st_future);
3637
PySymtable_Free(st);
3738
return t;

Objects/classobject.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ instance_getattr(register PyInstanceObject *inst, PyObject *name)
750750
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
751751
return NULL;
752752
PyErr_Clear();
753-
args = Py_BuildValue("(OO)", inst, name);
753+
args = PyTuple_Pack(2, inst, name);
754754
if (args == NULL)
755755
return NULL;
756756
res = PyEval_CallObject(func, args);
@@ -847,9 +847,9 @@ instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v)
847847
if (func == NULL)
848848
return instance_setattr1(inst, name, v);
849849
if (v == NULL)
850-
args = Py_BuildValue("(OO)", inst, name);
850+
args = PyTuple_Pack(2, inst, name);
851851
else
852-
args = Py_BuildValue("(OOO)", inst, name, v);
852+
args = PyTuple_Pack(3, inst, name, v);
853853
if (args == NULL)
854854
return -1;
855855
res = PyEval_CallObject(func, args);
@@ -1038,7 +1038,7 @@ instance_subscript(PyInstanceObject *inst, PyObject *key)
10381038
func = instance_getattr(inst, getitemstr);
10391039
if (func == NULL)
10401040
return NULL;
1041-
arg = Py_BuildValue("(O)", key);
1041+
arg = PyTuple_Pack(1, key);
10421042
if (arg == NULL) {
10431043
Py_DECREF(func);
10441044
return NULL;
@@ -1069,9 +1069,9 @@ instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value)
10691069
if (func == NULL)
10701070
return -1;
10711071
if (value == NULL)
1072-
arg = Py_BuildValue("(O)", key);
1072+
arg = PyTuple_Pack(1, key);
10731073
else
1074-
arg = Py_BuildValue("(OO)", key, value);
1074+
arg = PyTuple_Pack(2, key, value);
10751075
if (arg == NULL) {
10761076
Py_DECREF(func);
10771077
return -1;
@@ -1281,7 +1281,7 @@ instance_contains(PyInstanceObject *inst, PyObject *member)
12811281
if (func) {
12821282
PyObject *res;
12831283
int ret;
1284-
PyObject *arg = Py_BuildValue("(O)", member);
1284+
PyObject *arg = PyTuple_Pack(1, member);
12851285
if(arg == NULL) {
12861286
Py_DECREF(func);
12871287
return -1;
@@ -1346,7 +1346,7 @@ generic_binary_op(PyObject *v, PyObject *w, char *opname)
13461346
Py_INCREF(Py_NotImplemented);
13471347
return Py_NotImplemented;
13481348
}
1349-
args = Py_BuildValue("(O)", w);
1349+
args = PyTuple_Pack(1, w);
13501350
if (args == NULL) {
13511351
Py_DECREF(func);
13521352
return NULL;
@@ -1389,7 +1389,7 @@ half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc,
13891389
return generic_binary_op(v, w, opname);
13901390
}
13911391

1392-
args = Py_BuildValue("(O)", w);
1392+
args = PyTuple_Pack(1, w);
13931393
if (args == NULL) {
13941394
Py_DECREF(coercefunc);
13951395
return NULL;
@@ -1474,7 +1474,7 @@ instance_coerce(PyObject **pv, PyObject **pw)
14741474
return 1;
14751475
}
14761476
/* Has __coerce__ method: call it */
1477-
args = Py_BuildValue("(O)", w);
1477+
args = PyTuple_Pack(1, w);
14781478
if (args == NULL) {
14791479
return -1;
14801480
}
@@ -1587,7 +1587,7 @@ half_cmp(PyObject *v, PyObject *w)
15871587
return 2;
15881588
}
15891589

1590-
args = Py_BuildValue("(O)", w);
1590+
args = PyTuple_Pack(1, w);
15911591
if (args == NULL) {
15921592
Py_DECREF(cmp_func);
15931593
return -2;
@@ -1747,7 +1747,7 @@ instance_pow(PyObject *v, PyObject *w, PyObject *z)
17471747
func = PyObject_GetAttrString(v, "__pow__");
17481748
if (func == NULL)
17491749
return NULL;
1750-
args = Py_BuildValue("(OO)", w, z);
1750+
args = PyTuple_Pack(2, w, z);
17511751
if (args == NULL) {
17521752
Py_DECREF(func);
17531753
return NULL;
@@ -1786,7 +1786,7 @@ instance_ipow(PyObject *v, PyObject *w, PyObject *z)
17861786
PyErr_Clear();
17871787
return instance_pow(v, w, z);
17881788
}
1789-
args = Py_BuildValue("(OO)", w, z);
1789+
args = PyTuple_Pack(2, w, z);
17901790
if (args == NULL) {
17911791
Py_DECREF(func);
17921792
return NULL;
@@ -1859,7 +1859,7 @@ half_richcompare(PyObject *v, PyObject *w, int op)
18591859
return res;
18601860
}
18611861

1862-
args = Py_BuildValue("(O)", w);
1862+
args = PyTuple_Pack(1, w);
18631863
if (args == NULL) {
18641864
Py_DECREF(method);
18651865
return NULL;

Objects/complexobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ complex_divmod(PyComplexObject *v, PyComplexObject *w)
439439
mod = c_diff(v->cval, c_prod(w->cval, div));
440440
d = PyComplex_FromCComplex(div);
441441
m = PyComplex_FromCComplex(mod);
442-
z = Py_BuildValue("(OO)", d, m);
442+
z = PyTuple_Pack(2, d, m);
443443
Py_XDECREF(d);
444444
Py_XDECREF(m);
445445
return z;
@@ -865,7 +865,7 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
865865
if (f == NULL)
866866
PyErr_Clear();
867867
else {
868-
PyObject *args = Py_BuildValue("()");
868+
PyObject *args = PyTuple_New(0);
869869
if (args == NULL)
870870
return NULL;
871871
r = PyEval_CallObject(f, args);

Objects/fileobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ PyFile_GetLine(PyObject *f, int n)
12271227
if (reader == NULL)
12281228
return NULL;
12291229
if (n <= 0)
1230-
args = Py_BuildValue("()");
1230+
args = PyTuple_New(0);
12311231
else
12321232
args = Py_BuildValue("(i)", n);
12331233
if (args == NULL) {
@@ -2104,7 +2104,7 @@ PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
21042104
Py_DECREF(writer);
21052105
return -1;
21062106
}
2107-
args = Py_BuildValue("(O)", value);
2107+
args = PyTuple_Pack(1, value);
21082108
if (args == NULL) {
21092109
Py_DECREF(value);
21102110
Py_DECREF(writer);

0 commit comments

Comments
 (0)