2

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.

asked Jul 19, 2015 at 2:31

1 Answer 1

2

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.

answered Jul 19, 2015 at 2:52
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.