Hijack imports the hacky way
Go to file
GodSaveTheDoge 90e3d2335b First commit
Made a first proof of concept with the ability to override things
2021-08-28 00:18:06 +02:00
README.md First commit 2021-08-28 00:18:06 +02:00
hijack.py First commit 2021-08-28 00:18:06 +02:00

README.md

This allows to override things.

Some examples:

$ cat target.py
import sys

print(sys.executable)
$ python target.py
/usr/bin/python
$ cat patch.py
from hijack import BinLaden

patcher = BinLaden()
patcher.override("sys.executable", "nope")
patcher.run("target.py")
$ python patch.py
nope
$

This could be useful to add some functionality or act as a decorator:

$ cat target.py
import requests

try:
    r = requests.get("fsf.org")
    print(r.status_code, r.reason)
except Exception as e:
    print(e)
$ python target.py
Invalid URL 'fsf.org': No schema supplied. Perhaps you meant http://fsf.org?
$ cat patch.py
from hijack import BinLaden

patcher = BinLaden()
old_get = patcher.import_("requests").get

@patcher.override("requests.get")
def get(url, *a, **kw):
    if not url.startswith("http"):
        url = "https://" + url
    return old_get(url, *a, **kw)

patcher.run("target.py")
$ python patch.py
200 OK
$