[Python-3000] PyObject_AsString()

Amaury Forgeot d'Arc amauryfa at gmail.com
Thu Nov 22 11:03:06 CET 2007


Christian Heimes wrote:
> A lot of functions in Python 3.x have blocks like
>
>         if (PyString_Check(sub_obj)) {
>                 sub = PyString_AS_STRING(sub_obj);
>                 sub_len = PyString_GET_SIZE(sub_obj);
>         }
>         else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len))
>                 return NULL;
>
> The functions check for PyString first to gain a little speed up. I like
> to add a new function to the abstract object protocol
> (Objects/abstract.c) to make code more readable while keeping the speed up.
>
> int inline
> PyObject_AsString(PyObject *obj,
>                   const char **buffer,
>                   Py_ssize_t *buffer_len)
> {
>         if (PyString_Check(obj)) {
>                 *buffer = PyString_AS_STRING(obj);
>                 *buffer_len = PyString_GET_SIZE(obj);
>                 return 1;
>         }
>         return (PyObject_AsCharBuffer(obj, buffer, buffer_len));
> }

Could this code replace the current PyString_AsStringAndSize, instead
of creating a new function?

-- 
Amaury Forgeot d'Arc


More information about the Python-3000 mailing list