Gilgamesz Gilgamesz - 1 year ago 246
Python Question

C/C++ module vs python module.

In Python ( CPython) we can import module:

import module
and module can be just
file ( with a python code) or module can be a module written in C/C++ ( be extending python). So, a such module is just compiled object file ( like
on the Unix).

I would like to know how is it executed by the interpreter exactly.

I think that python module is compiled to a bytecode and then it will be interpreted. In the case of C/C++ module functions from a such module are just executed. So, jump to the address and start execution.

Please correct me if I am wrong/ Please say more.

Answer Source

When you import a C extension, python uses the platform's shared library loader to load the library and then, as you say, jumps to a function in the library. But you can't load just any library or jump to any function this way. It only works for libs specifically implemented to support python and to functions that are exported by the library as a python object. The lib must understand python objects and use those objects to communicate.

Alternately, instead of importing, you can use a foreign-function library like ctypes to load the library and convert data to the C view of data to make calls.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download