]> granicus.if.org Git - python/commitdiff
SF patch #659536: Use PyArg_UnpackTuple where possible.
authorRaymond Hettinger <python@rcn.com>
Sun, 29 Dec 2002 16:33:45 +0000 (16:33 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 29 Dec 2002 16:33:45 +0000 (16:33 +0000)
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.

Modules/mathmodule.c
Modules/operator.c
Objects/classobject.c
Objects/descrobject.c
Objects/dictobject.c
Objects/fileobject.c
Objects/funcobject.c
Objects/listobject.c
Objects/sliceobject.c
Objects/stringobject.c
Python/bltinmodule.c

index 25728864fffe5ab0fdb6d4fa2f3178ad94e96006..44c6abbd5368838d6b564b657b01a1ece0ea103a 100644 (file)
@@ -254,7 +254,7 @@ math_log(PyObject *self, PyObject *args)
        PyObject *ans;
        PyObject *newargs;
 
-       if (! PyArg_ParseTuple(args, "O|O:log", &arg, &base))
+       if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
                return NULL;
        if (base == NULL)
                return loghelper(args, log, "d:log", arg);
@@ -298,7 +298,7 @@ math_log10(PyObject *self, PyObject *args)
 {
        PyObject *arg;
 
-       if (! PyArg_ParseTuple(args, "O:log10", &arg))
+       if (!PyArg_UnpackTuple(args, "log10", 1, 1, &arg))
                return NULL;
        return loghelper(args, log10, "d:log10", arg);
 }
index 55c26df151291c4b173fe65357b0b0c3f1ad2b4d..d89564d5064954ad71e3e7d72fbc287112965ca6 100644 (file)
@@ -12,12 +12,12 @@ used for special class methods; variants without leading and trailing\n\
 
 #define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
   PyObject *a1; \
-  if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \
+  if(! PyArg_UnpackTuple(a,#OP,1,1,&a1)) return NULL; \
   return AOP(a1); }
 
 #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
   PyObject *a1, *a2; \
-  if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
+  if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
   return AOP(a1,a2); }
 
 #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\
 
 #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
   PyObject *a1, *a2; \
-  if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
+  if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
   if(-1 == AOP(a1,a2)) return NULL; \
   Py_INCREF(Py_None); \
   return Py_None; }
 
 #define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
   PyObject *a1, *a2, *a3; \
-  if(! PyArg_ParseTuple(a,"OOO:" #OP,&a1,&a2,&a3)) return NULL; \
+  if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \
   if(-1 == AOP(a1,a2,a3)) return NULL; \
   Py_INCREF(Py_None); \
   return Py_None; }
 
 #define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
   PyObject *a1; long r; \
-  if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \
+  if(! PyArg_UnpackTuple(a,#OP,1,1,&a1)) return NULL; \
   if(-1 == (r=AOP(a1))) return NULL; \
   return PyBool_FromLong(r); }
 
 #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
   PyObject *a1, *a2; long r; \
-  if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
+  if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
   if(-1 == (r=AOP(a1,a2))) return NULL; \
   return PyInt_FromLong(r); }
 
 #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
   PyObject *a1, *a2; long r; \
-  if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
+  if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
   if(-1 == (r=AOP(a1,a2))) return NULL; \
   return PyBool_FromLong(r); }
 
 #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
   PyObject *a1, *a2; \
-  if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
+  if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
   return PyObject_RichCompare(a1,a2,A); }
 
 spami(isCallable       , PyCallable_Check)
@@ -105,7 +105,7 @@ static PyObject*
 op_pow(PyObject *s, PyObject *a)
 {
        PyObject *a1, *a2;
-       if (PyArg_ParseTuple(a,"OO:pow",&a1,&a2))
+       if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
                return PyNumber_Power(a1, a2, Py_None);
        return NULL;
 }
index 5234a658a248e78da1509aaa85c4965ccbb07577..bc2234528b801faa7f7ac3ca3feacbfc62be90ed 100644 (file)
@@ -2178,7 +2178,7 @@ instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
        PyObject *self;
        PyObject *classObj;
 
-       if (!PyArg_ParseTuple(args, "OOO:instancemethod",
+       if (!PyArg_UnpackTuple(args, "instancemethod", 3, 3,
                              &func, &self, &classObj))
                return NULL;
        if (!PyCallable_Check(func)) {
index 761e1ab854c0e858024efb33e95697428dcecbe7..6c78778fd593e7861ac017073a77268d930f0ac7 100644 (file)
@@ -686,7 +686,7 @@ proxy_get(proxyobject *pp, PyObject *args)
 {
        PyObject *key, *def = Py_None;
 
-       if (!PyArg_ParseTuple(args, "O|O:get", &key, &def))
+       if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
                return NULL;
        return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
 }
index 5e39dfa96793f66fc6a26178bd8185e5e933c2ce..de7a18ec11bbe9e52f98735d18b9c754e771c1c6 100644 (file)
@@ -972,7 +972,7 @@ dict_fromkeys(PyObject *cls, PyObject *args)
        PyObject *d;
        int status;
 
-       if (!PyArg_ParseTuple(args, "O|O:fromkeys", &seq, &value))
+       if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
                return NULL;
 
        d = PyObject_CallObject(cls, NULL);
@@ -1479,7 +1479,7 @@ dict_get(register dictobject *mp, PyObject *args)
        PyObject *val = NULL;
        long hash;
 
-       if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
+       if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
                return NULL;
 
        if (!PyString_CheckExact(key) ||
@@ -1505,7 +1505,7 @@ dict_setdefault(register dictobject *mp, PyObject *args)
        PyObject *val = NULL;
        long hash;
 
-       if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
+       if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
                return NULL;
 
        if (!PyString_CheckExact(key) ||
@@ -1834,7 +1834,7 @@ dict_init(PyObject *self, PyObject *args, PyObject *kwds)
        PyObject *arg = NULL;
        int result = 0;
 
-       if (!PyArg_ParseTuple(args, "|O:dict", &arg))
+       if (!PyArg_UnpackTuple(args, "dict", 0, 1, &arg))
                result = -1;
 
        else if (arg != NULL) {
index 33fb3bc168a2882aa489e3c19174b70832228d71..fb73385217f21c54db20e5cd9ddbaabca6119505 100644 (file)
@@ -503,7 +503,7 @@ file_truncate(PyFileObject *f, PyObject *args)
        if (f->f_fp == NULL)
                return err_closed();
        newsizeobj = NULL;
-       if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj))
+       if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
                return NULL;
 
        /* Set newsize to current postion if newsizeobj NULL, else to the
index 4f36df97472d91e0492c06289680869721aa2643..6154d99a5ef2c94057fc25b0594c950fcad85753 100644 (file)
@@ -588,7 +588,7 @@ cm_init(PyObject *self, PyObject *args, PyObject *kwds)
        classmethod *cm = (classmethod *)self;
        PyObject *callable;
 
-       if (!PyArg_ParseTuple(args, "O:classmethod", &callable))
+       if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
                return -1;
        Py_INCREF(callable);
        cm->cm_callable = callable;
@@ -720,7 +720,7 @@ sm_init(PyObject *self, PyObject *args, PyObject *kwds)
        staticmethod *sm = (staticmethod *)self;
        PyObject *callable;
 
-       if (!PyArg_ParseTuple(args, "O:staticmethod", &callable))
+       if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
                return -1;
        Py_INCREF(callable);
        sm->sm_callable = callable;
index ba470285ebf59ab11125921d70447c790b35c4e9..461350c79c5ceb40e9a58b3a1ec98775753a525b 100644 (file)
@@ -1654,7 +1654,7 @@ listsort(PyListObject *self, PyObject *args)
 
        assert(self != NULL);
        if (args != NULL) {
-               if (!PyArg_ParseTuple(args, "|O:sort", &compare))
+               if (!PyArg_UnpackTuple(args, "sort", 0, 1, &compare))
                        return NULL;
        }
        merge_init(&ms, compare);
index 7198cca4b1c71d85d166c6a7512815a03371c710..796df2bb0cb77a8f17a2db3218ca4d84493418bf 100644 (file)
@@ -174,7 +174,7 @@ slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
 
        start = stop = step = NULL;
 
-       if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
+       if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
                return NULL;
 
        /* This swapping of stop and start is to maintain similarity with
index 7937b46a3d9454a45e9363fd71c9c3823c690672..1e4285672949f4e8382d7b7e176cf8fd4dfe956b 100644 (file)
@@ -2046,7 +2046,7 @@ string_translate(PyStringObject *self, PyObject *args)
        int trans_table[256];
        PyObject *tableobj, *delobj = NULL;
 
-       if (!PyArg_ParseTuple(args, "O|O:translate",
+       if (!PyArg_UnpackTuple(args, "translate", 1, 2,
                              &tableobj, &delobj))
                return NULL;
 
index ab760068903799b7d5c67e62aa66912c30487c60..5cf1d413ae90aaf5e771bbd3352dee09b7be6ddb 100644 (file)
@@ -70,7 +70,7 @@ builtin_apply(PyObject *self, PyObject *args)
        PyObject *func, *alist = NULL, *kwdict = NULL;
        PyObject *t = NULL, *retval = NULL;
 
-       if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
+       if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
                return NULL;
        if (alist != NULL) {
                if (!PyTuple_Check(alist)) {
@@ -126,7 +126,7 @@ builtin_filter(PyObject *self, PyObject *args)
        int len;   /* guess for result list size */
        register int j;
 
-       if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
+       if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
                return NULL;
 
        /* Strings and tuples return a result of the same type. */
@@ -284,7 +284,7 @@ builtin_cmp(PyObject *self, PyObject *args)
        PyObject *a, *b;
        int c;
 
-       if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
+       if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
                return NULL;
        if (PyObject_Cmp(a, b, &c) < 0)
                return NULL;
@@ -303,7 +303,7 @@ builtin_coerce(PyObject *self, PyObject *args)
        PyObject *v, *w;
        PyObject *res;
 
-       if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
+       if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
                return NULL;
        if (PyNumber_Coerce(&v, &w) < 0)
                return NULL;
@@ -381,7 +381,7 @@ builtin_dir(PyObject *self, PyObject *args)
 {
        PyObject *arg = NULL;
 
-       if (!PyArg_ParseTuple(args, "|O:dir", &arg))
+       if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
                return NULL;
        return PyObject_Dir(arg);
 }
@@ -404,7 +404,7 @@ builtin_divmod(PyObject *self, PyObject *args)
 {
        PyObject *v, *w;
 
-       if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
+       if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
                return NULL;
        return PyNumber_Divmod(v, w);
 }
@@ -580,7 +580,7 @@ builtin_getattr(PyObject *self, PyObject *args)
        PyObject *v, *result, *dflt = NULL;
        PyObject *name;
 
-       if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
+       if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
                return NULL;
 #ifdef Py_USING_UNICODE
        if (PyUnicode_Check(name)) {
@@ -636,7 +636,7 @@ builtin_hasattr(PyObject *self, PyObject *args)
        PyObject *v;
        PyObject *name;
 
-       if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
+       if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
                return NULL;
 #ifdef Py_USING_UNICODE
        if (PyUnicode_Check(name)) {
@@ -856,7 +856,7 @@ builtin_setattr(PyObject *self, PyObject *args)
        PyObject *name;
        PyObject *value;
 
-       if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
+       if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
                return NULL;
        if (PyObject_SetAttr(v, name, value) != 0)
                return NULL;
@@ -877,7 +877,7 @@ builtin_delattr(PyObject *self, PyObject *args)
        PyObject *v;
        PyObject *name;
 
-       if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
+       if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
                return NULL;
        if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
                return NULL;
@@ -990,7 +990,7 @@ builtin_iter(PyObject *self, PyObject *args)
 {
        PyObject *v, *w = NULL;
 
-       if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
+       if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
                return NULL;
        if (w == NULL)
                return PyObject_GetIter(v);
@@ -1051,7 +1051,7 @@ min_max(PyObject *args, int op)
 
        if (PyTuple_Size(args) > 1)
                v = args;
-       else if (!PyArg_ParseTuple(args, "O:min/max", &v))
+       else if (!PyArg_UnpackTuple(args, "min/max", 1, 1, &v))
                return NULL;
 
        it = PyObject_GetIter(v);
@@ -1188,7 +1188,7 @@ builtin_pow(PyObject *self, PyObject *args)
 {
        PyObject *v, *w, *z = Py_None;
 
-       if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
+       if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
                return NULL;
        return PyNumber_Power(v, w, z);
 }
@@ -1296,7 +1296,7 @@ builtin_raw_input(PyObject *self, PyObject *args)
        PyObject *fin = PySys_GetObject("stdin");
        PyObject *fout = PySys_GetObject("stdout");
 
-       if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
+       if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
                return NULL;
 
        if (fin == NULL) {
@@ -1377,7 +1377,7 @@ builtin_reduce(PyObject *self, PyObject *args)
 {
        PyObject *seq, *func, *result = NULL, *it;
 
-       if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
+       if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
                return NULL;
        if (result != NULL)
                Py_INCREF(result);
@@ -1513,7 +1513,7 @@ builtin_vars(PyObject *self, PyObject *args)
        PyObject *v = NULL;
        PyObject *d;
 
-       if (!PyArg_ParseTuple(args, "|O:vars", &v))
+       if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
                return NULL;
        if (v == NULL) {
                d = PyEval_GetLocals();
@@ -1549,7 +1549,7 @@ builtin_isinstance(PyObject *self, PyObject *args)
        PyObject *cls;
        int retval;
 
-       if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
+       if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
                return NULL;
 
        retval = PyObject_IsInstance(inst, cls);
@@ -1574,7 +1574,7 @@ builtin_issubclass(PyObject *self, PyObject *args)
        PyObject *cls;
        int retval;
 
-       if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
+       if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
                return NULL;
 
        retval = PyObject_IsSubclass(derived, cls);