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.
I looked into the issue you told me about the other day, that
one_line_regex.match_magic()
doesn't work when called from within a function, but only when called from the top-level scope.That is because accessing a variable that hasn't been defined results into a
LOAD_GLOBAL
instruction. But you assign to the caller's local scope. However, if the function is called from the top-level scopef_globals == f_locals
. That is the reason it still worked in that scenario. So the fix is as simple as assigning to the callers global scope instead, and voilamagic_match()
works within function calls as well.Of course this bears side effects like polluting the global scope, but I'm confident this is the only way to make it work (without a decorator rewriting the byte code). Also there is still a corner case left that seems to be impossible to tackle (without rewriting the byte code):
In that case
FAST_LOAD
is used to lookup the variablematch
and globals are ignored. However, assigning to locals wouldn't help either, because the value of the variable is looked up from the stack.The other changes in this PR are just minor code cleanups (isolated in separate commits) to allow importing this file actually as module and passing additional parameters such as flags.