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

Skip to content

Commit ea3fdf4

Browse files
committed
SF patch #659536: Use PyArg_UnpackTuple where possible.
Obtain cleaner coding and a system wide performance boost by using the fast, pre-parsed PyArg_Unpack function instead of PyArg_ParseTuple function which is driven by a format string.
1 parent f8bcfb1 commit ea3fdf4

11 files changed

Lines changed: 41 additions & 41 deletions

File tree

Modules/mathmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ math_log(PyObject *self, PyObject *args)
254254
PyObject *ans;
255255
PyObject *newargs;
256256

257-
if (! PyArg_ParseTuple(args, "O|O:log", &arg, &base))
257+
if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
258258
return NULL;
259259
if (base == NULL)
260260
return loghelper(args, log, "d:log", arg);
@@ -298,7 +298,7 @@ math_log10(PyObject *self, PyObject *args)
298298
{
299299
PyObject *arg;
300300

301-
if (! PyArg_ParseTuple(args, "O:log10", &arg))
301+
if (!PyArg_UnpackTuple(args, "log10", 1, 1, &arg))
302302
return NULL;
303303
return loghelper(args, log10, "d:log10", arg);
304304
}

Modules/operator.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ used for special class methods; variants without leading and trailing\n\
1212

1313
#define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
1414
PyObject *a1; \
15-
if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \
15+
if(! PyArg_UnpackTuple(a,#OP,1,1,&a1)) return NULL; \
1616
return AOP(a1); }
1717

1818
#define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
1919
PyObject *a1, *a2; \
20-
if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
20+
if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
2121
return AOP(a1,a2); }
2222

2323
#define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
@@ -27,39 +27,39 @@ used for special class methods; variants without leading and trailing\n\
2727

2828
#define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
2929
PyObject *a1, *a2; \
30-
if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
30+
if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
3131
if(-1 == AOP(a1,a2)) return NULL; \
3232
Py_INCREF(Py_None); \
3333
return Py_None; }
3434

3535
#define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
3636
PyObject *a1, *a2, *a3; \
37-
if(! PyArg_ParseTuple(a,"OOO:" #OP,&a1,&a2,&a3)) return NULL; \
37+
if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \
3838
if(-1 == AOP(a1,a2,a3)) return NULL; \
3939
Py_INCREF(Py_None); \
4040
return Py_None; }
4141

4242
#define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
4343
PyObject *a1; long r; \
44-
if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \
44+
if(! PyArg_UnpackTuple(a,#OP,1,1,&a1)) return NULL; \
4545
if(-1 == (r=AOP(a1))) return NULL; \
4646
return PyBool_FromLong(r); }
4747

4848
#define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
4949
PyObject *a1, *a2; long r; \
50-
if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
50+
if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
5151
if(-1 == (r=AOP(a1,a2))) return NULL; \
5252
return PyInt_FromLong(r); }
5353

5454
#define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
5555
PyObject *a1, *a2; long r; \
56-
if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
56+
if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
5757
if(-1 == (r=AOP(a1,a2))) return NULL; \
5858
return PyBool_FromLong(r); }
5959

6060
#define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
6161
PyObject *a1, *a2; \
62-
if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
62+
if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
6363
return PyObject_RichCompare(a1,a2,A); }
6464

6565
spami(isCallable , PyCallable_Check)
@@ -105,7 +105,7 @@ static PyObject*
105105
op_pow(PyObject *s, PyObject *a)
106106
{
107107
PyObject *a1, *a2;
108-
if (PyArg_ParseTuple(a,"OO:pow",&a1,&a2))
108+
if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
109109
return PyNumber_Power(a1, a2, Py_None);
110110
return NULL;
111111
}

Objects/classobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2178,7 +2178,7 @@ instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
21782178
PyObject *self;
21792179
PyObject *classObj;
21802180

