-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add __import__ #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add __import__ #652
Conversation
Codecov Report
@@ Coverage Diff @@
## master #652 +/- ##
==========================================
- Coverage 40.75% 40.57% -0.19%
==========================================
Files 77 77
Lines 17402 17431 +29
Branches 4528 4519 -9
==========================================
- Hits 7093 7073 -20
- Misses 8409 8459 +50
+ Partials 1900 1899 -1
Continue to review full report at Codecov.
|
@@ -63,28 +63,6 @@ pub fn import_module( | |||
Ok(module) | |||
} | |||
|
|||
pub fn import( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to keep this function. Then call it from both the builtin __import__
function, as well as from the import statement in frame.
}; | ||
|
||
let obj = import(vm, current_path, module, symbol)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to keep the import function which accepts a module as a &str
than to fetch the __import__
function from the builtins and invoke that one. Do we expect the __import__
function to be overridden by python code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole point in me adding this is to override __import__
. You can do for example:
import builtins
>>> def f(name, globals=None, locals=None, fromlist=(), level=0):
... print(name)
...
>>> builtins.__import__ = f
>>> import a
a
>>> import requests
requests
You can read about it here.
I will open an issue with my plan and maybe we can do it in some other way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened #659 that I want the import for. The point is to override the function on WASM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, is this possible, I did not know this. In that case, good change! Is this a good thing of python?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One example use is for packaged applications to be able to import
stuff from other packaged files.
I think there are now more layers of higher and lower level than just __import__
in importlib
, but this will probably be enough for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, is this possible, I did not know this. In that case, good change! Is this a good thing of python?
Not sure if this is a good thing but it is probably the easiest way to change import functionality.
I actually went to implement |
I'd return an ImportError instead of panicking, as CPython does: import builtins
del builtins.__import__
import a
# ImportError: __import__ not found |
I changed to a ImportError but doing
don't seems to delete >>>>> import builtins
>>>>> del builtins.__import__
>>>>> import a
Traceback (most recent call last):
File <stdin>, line 1, in <module>
ModuleNotFoundError: No module named 'a' I will look into it later |
I will add the builtin module as well so I can test overriding the
__import__
.