How to get the Callable of builtin functions

chris liechti cliechti at no.spam.gmx.net
Wed Sep 26 17:32:23 EDT 2001


zptang at hotmail.com (Janet) wrote in 
news:c194faf3.0109261310.5a8f5add at posting.google.com:

> Hi,
> 
> This question is about embedding Python in C.
> Does anyone know how to obtain a callable PyObject of the system
> built-in functions, like "print" and "exec"?

Those are not functions, those are keywords. 

> I tried:
> PyObject *m = PyImport_AddModule(__main__);
> PyObject *func = PyObject_GetAttrString(m, "print");

the builtin functions are in the module "__builtins__" anyway.

> 
> but the 2nd function failed, returning NULL.
> 
> Thanks

print can be substituted by either using printf/puts directly
or a call to pythons sys.stdout.write.

and for exec:

int PyRun_SimpleString (char *command)

or

PyObject* PyRun_String(char *str, int start,
      PyObject *globals,PyObject *locals)

could do the job 



-- 
chris <cliechti at gmx.net>




More information about the Python-list mailing list