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
Require Python file handers to import helpers via root path #26328
Conversation
58e0127
to
b6faaff
Compare
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.
cc @jgraham and @Hexcles to take a first look and give any early feedback.
I've analysed the results of trigger runs on Chromium Dev for both commit 1 and commit 2, and summarized them here https://docs.google.com/spreadsheets/d/14tocLn8d7y1_CPsKGCyC3HC5KWLmi0_VcMtaksQCn1o/edit?usp=sharing
Overall the results revealed one more location that I had overlooked, which I fixed in commit 3.
Still to do:
- Figuring out whether we should explicitly add _WPT_ROOT to the path here, or if its ok to rely on the fact that it seems to already be there (?!).
- Fixing the tests.
- Documentation updates for the new world.
278f881
to
321c418
Compare
Well, this is fun. Adding an |
I can answer why it works currently. There are two cases, both of which involve the CWD.
|
This isn't true for |
Ah, it's a general pytest feature. (Maybe. If I'm reading this right). https://docs.pytest.org/en/stable/pythonpath.html#prepend-and-append-import-modes-scenarios
|
Ahh sorry for adding to the confusion. You're right, and this also explains why your change breaks it. With the addition of root-level |
So this is, I think, the set of changes needed to make the unit tests pass. (Best reviewed by commit, I think). It's not pretty, but PTAL. There's one failure in Windows that I think is unrelated, but not sure. |
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.
LGTM
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.
Do we have a way to enforce the lack of local imports? We could do something like
old_modules = set(sys.modules.keys())
[...]
for name, module in sys.modules.items():
if name not in old_modules:
path = os.path.dirname(module.__file__)
if path.startswith(self.docroot):
mod_name = os.path.relpath(path, self.docroot).replace(os.path.sep, ".")
if mod_name != name:
raise HTTPError("Illegal import in handler")
although it's not lovely.
I think I would rather lint it than do something nasty to sys.modules again. I think a lint looking at only |
bc59dba
to
b4e2607
Compare
RFC has now merged. I've done another comparison study on Chrome dev with/without this PR: https://docs.google.com/spreadsheets/d/14tocLn8d7y1_CPsKGCyC3HC5KWLmi0_VcMtaksQCn1o/edit#gid=1578637675 Only concerning result is |
For web-platform-tests/rfcs#65
Note this will likely require an RFC of its own if we go ahead with it.