codemach¶
Contents:
Small module that executes python code objects.
This module was created to solve the security issues associated with execution of arbitrary code strings. The Machine class can execute python code objects and allow the user to intervene.
Small module that executes python code objects.
Install¶
pip3 install codemach
Test¶
git clone git@github.com:chuck1/codemach
cd codemach
pip3 install -r requirements.txt
pip3 install -e .
pytest
Example¶
from codemach import Machine
m = Machine(verbose=True)
s = """
def func(a, b):
return a + b
func(2, 3)"""
c = compile(s, '<string>', 'exec')
m.exec(c)
Below is the output. Each line shows the opname and the stack after the operation.
------------- begin exec
LOAD_CONST [<class 'code'>]
LOAD_CONST [<class 'code'>, "'func'"]
MAKE_FUNCTION ['<codemach.FunctionType object, function=func>']
STORE_NAME []
LOAD_NAME ['<codemach.FunctionType object, function=func>']
LOAD_CONST ['<codemach.FunctionType object, function=func>', '2']
LOAD_CONST ['<codemach.FunctionType object, function=func>', '2', '3']
------------- begin exec
LOAD_FAST ['2']
LOAD_FAST ['2', '3']
BINARY_ADD ['5']
RETURN_VALUE []
------------- return
CALL_FUNCTION ['5']
POP_TOP []
LOAD_CONST ['None']
RETURN_VALUE []
------------- return