[Python-3000] PyObject_AsString()

Christian Heimes lists at cheimes.de
Thu Nov 22 07:49:42 CET 2007

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?


More information about the Python-3000 mailing list