I have some Python code that uses a library that implements virtual file systems. For the drivers for those virtual file systems to work a bunch of C functions (like readdir(), opendir(), fseek()) need to be overridden/replaced - with the replacements defined in a .so/.cpp file. Usually this could be done by setting LD_PRELOAD to that .so file. However, I need to be able to override these functions at runtime, and ideally revert back to the non-overridden functions once the Python functions that use those overridden functions have executed. Is this possible?
1 Answer
I am guessing you want ctypes. Here is a discussion: https://docs.python.org/2/library/ctypes.html
3 Comments
false_azure
This looks very promising, thank you. I'll mark it as the answer once I figure out how to make the functions in the os module in Python use the library I imported using ctypes!
jim mcnamara
Usually these kinds of things are coded in C or C++. And linked into a shared library.
false_azure
That makes sense, but I need to work with an existing Python code base. I have a .cpp/.so file with all the redefinitions that the Python os functions should use, which is part of a C++ library that has Python wrappers.