On Tue, May 22, 2012 at 12:31 PM, Eric V. Smith
<eric@trueblade.com> wrote:
On 05/22/2012 11:39 AM, Nick Coghlan wrote:
> Oops, I also meant to say that it's probably worth at least issuing
> ImportWarning if a new portion with an __init__.py gets added - it's
> going to block all future dynamic updates of that namespace package.
Right. That's on my list of things to clean up. It actually won't block
updates during this run of Python, though: once a namespace package,
always a namespace package. But if, on another run, that entry is on
sys.path, then yes, it will block all namespace package portions.
This discussion has gotten me thinking: should we expose a pkgutil.declare_namespace() API to allow such an __init__.py to turn itself back into a namespace? (Per our previous discussion on transitioning existing namespace packages.) It wouldn't need to do all the other stuff that the setuptools version does, it would just be a way to transition away from setuptools.
What it would do is:
1. Recursively invoke itself for parent packages
2. Create the module object if it doesn't already exist
3. Set the module __path__ to a _NamespacePath instance.
def declare_namespace(package_name):
parent, dot, tail = package_name.rpartition('.')
attr = '__path__'
if dot:
declare_namespace(parent)
else:
parent, attr = 'sys', 'path'
with importlockcontext:
module = sys.modules.get(package_name)
if module is None:
module = XXX new module here
module.__path__ = _NamespacePath(...stuff involving 'parent' and 'attr')