Skip to content

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 6 commits into from
Mar 12, 2019
Merged

Add __import__ #652

merged 6 commits into from
Mar 12, 2019

Conversation

palaviv
Copy link
Contributor

@palaviv palaviv commented Mar 10, 2019

I will add the builtin module as well so I can test overriding the __import__.

@codecov-io
Copy link

codecov-io commented Mar 10, 2019

Codecov Report

Merging #652 into master will decrease coverage by 0.18%.
The diff coverage is 37.03%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
vm/src/import.rs 34.21% <ø> (-9%) ⬇️
wasm/lib/src/browser_module.rs 0% <0%> (ø) ⬆️
wasm/lib/src/convert.rs 0% <0%> (ø) ⬆️
vm/src/builtins.rs 37.35% <33.33%> (-0.25%) ⬇️
vm/src/frame.rs 46.04% <44.44%> (-1.72%) ⬇️
vm/src/vm.rs 53.51% <83.33%> (+0.05%) ⬆️
vm/src/obj/objobject.rs 39.72% <0%> (-4.93%) ⬇️
vm/src/obj/objtype.rs 39.93% <0%> (-1%) ⬇️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3d306fc...a3823b4. Read the comment docs.

@@ -63,28 +63,6 @@ pub fn import_module(
Ok(module)
}

pub fn import(
Copy link
Contributor

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)?;
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@palaviv
Copy link
Contributor Author

palaviv commented Mar 11, 2019

I will add the builtin module as well so I can test overriding the __import__.

I actually went to implement builtins and found it exists so I added an override test.

@coolreader18
Copy link
Member

I'd return an ImportError instead of panicking, as CPython does:

import builtins
del builtins.__import__
import a
# ImportError: __import__ not found

@palaviv
Copy link
Contributor Author

palaviv commented Mar 12, 2019

I changed to a ImportError but doing

del builtins.__import__

don't seems to delete __import__.

>>>>> 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

@windelbouwman windelbouwman merged commit 048a954 into RustPython:master Mar 12, 2019
@palaviv palaviv mentioned this pull request Mar 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants