[Python-Dev] unexpected import behaviour

Michael Foord fuzzyman at voidspace.org.uk
Sat Jul 31 17:14:07 CEST 2010


On 31/07/2010 16:07, Nick Coghlan wrote:
> On Sat, Jul 31, 2010 at 3:57 PM, Daniel Waterworth
> <da.waterworth at gmail.com>  wrote:
>    
>> @Nick: I suppose the simplest way to detect re-importation in the
>> general case, is to store a set of hashes of files that have been
>> imported. When a user tries to import a file where it's hash is
>> already in the set, a warning is generated. It's simpler than trying
>> to figure out all the different ways that a file can be imported, and
>> will also detect copied files. This is less infrastructure than you
>> were suggesting, but it's not a perfect solution.
>>      
> Hashing every file on import would definitely be more overhead than
> just checking __file__ values (since we already calculate the latter,
> and regardless of how a file is imported, it needs to end up in
> sys.modules eventually). Besides, importing the same code under
> different names happens in several places in our own test suite (we
> use it to check that code behaviour doesn't change just because we
> import it differently), so we can hardly disable that behaviour.
>
> That said, I really don't think catching such a rare error is worth
> *any* runtime overhead. Just making "__main__" and the real module
> name refer to the same object in sys.modules is a different matter,
> but I'm not confident enough that I fully grasp the implications to do
> it without gathering feedback from a wider audience.
>
>    

Some people workaround the potential for bugs caused by __main__ 
reimporting itself by doing it *deliberately*. Glyf even recommends it 
as good practise. ;-)

http://glyf.livejournal.com/60326.html

So - the fix you suggest would *break* this code. Raising a warning 
wouldn't... (and would eventually make this workaround unnecessary.)

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