python / cpython

The Python programming language
https://www.python.org
Other
63.38k stars 30.35k forks source link

Importing the main module twice leads to two incompatible instances #71794

Open a87580a0-fa61-4695-a31b-f532ecf7185b opened 8 years ago

a87580a0-fa61-4695-a31b-f532ecf7185b commented 8 years ago
BPO 27607
Nosy @rhettinger, @cameron-simpson, @bitdancer, @vadmium, @Vgr255, @TheLastProject, @Kwpolska
Files
  • issue27607.zip: Example test case
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['interpreter-core', 'type-bug', '3.11'] title = 'Importing the main module twice leads to two incompatible instances' updated_at = user = 'https://github.com/TheLastProject' ``` bugs.python.org fields: ```python activity = actor = 'iritkatriel' assignee = 'none' closed = False closed_date = None closer = None components = ['Interpreter Core'] creation = creator = 'SylvieLorxu' dependencies = [] files = ['43877'] hgrepos = [] issue_num = 27607 keywords = [] message_count = 14.0 messages = ['271164', '271184', '271185', '271188', '271189', '271195', '271196', '271210', '271211', '271217', '271243', '271248', '271253', '271264'] nosy_count = 7.0 nosy_names = ['rhettinger', 'cameron', 'r.david.murray', 'martin.panter', 'abarry', 'SylvieLorxu', 'Kwpolska'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue27607' versions = ['Python 3.11'] ```

    a87580a0-fa61-4695-a31b-f532ecf7185b commented 8 years ago

    If main imports another file, and this file imports an Enum class from main, isinstance will return false for Enum variables set in this file, causing the Enums to never match.

    A proof of concept is added as a zip. I thank Kwpolska for simplifying my initial proof of concept.

    To test this, unzip both files into the same directory and launch main.py with Python 3. I have succesfully been able to reproduce this issue on Python 3.4.3.

    From discussion on IRC with Vgr, PEP-499 may be related to this issue: https://www.python.org/dev/peps/pep-0499/.

    bitdancer commented 8 years ago

    This is a consequence of the way __main__ works. You actually have two separate instances of the Enum class. If there is a fix for this it would be that PEP, so I'm not sure there is any point in keeping this issue open.

    a87580a0-fa61-4695-a31b-f532ecf7185b commented 8 years ago

    For what it's worth, I am not opposed to having this issue closed. As I am new to reporting anything on the Python issue tracker and was uncertain on how relevant the PEP in question was, I figured it would be better to report this anyway just in case.

    bitdancer commented 8 years ago

    Certainly reporting it is fine, if nothing else it gives a record for someone else searching for the same problem. I'm not certain why Raymond left the issue open, so I'm not closing it myself :)

    rhettinger commented 8 years ago

    I left it open so that Ethan would get a chance to see anything related to Enum.

    vadmium commented 8 years ago

    I would expect isintance() to fail like this for any class, not just Enum. I haven’t looked in your zip file, but a workaround may be to import the Enum class from a third module into both main.py and the other file. The third module should only get executed once, thus only defining one copy of the Enum class.

    a87580a0-fa61-4695-a31b-f532ecf7185b commented 8 years ago

    Importing it from a third module indeed works without issues and makes both isinstance() calls return True, yes.

    ethanfurman commented 8 years ago

    Sylvia, if you have any control over the name of your zip files, please use something shorter. ;)

    The files inside are a 'main.py' and a 'module.py' -- notice there is no '__main__.py'. So it can't be executed as 'python3 -m blahblah'.

    I tried renaming 'main.py' to '__main__.py' but then I get either

    ImportError: cannot import name 'Module'

    or

    ImportError: No module named 'module'

    I'm happy to check for strange Enum interactions, but I need a working test case first.

    ethanfurman commented 8 years ago

    Oh, forgot to mention I also tried testing as just 'main.py' from inside the test directory (that's what accounted for one of the two error messages).

    cd293b3e-6d38-412b-8370-a46a9aaee518 commented 8 years ago

    I'm fairly sure enums aren't related to this issue, so I'm unassigning Ethan (but feel free to self-assign back if you so desire :). As I said on IRC (I'm Vgr), I think that importing a module twice is the problem here, and that PEP-499 is the way to properly solve this and related issues. Thanks for opening this issue!

    I added Cameron Simpson (PEP-499's author) to the nosy list, hoping he still wants to work on this (and let us know otherwise). I would personally like to see the PEP accepted and implemented for 3.6.

    92259bf3-4b4c-47c1-a468-08a70c584615 commented 8 years ago

    On 25Jul2016 02:56, Python Bug Reports \report@bugs.python.org\ wrote:

    Emanuel Barry added the comment:

    I'm fairly sure enums aren't related to this issue, so I'm unassigning Ethan (but feel free to self-assign back if you so desire :). As I said on IRC (I'm Vgr), I think that importing a module twice is the problem here, and that PEP-499 is the way to properly solve this and related issues. Thanks for opening this issue!

    I added Cameron Simpson (PEP-499's author) to the nosy list, hoping he still wants to work on this (and let us know otherwise). I would personally like to see the PEP accepted and implemented for 3.6.

    Thank you; I do want to finish off PEP-499 and get it accepted. I have been very remiss in pursuing that.

    What can I do to assist here?

    Cheers, Cameron Simpson \cs@zip.com.au\

    a87580a0-fa61-4695-a31b-f532ecf7185b commented 8 years ago

    My apologies, I can't get that test case to run here either. Here is a slightly older one that does reproduce the issue too.

    I specifically run it with python3 main.py.

    cd293b3e-6d38-412b-8370-a46a9aaee518 commented 8 years ago

    @Cameron - I don't think this issue can be solved without the PEP, so possibly that the best move would be to work on the PEP and submit it for another round on Python-Dev, to get it reviewed. Thanks!

    bitdancer commented 8 years ago

    If you have any sort of implementation of the pep, proving that it does fix this issue would be informative.