-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add (very debug) selector hijacking API
- Loading branch information
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from contextlib import contextmanager | ||
from .selector import Selector | ||
|
||
|
||
class HijackedSelector(Selector): | ||
def __init__(self, *a, **kw): | ||
raise NotImplementedError | ||
|
||
def eval(self, entities, source): | ||
return self._hijack_.eval(entities, source) | ||
|
||
|
||
def hijack(victim, replace): | ||
if victim.__class__ is not HijackedSelector: | ||
victim._truth_ = victim.__class__ | ||
victim.__class__ = HijackedSelector | ||
victim._hijack_ = replace | ||
|
||
|
||
def unhijack(victim): | ||
try: | ||
victim.__class__ = victim._truth_ | ||
except AttributeError as e: | ||
raise ValueError("not a hijacked selector") from e | ||
|
||
|
||
@contextmanager | ||
def hijacked(victim, replace): | ||
if not isinstance(victim, Selector): | ||
raise TypeError("not a selector: %r" % victim) | ||
prev = victim.__class__ | ||
try: | ||
hijack(victim, replace) | ||
yield | ||
finally: | ||
victim.__class__ = prev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters