diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2011-10-09 08:38:36 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2011-10-09 08:38:36 (GMT) |
commit | afe55bba33a20f87a58f940186359237064b428f (patch) | |
tree | 66d64a1518d79c3d0e90c0a1d0080cd88e887d99 /Objects/abstract.c | |
parent | 67df285a3389c7fdb8c7bd301314ac45e17f8074 (diff) | |
download | cpython-afe55bba33a20f87a58f940186359237064b428f.zip cpython-afe55bba33a20f87a58f940186359237064b428f.tar.gz cpython-afe55bba33a20f87a58f940186359237064b428f.tar.bz2 |
Add API for static strings, primarily good for identifiers.
Thanks to Konrad Schöbel and Jasper Schulz for helping with the mass-editing.
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r-- | Objects/abstract.c | 111 |
1 files changed, 75 insertions, 36 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 9105769..57424a6 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -2082,10 +2082,11 @@ PyMapping_Keys(PyObject *o) { PyObject *keys; PyObject *fast; + _Py_identifier(keys); if (PyDict_CheckExact(o)) return PyDict_Keys(o); - keys = PyObject_CallMethod(o, "keys", NULL); + keys = _PyObject_CallMethodId(o, &PyId_keys, NULL); if (keys == NULL) return NULL; fast = PySequence_Fast(keys, "o.keys() are not iterable"); @@ -2098,10 +2099,11 @@ PyMapping_Items(PyObject *o) { PyObject *items; PyObject *fast; + _Py_identifier(items); if (PyDict_CheckExact(o)) return PyDict_Items(o); - items = PyObject_CallMethod(o, "items", NULL); + items = _PyObject_CallMethodId(o, &PyId_items, NULL); if (items == NULL) return NULL; fast = PySequence_Fast(items, "o.items() are not iterable"); @@ -2114,10 +2116,11 @@ PyMapping_Values(PyObject *o) { PyObject *values; PyObject *fast; + _Py_identifier(values); if (PyDict_CheckExact(o)) return PyDict_Values(o); - values = PyObject_CallMethod(o, "values", NULL); + values = _PyObject_CallMethodId(o, &PyId_values, NULL); if (values == NULL) return NULL; fast = PySequence_Fast(values, "o.values() are not iterable"); @@ -2223,22 +2226,11 @@ _PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...) return call_function_tail(callable, args); } -PyObject * -PyObject_CallMethod(PyObject *o, char *name, char *format, ...) +static PyObject* +callmethod(PyObject* func, char *format, va_list va, int is_size_t) { - va_list va; - PyObject *args; - PyObject *func = NULL; PyObject *retval = NULL; - - if (o == NULL || name == NULL) - return null_error(); - - func = PyObject_GetAttrString(o, name); - if (func == NULL) { - PyErr_SetString(PyExc_AttributeError, name); - return 0; - } + PyObject *args; if (!PyCallable_Check(func)) { type_error("attribute of type '%.200s' is not callable", func); @@ -2246,9 +2238,10 @@ PyObject_CallMethod(PyObject *o, char *name, char *format, ...) } if (format && *format) { - va_start(va, format); - args = Py_VaBuildValue(format, va); - va_end(va); + if (is_size_t) + args = _Py_VaBuildValue_SizeT(format, va); + else + args = Py_VaBuildValue(format, va); } else args = PyTuple_New(0); @@ -2263,10 +2256,9 @@ PyObject_CallMethod(PyObject *o, char *name, char *format, ...) } PyObject * -_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...) +PyObject_CallMethod(PyObject *o, char *name, char *format, ...) { va_list va; - PyObject *args; PyObject *func = NULL; PyObject *retval = NULL; @@ -2279,28 +2271,75 @@ _PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...) return 0; } - if (!PyCallable_Check(func)) { - type_error("attribute of type '%.200s' is not callable", func); - goto exit; - } + va_start(va, format); + retval = callmethod(func, format, va, 0); + va_end(va); + return retval; +} - if (format && *format) { - va_start(va, format); - args = _Py_VaBuildValue_SizeT(format, va); - va_end(va); +PyObject * +_PyObject_CallMethodId(PyObject *o, _Py_Identifier *name, char *format, ...) +{ + va_list va; + PyObject *func = NULL; + PyObject *retval = NULL; + + if (o == NULL || name == NULL) + return null_error(); + + func = _PyObject_GetAttrId(o, name); + if (func == NULL) { + PyErr_SetString(PyExc_AttributeError, name->string); + return 0; } - else - args = PyTuple_New(0); - retval = call_function_tail(func, args); + va_start(va, format); + retval = callmethod(func, format, va, 0); + va_end(va); + return retval; +} - exit: - /* args gets consumed in call_function_tail */ - Py_XDECREF(func); +PyObject * +_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...) +{ + va_list va; + PyObject *func = NULL; + PyObject *retval; + + if (o == NULL || name == NULL) + return null_error(); + func = PyObject_GetAttrString(o, name); + if (func == NULL) { + PyErr_SetString(PyExc_AttributeError, name); + return 0; + } + va_start(va, format); + retval = callmethod(func, format, va, 1); + va_end(va); return retval; } +PyObject * +_PyObject_CallMethodId_SizeT(PyObject *o, _Py_Identifier *name, char *format, ...) +{ + va_list va; + PyObject *func = NULL; + PyObject *retval; + + if (o == NULL || name == NULL) + return null_error(); + + func = _PyObject_GetAttrId(o, name); + if (func == NULL) { + PyErr_SetString(PyExc_AttributeError, name->string); + return NULL; + } + va_start(va, format); + retval = callmethod(func, format, va, 1); + va_end(va); + return retval; +} static PyObject * objargs_mktuple(va_list va) |