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));
}

Maybe somebody can come up with a clever macro instead of this short
inline function?

Christian

_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to