Require Python file handers to import helpers via root path#26328
Require Python file handers to import helpers via root path#26328stephenmcgruer merged 3 commits intomasterfrom
Conversation
58e0127 to
b6faaff
Compare
stephenmcgruer
left a comment
There was a problem hiding this comment.
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).
|
|
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.
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.