Last active
January 6, 2024 04:52
-
-
Save barneygale/8ff070659178135b10b5e202a1ecaa3f to your computer and use it in GitHub Desktop.
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
import sys, marshal, functools, subprocess | |
child_script = """ | |
import marshal, sys, types; | |
fn, args, kwargs = marshal.load(sys.stdin) | |
marshal.dump( | |
types.FunctionType(fn, globals())(*args, **kwargs), | |
sys.stdout) | |
""" | |
def sudo(fn): | |
@functools.wraps(fn) | |
def inner(*args, **kwargs): | |
proc_args = [ | |
"sudo", | |
sys.executable, | |
"-c", | |
child_script] | |
proc = subprocess.Popen( | |
proc_args, | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE) | |
send_data = marshal.dumps(( | |
fn.func_code, | |
args, | |
kwargs)) | |
recv_data = proc.communicate(send_data)[0] | |
return marshal.loads(recv_data) | |
return inner | |
@sudo | |
def whoami(): | |
import os | |
return "I am: %d" % os.getuid() | |
if __name__ == '__main__': | |
print whoami() |
Very useful, I took @mohsenBanan code to add support to all kind of objects by using the pickle library.
The only drawback is that you should include the import or definition of what you want inside the function's definition.
#main.py
import sys, marshal, functools, subprocess, pickle
child_script = """
import pickle, sys, types, marshal;
fn, args, kwargs = marshal.loads(sys.stdin.buffer.read())
sys.stdout.buffer.write(
pickle.dumps(
types.FunctionType(fn, globals())(*args, **kwargs)
)
)
"""
def sudo(fn):
@functools.wraps(fn)
def inner(*args, **kwargs):
proc_args = [
"sudo",
sys.executable,
"-c",
child_script]
proc = subprocess.Popen(
proc_args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
send_data = marshal.dumps((
fn.__code__,
args,
kwargs))
recv_data = proc.communicate(send_data)[0]
out = pickle.loads(recv_data)
return out
return inner
@sudo
def func(x):
from transport import Vehicle
return (x, "Hello", Vehicle("green", 2))
if __name__ == "__main__":
print(func(3))
#transport.py
class Vehicle:
def __init__(self, color, wheels):
self.color = color
self.wheels = wheels
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As of 10/23/2021, above code won't run under py3.
But the below code would: