-
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
Merged
Merged
Add __import__ #652
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
87e6d8d
Add __import__
palaviv 8c92636
Fix wasm code
palaviv a3823b4
Test override __import__
palaviv e6c460f
Add import method to VirtualMachine
palaviv 2c6baac
Return ImportError __import__ is not in builtins
palaviv 6a3e82e
Fix typo
palaviv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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 |
||
vm: &mut VirtualMachine, | ||
current_path: PathBuf, | ||
module_name: &str, | ||
symbol: &Option<String>, | ||
) -> PyResult { | ||
let module = import_module(vm, current_path, module_name)?; | ||
// If we're importing a symbol, look it up and use it, otherwise construct a module and return | ||
// that | ||
if let Some(symbol) = symbol { | ||
module.get_attr(symbol).map_or_else( | ||
|| { | ||
let import_error = vm.context().exceptions.import_error.clone(); | ||
Err(vm.new_exception(import_error, format!("cannot import name '{}'", symbol))) | ||
}, | ||
Ok, | ||
) | ||
} else { | ||
Ok(module) | ||
} | ||
} | ||
|
||
fn find_source(vm: &VirtualMachine, current_path: PathBuf, name: &str) -> Result<PathBuf, String> { | ||
let sys_path = vm.sys_module.get_attr("path").unwrap(); | ||
let mut paths: Vec<PathBuf> = objsequence::get_elements(&sys_path) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: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__
inimportlib
, 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.
Not sure if this is a good thing but it is probably the easiest way to change import functionality.