New submission from George <geo...@cerebralab.com>:

Warning: There's a higher probability this is "expected" undefined behaviour or 
not even undefined and I'm just a moron. In addtion, I couldn't actually 
replicate it outside of the specific context it happened. But if it sounds 
plausible and it's something that shouldn't happen I can spend more time trying 
to replicate.

1. In two different python processes I'm "dynamically" creating a module named 
`M` using a file `m1.py` that contains a class `C`. Then I create an object of 
tpye `C` and pickle it. (let's call this object `c1`)
2. In a different thread I do the exact same thing, but the file is `m2.py` 
then I create an object of type `C` and pickle it. (call this one `c2`)
3. Then, in the same thread, I recreate the module named `M` from `m1.py` and 
unpickle `c1`, second I create a module named `M` from `m2.py` (this doesn't 
cause an error) and unpickle `c2`.
4. This (spurprisingly?) seems to basically work fine in most cases. Except for 
one (and I can't find why it's special) where for some reason `c2` starts 
calling the methods from a class that's not it's own. In other words `c1` 
usually maps ot `M.C --> m1.py` and `c2` to `M.C --> m2.py` | But randomly `c2` 
will start looking up methods in `M.C --> m1.py`, or at least that's what stack 
traces & debuggers seem to indicate.

The way I create the module `M` in all cases:

```
with open(`m1.py`, 'wb') as fp:
        fp.write(code.encode('utf-8'))
        spec = importlib.util.spec_from_file_location('M', fp.name)
        temp_module = importlib.util.module_from_spec(spec)
        sys.modules['M] = temp_module
        spec.loader.exec_module(temp_module)

# Note: Same for the other module but using `m2.py`, the code I use here 
contains a class `C` in both cases
```

This seems, unexpected. I wouldn't expect the recreation to cause a crash, but 
I'd expect it to either override the previous `M` for all existing objects 
instantiated from that module in all cases, or in no cases... currently it 
seems that both modules stay loaded and lookups are made randomly.

----------
components: Interpreter Core
messages: 399596
nosy: George3d6
priority: normal
severity: normal
status: open
title: Undefined/random behaviour when importing two modules with the same name 
but different source files
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44916>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to