[Python-Dev] avoiding accidental shadowing of top-level libraries by the main module

Nick Coghlan ncoghlan at gmail.com
Tue Jul 13 15:00:53 CEST 2010


> (Two different classes with the same name created - one from __main__ and
> one from real_name.) Use cases for *genuinely* reimporting the same module
> with different names (as different module objects rather than aliases) are
> relatively rare, and the problem of modules *accidentally* reimporting
> themselves not that rare.

That particular issue can be resolved by automatically doing the
shadowing at the sys.module level though (i.e. insert into sys.modules
under the real module name as well as __main__ for
runpy._run_module_as_main and just strip the directory and extension
details from __file__ to determine where to insert the second
reference for a directly executed script file).

Making sure both __main__ and the corresponding importable name refers
to the same module object seems reasonable. Trying to special case
shadowing detection just because the shadowing module happens to also
be the main module seems ugly as hell :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list