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

Michael Foord fuzzyman at voidspace.org.uk
Tue Jul 13 18:25:48 CEST 2010


On 13/07/2010 14:00, Nick Coghlan wrote:
>> (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).
>    

Sure - there are trivial workarounds which is why I don't think there 
are *many* genuine use cases for a module reimporting itself with a 
different name.

Michael

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


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog

READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.




More information about the Python-Dev mailing list