Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the _socket file?

Tags:

python

sockets

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?

like image 239
james reeves Avatar asked May 27 '16 04:05

james reeves


2 Answers

_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.

like image 135
Martijn Pieters Avatar answered Oct 20 '22 05:10

Martijn Pieters


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']
like image 45
Andy Hayden Avatar answered Oct 20 '22 05:10

Andy Hayden