How to get the Callable of builtin functions 
Author Message
 How to get the Callable of builtin functions

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"?
I tried:
PyObject *m = PyImport_AddModule(__main__);
PyObject *func = PyObject_GetAttrString(m, "print");

but the 2nd function failed, returning NULL.

Thanks



Mon, 15 Mar 2004 05:10:54 GMT  
 How to get the Callable of builtin functions


Quote:
> 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.

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

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

Quote:

> 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>



Mon, 15 Mar 2004 05:32:23 GMT  
 How to get the Callable of builtin functions

Quote:
> 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"?
> I tried:
> PyObject *m = PyImport_AddModule(__main__);
> PyObject *func = PyObject_GetAttrString(m, "print");

"print" and "exec" are not functions, they are operators. There are C
API functions to use those operators e.g. PyObject_Print.

Cheers,
Brian



Mon, 15 Mar 2004 05:24:28 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. callable() builtin-function

2. Builtin dict should be callable, since a dict defines a function

3. Builtin methods versus Builtin Functions -- help!

4. Builtin dict should be callable, since a dict defines a funct ion

5. Pickler that handles modules, functions, and builtin functions -- done

6. generic functions to redefine builtin functions - how ?

7. getting number of arguments of builtin/C func

8. Callable-iterator is not callable ;-)

9. DECIMAL Builtin Function

10. Regina vs IBM REXX: builtin functions

11. C callable assembly functions w/ Visual C/C++

12. builtin functions

 

 
Powered by phpBB® Forum Software