Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

python - How does ctypes.cdll.LoadLibrary(None) work?

How does ctypes.cdll.LoadLibrary() call work with None passed in as an argument? When I try the code below, it seems that the math library gets loaded automatically:

>>> import ctypes
>>> lib = ctypes.cdll.LoadLibrary(None)
>>> lib.sin
<_FuncPtr object at 0x7f36dd65f430>
>>> lib.exp
<_FuncPtr object at 0x7f36dd65f4f8>
>>> 

How does the math library get loaded without being explicitly specified? Do all shared libraries in the standard library get loaded? There is something happening behind the scenes that I don't understand.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Note: You encountered this on Nix (on Win it isn't reproducible).

Take a look at [SO]: How to check whether a file exists without exceptions? (@CristiFati's answer).
There, in the last part (the Notes section) of item #4, I explained this exact scenario, quoting the following passage from [man7]: DLOPEN(3):

If filename is NULL, then the returned handle is for the main program. When given to dlsym(), this handle causes a search for a symbol in the main program, followed by all shared objects loaded at program startup, and then all shared objects loaded by dlopen() with the flag RTLD_GLOBAL.

which is used when loading libraries, according to [Python 3]: Loading shared libraries:

All these classes can be instantiated by calling them with at least one argument, the pathname of the shared library. If you have an existing handle to an already loaded shared library, it can be passed as the handle named parameter, otherwise the underlying platforms dlopen or LoadLibrary function is used to load the library into the process, and to get a handle to it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...