On 30 January 2016 at 21:19, Ned Batchelder
On 1/30/16 4:30 AM, Nick Coghlan wrote:
We could potentially detect when __main__ is being reimported under a different name and issue a user visible warning when it happens, but we can't readily detect a file importing itself in the general case (since it may be an indirect circular reference rather than a direct).
I thought about the indirect case, and for the errors I'm trying to make clearer, the direct case is plenty.
In that case, the only problem I see off the top of my head with emitting a warning for direct self-imports is that it would rely on import system behaviour we're currently trying to reduce/minimise: the import machinery needing visibility into the globals for the module initiating the import. It's also possible that by the time we get hold of the __spec__ for the module being imported, we've already dropped our reference to the importing module's globals, so we can't check against __file__ any more. However, I'd need to go read the code to remember how quickly we get to extracting just the globals of potential interest.
While we're at it though, re-importing __main__ is a separate kind of behavior that is often a problem, since it means you'll have the same classes defined twice.
Right, but it combines with the name shadowing behaviour to create a *super* confusing combination when you write a *script* that shadows the name of a standard library module: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_t... Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia