Reputation: 153
The function in "time.py" from cpython is almost just "pass" definition. I guess the implement of its function is in "lib/python2.7/lib-dynload/time.so", so I want to know how cpython interpreter invoke ".so" file? I know ctypes, but I can't find where "time" module in cpython use it.
Upvotes: 2
Views: 68
Reputation: 95519
The short answer is that Python provides an API for C programs (the Python / C API), which allows one to define/instantiate Python objects, including functions, in C code (see extending Python with C). There is a method called Py_InitModule (and variations of that), which make it possible to instantiate a Python module within C code and to register method definitions and set/add fields to it.
In terms of how Python loads "time.so", it probably does so using the dlopen C function, which allows libraries to be opened dynamically at runtime (or, on Windows, the equivalent LoadLibrary). Using this approach, it is possible to retrieve a function with a specific name (e.g. Python can enforce that any ".so" file that implements a Python module, must provide an initialization function with a special signature) and then invoke it, allowing the initialization function to register the various definitions.
Upvotes: 2