Thanks to visit codestin.com
Credit goes to github.com

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

Lines changed: 71 additions & 75 deletions

Modules/_sre.c

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)