How to list the global functions from a C program

Jack Diederich jack at performancedrivers.com
Fri Jan 14 11:07:34 EST 2005


On Fri, Jan 14, 2005 at 04:01:13PM +0100, Francesco Montorsi wrote:
<snip>
> PyObject *list = PyObject_Dir(m_pGlobals);
> if (!list || PyList_Check(list) == FALSE)
>  return;
> 
> for (int i=0,max=PyList_Size(list); i<max; i++) {
> 
>      PyObject *elem = PyList_GetItem(list, i);
>      if (PyCallable_Check(elem) != 0) {
> 
>           /* this should be a function..  */
>            /* HERE IS THE PROBLEM: this code is never reached */
>           PyObject *str = PyObject_GetAttrString(elem, "func_name");
>      }
> }
> ==============================================
> 
> Everything seems to work but then when scanning the list returned
> by PyObject_Dir() I never find any callable object....
> what am I doing wrong ?

You are checking the list of strings returned from the dir() to
see if any of them are callable (they aren't).  You mean to check
the thing that the string is a name for, so instead of

# callable(name)
PyCallable_Check(elem)

use

# callable(globals()[name])
PyCallable_Check(PyDict_GetItem(m_pGlobals, elem))

-Jack




More information about the Python-list mailing list