Handle *-nspkg.pth files


Some packages on Python <3.3 create .pth files to emulate namespace packages. These files typically look like:
import sys,types,os; p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('$(NAME)',)); ie = os.path.exists(os.path.join(p,'__init__.py')); m = not ie and sys.modules.setdefault('$(NAME)',types.ModuleType('$(NAME)')); mp = (m or []) and m.__dict__.setdefault('__path__',[]); (p not in mp) and mp.append(p)
The result is that the folder $(NAME) should be treated as if it includes an empty __init__.py file.

Because we don't handle arbitrary code in pth files, we will ignore the packages. We should attempt to detect this pattern and include the path(s) it references.


Zooba wrote Nov 27, 2013 at 6:05 PM

The format of this string comes from setuptools, so it is going to be consistent enough that we can parse it.

Zooba wrote Dec 17, 2014 at 2:50 PM

See also: https://pytools.codeplex.com/workitem/2817

coopr and pyutilib are good examples of namespaces that use this trick extensively and do not work when an empty __init__.py file is added instead.

Also, there is the potential for the code above to appear multiple times in the same .pth file.

Zooba wrote Jan 21 at 8:41 PM

Best detection for these will be the -nspkg in the filename and then a search for all instances of sys\.modules\.setdefault\(['"](\w+)