I'm looking through python built in library modules, and for example in socket.py
I see the line:
import _socket
I understand that the socket module acts as a wrapper for _socket
. I want to read through some of the source code files within _socket
to see how certain tasks are accomplished.
Where can I find _socket
or any of these other shared files on a Linux box?
_socket
is a C extension. The socket.py
module wraps this with some additional information that doesn't need the speed boost or access to OS-level C APIs.
If you are versed in C, you can read the socketmodule.c
source code.
There is no one-on-one mapping between the final .so
or .dll
file and the original source file however. You can grep the setup.py
file for the names instead:
exts.append( Extension('_socket', ['socketmodule.c'],
depends = ['socketmodule.h']) )
Take into account however that some modules are built-in, compiled as part of the python
binary; these are all listed in the sys.builtin_module_names
tuple.
You can use the __file__
attribute:
In [11]: _socket.__file__
Out[11]: '/Users/andy/.miniconda3/lib/python3.5/lib-dynload/_socket.cpython-35m-darwin.so'
In python packages you can also use the __path__
attribute (for the directory):
In [12]: yapf.__file__
Out[12]: '/Users/andy/.miniconda3/lib/python3.5/site-packages/yapf/__init__.py'
In [13]: yapf.__path__
Out[13]: ['/Users/andy/.miniconda3/lib/python3.5/site-packages/yapf']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With