Skip to content
This repository has been archived by the owner on Jan 28, 2020. It is now read-only.

Need some help with identical modules imported from two different places #3

Open
pmulgaonkar opened this issue Mar 8, 2017 · 1 comment

Comments

@pmulgaonkar
Copy link

Love this repo. Am trying to work with it, but am seeing behavior in importing files that I cannot explain.
The simplest way to explain it is this: Consider a minimal set of 3 modules, say main, foo, and bar
main:
imports ihook.py
sets the import hook
imports foo
imports bar

foo:
imports bar

Now if the files are all unencrypted, what I see happening (with a print statement in the Finder function), is that the system tries to find foo, then since foo imports bar, it imports bar from foo. The behavior of this is different when the file is encrypted vs. when it is not.

If the files foo and bar are not encrypted, the Finder shows the calls to be:
Find (foo)
Find (bar)
if the files foo and bar are encrypted, the Finder shows the calls to be
Find (foo)
Find(foo.bar)
This lands up loading bar as foo.bar inside foo, which is a different instance of the module than the one that the main program loads when it imports bar.

What am I missing here. Why is the behavior different between loading the modules encrypted or unencrypted?

@pmulgaonkar
Copy link
Author

pmulgaonkar commented Mar 8, 2017

After a bunch of looking at various examples and documentation, I have a partial answer.

In the code above I noticed that I was not setting module.__package__. Somewhere in the import process, that resulted in an entry of foo.__package__ = 'foo' being set in the module definition. This resulted in foo being considered a package and any imports it made being considered imports relative to the package directory.

When running against imports where I was not doing the module setup, I saw that the module.__package__ was set to None by the system. But setting module.__package__ = None in the code above did not work. Something reset it to foo.

The solution that worked was to set module.__package__ = '' (the null string). So the working piece of the code for adding the module is:

module = sys.modules.setdefault(name, imp.new_module(name))
module.__file__ = filename
module.__path__ = [os.path.dirname(os.path.abspath(file.name))]
module.__loader__ = self
module.__package__ = ''
sys.modules[name] = module
exec(src, module.__dict__)

This is working now, and the modules foo and bar get imported only once. Behavior of the encrypted and nonencrypted modules looks similar.

I still do not understand where the module.__package__ gets set if it is not explicitly set to ''.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant