Skip to content
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

Reactivate the importing test. #136

Merged
merged 5 commits into from
Dec 21, 2020

Conversation

hodgestar
Copy link
Contributor

No description provided.

@@ -112,6 +112,9 @@ def __bootstrap__():
ext_filepath = pkg_resources.resource_filename(__name__, {ext_file!r})
m = load_from_spec(Spec({module_name!r}, ext_filepath))
m.__file__ = ext_filepath
m.__loader__ = __loader__
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'm not 100% sure that copy the loader and spec are the perfect thing to do, but:

  1. These are the loader and spec that were used to import the module, so they're correct in that sense.
  2. There isn't another loader and spec than can be used to import an hpy.universal extension.

import sys
modname = 'mytest'
so_filename = self.compile_module("""
if not hasattr(sys, "executable"):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is is to check whether we are inside app-level tests or not. I should probably factor this out into a small helper function but I'm not particular sure what to call it. is_app_level is one idea, but perhaps that's too PyPy specific?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally, the HPy tests should be independent of the implementation. But practically speaking, being able to do if is_pypy_apptest will probably be very useful.
What about introducing a generic way to check the implementation? E.g., we could have self.implementation which by default is sys.implementation.name (so, 'cpython', 'pypy', etc.), but pypy's test/support.py could override it and set to 'pypy_apptest'. And we could also add a self.is_pypy which does return self.implementation in ('pypy', 'pypy_apptest') maybe?

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 added some more methods that describe the possible capabilities of the test runner that the hpy tests currently care about. I dabbled with making something more generic (e.g. a .capabilities(...) method that returns a set of supported behaviours) but that was actually more code and less clear than the simple methods, so I went back to those.

so_filename = self.compile_module("""
if not hasattr(sys, "executable"):
pytest.skip()
mod = self.make_module("""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make_module now does a proper import, so we don't need to do anything different in this test to import the module normally.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear that we are stumbling across each other feet in this PR and #142, sorry for that :(.

I strongly believe that make_module should NOT do a proper import. Basically, the biggest difference between loading a module in memory and "properly importing" it is that the latter modifies some global state (e.g. sys.modules) plus it has interactions with importlib caches, etc.. And for tests, the least we mess with global state, the better it is.
E.g. for testing the debug mode, we need to load the same module twice (with and without the debug mode support), so if we do "proper imports" we need to un-import the module between tests etc. It's a mess. That's why self.make_module was originally written like that, it even had a comment to make this behavior explicit ;)

See also commit ba33924: I believe that the code written this way is much cleaner and doesn't need all the hacks to sys.path, sys.modules, etc.

That said, we surely need a way to properly import modules of course, but it's probably better/easier to leave it clearly separate from the low-level stuff used by tests. So I propose the following:

  1. hpy.universal.load is a very low-level function which just loads the module in memory, without "importing" it
  2. we write a helper (maybe called hpy.importlib or something like that) which uses hpy.universal.load under the hood
  3. hpy.importlib can have its own tests of course: these can be written by calling self.compile_module + hpy.importlib.do_proper_import instead of relying on self.make_module to do everything
  4. the .py stubs can call hpy.importlib to do whatever is appropriate

What do you think about this solution?

@hodgestar
Copy link
Contributor Author

@antocuni Ready for a next review, I think.

@hodgestar
Copy link
Contributor Author

@antocuni I am happy for us to go back to test_support doing a lower-level import that avoids updating sys.modules, etc, but as a practical matter could we perhaps merge this and then maybe sort things out in your PR since this one is about re-activating the import tests.

Probably we need the current import code in support.py to move to test_import.py and be replaced with the new code in your debug branch.

@antocuni
Copy link
Collaborator

@antocuni I am happy for us to go back to test_support doing a lower-level import that avoids updating sys.modules, etc, but as a practical matter could we perhaps merge this and then maybe sort things out in your PR since this one is about re-activating the import tests.

sounds like a good idea, +1

@hodgestar
Copy link
Contributor Author

Thanks!

@hodgestar hodgestar merged commit 8e20b89 into master Dec 21, 2020
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.

2 participants