pythonlib
Classes, exceptions, and methods to run Python code in a remote process. Python >=3.6 supported
- class pyjectify.windows.modules.pythonlib.PythonLib(process: ProcessHandle, python_mod: PE | None = None)[source]
This class provides methods to run Python inside a target process
- python_mod: int
Handle to the Python library loaded in the target process
- setprogramname(programname: str) None [source]
Set the value of the argv[0] argument to the main() function
- Parameters:
programname – the new programname
- setpath(pythonpath: str) None [source]
Set the default module search path
- Parameters:
pythonpath – the PYTHON_PATH value
- setpythonhome(pythonhome: str) None [source]
Set the default “home” directory This method calls Py_SetPythonHome
- Parameters:
pythonhome – the PYTHON_HOME value
- isinitialized() bool [source]
Check if the Python interpreter is initialized in the target process This method calls Py_IsInitialized
- Returns:
A boolean specifying if the Python interpreter is initialized
- initialize(initsigs: int = 0) None [source]
Initialize the Python interpreter in the target process and release the GIL This method calls Py_IsInitialized + Py_InitializeEx + PyEval_SaveThread
- Parameters:
initsigs – initsigs for Py_InitializeEx
- exec(py_code: str) None [source]
Execute python code in the target process (acquire and then release the GIL). Python interpreter MUST be initialized. This method calls PyEval_RestoreThread + PyRun_SimpleString + PyEval_SaveThread
- Parameters:
py_code – the python code to run
- finalize() None [source]
Undo all initializations of the Python interpreter in the target process This method calls PyEval_RestoreThread + Py_FinalizeEx
- prepare_hook(func: str, ofunc_addr: int = 0) int [source]
Utility for using a python function as a hook.
This method returns the address of the function and optionally creates the global function o_{func} which points to the original function.
- Parameters:
func – the name of the Python function to get the address
ofunc_addr – address of the original function
- Returns:
The address of the Python function. If the method succeeds, this address is nonzero.