Is the itertools C module included somehow in the main Python binary in 3.x?
Assuming that the C module is built and included, which it appears to be:
>>> import inspect
>>> import itertools
>>>
>>> inspect.getsourcefile(itertools)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 571, in getsourcefile
filename = getfile(object)
File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 518, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'itertools' (built-in)> is a built-in module
I can't find an itertools.so
for Python 3.x on my system, but there's one for 2.7.
I noted that some other C modules exist as shared objects (locate '.so' | grep -E '^/usr/local/' | grep '.so'
e.g. mmap.so
) on disk, so what's the deal with itertools
? How can I use it if there's not a shared library?
In Python 3, the
itertools
extension is compiled into the main Python binary:See the
sys.builtin_module_names
documentation:The module was added to the Python binary because it is used very widely in the Python standard library.
The list of modules to include is taken from the
Modules/Setup.dist
file in the Python distribution;itertools
was added together with_collections
, as it is a transient dependency of that module. See issue #9545.There are hints in the makefile that's near the Python wrapper of
inspect.py
:/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/config-3.4m/Makefile
We can see the build rules for the itertools.c source:
And then trace it a little to see that it's being bundled in:
Or if made via
distutils
, the path will be something like:/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_sysconfigdata.py
And assuming that this gets built into a dynamic library:
Which means that at build time, the itertools.c module gets included in the
libpython
dynamic library, meaning that it's now part of the standard library.