2181-
if (!PyArg_ParseTuple(args, "OOO:instancemethod",
2181+
if (!PyArg_UnpackTuple(args, "instancemethod", 3, 3,
21822182
&func, &self, &classObj))
21832183
return NULL;
21842184
if (!PyCallable_Check(func)) {

Objects/descrobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ proxy_get(proxyobject *pp, PyObject *args)
686686
{
687687
PyObject *key, *def = Py_None;
688688

689-
if (!PyArg_ParseTuple(args, "O|O:get", &key, &def))
689+
if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
690690
return NULL;
691691
return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
692692
}

Objects/dictobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ dict_fromkeys(PyObject *cls, PyObject *args)
972972
PyObject *d;
973973
int status;
974974

975-
if (!PyArg_ParseTuple(args, "O|O:fromkeys", &seq, &value))
975+
if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
976976
return NULL;
977977

978978
d = PyObject_CallObject(cls, NULL);
@@ -1479,7 +1479,7 @@ dict_get(register dictobject *mp, PyObject *args)
14791479
PyObject *val = NULL;
14801480
long hash;
14811481

1482-
if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
1482+
if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
14831483
return NULL;
14841484

14851485
if (!PyString_CheckExact(key) ||
@@ -1505,7 +1505,7 @@ dict_setdefault(register dictobject *mp, PyObject *args)
15051505
PyObject *val = NULL;
15061506
long hash;
15071507

1508-
if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1508+
if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
15091509
return NULL;
15101510

15111511
if (!PyString_CheckExact(key) ||
@@ -1834,7 +1834,7 @@ dict_init(PyObject *self, PyObject *args, PyObject *kwds)
18341834
PyObject *arg = NULL;
18351835
int result = 0;
18361836

1837-
if (!PyArg_ParseTuple(args, "|O:dict", &arg))
1837+
if (!PyArg_UnpackTuple(args, "dict", 0, 1, &arg))
18381838
result = -1;
18391839

18401840
else if (arg != NULL) {

Objects/fileobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ file_truncate(PyFileObject *f, PyObject *args)
503503
if (f->f_fp == NULL)
504504
return err_closed();
505505
newsizeobj = NULL;
506-
if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj))
506+
if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
507507
return NULL;
508508

509509
/* Set newsize to current postion if newsizeobj NULL, else to the

Objects/funcobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ cm_init(PyObject *self, PyObject *args, PyObject *kwds)
588588
classmethod *cm = (classmethod *)self;
589589
PyObject *callable;
590590

591-
if (!PyArg_ParseTuple(args, "O:classmethod", &callable))
591+
if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
592592
return -1;
593593
Py_INCREF(callable);
594594
cm->cm_callable = callable;
@@ -720,7 +720,7 @@ sm_init(PyObject *self, PyObject *args, PyObject *kwds)
720720
staticmethod *sm = (staticmethod *)self;
721721
PyObject *callable;
722722

723-
if (!PyArg_ParseTuple(args, "O:staticmethod", &callable))
723+
if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
724724
return -1;
725725
Py_INCREF(callable);
726726
sm->sm_callable = callable;

Objects/listobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ listsort(PyListObject *self, PyObject *args)
16541654

16551655
assert(self != NULL);
16561656
if (args != NULL) {
1657-
if (!PyArg_ParseTuple(args, "|O:sort", &compare))
1657+
if (!PyArg_UnpackTuple(args, "sort", 0, 1, &compare))
16581658
return NULL;
16591659
}
16601660
merge_init(&ms, compare);

Objects/sliceobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
174174

175175
start = stop = step = NULL;
176176

177-
if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
177+
if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
178178
return NULL;
179179

180180
/* This swapping of stop and start is to maintain similarity with

Objects/stringobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@ string_translate(PyStringObject *self, PyObject *args)
20462046
int trans_table[256];
20472047
PyObject *tableobj, *delobj = NULL;
20482048

2049-
if (!PyArg_ParseTuple(args, "O|O:translate",
2049+
if (!PyArg_UnpackTuple(args, "translate", 1, 2,
20502050
&tableobj, &delobj))
20512051
return NULL;
20522052

0 commit comments

Comments
 (0)