pythonlib
Classes, exceptions, and methods to run Python code in a remote process. PyJectify supports instrumenting Python >=3.6.
- 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.
Initialization: bind the module to a specific process
- Parameters:
process – ProcessHandle targeted by the module
python_mod – handle to the Python library loaded in the target process (optional, can be set later)
- set_program_name(name: str) None [source]
Set the value of the argv[0] argument to the main() function
- Parameters:
name – the new program name
- set_path(path: str) None [source]
Set the default module search path
- Parameters:
path – the PYTHON_PATH value
- set_python_home(home: str) None [source]
Set the default “home” directory
This method calls Py_SetPythonHome
- Parameters:
home – the PYTHON_HOME value
- is_initialized() 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]
Run Python code in the target process (acquire and then release the GIL)
The Python interpreter must have been initialized by PyJectify before calling this method
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
The Python interpreter must have been initialized by PyJectify before calling this method
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
The Python interpreter must have been initialized by PyJectify before calling this method
- 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.