def _register_external_symbols() -> None: python_cchar = ffi.new('char []', b'.Python') ffi_proxy = openrlib.ffi_proxy if (ffi_proxy.get_ffi_mode( openrlib._rinterface_cffi) == ffi_proxy.InterfaceType.ABI): python_callback = ffi.cast('DL_FUNC', _evaluate_in_r) else: python_callback = ffi.cast('DL_FUNC', openrlib.rlib._evaluate_in_r) externalmethods = ffi.new( 'R_ExternalMethodDef[]', [[python_cchar, python_callback, -1], [ffi.NULL, ffi.NULL, 0]]) openrlib.rlib.R_registerRoutines(openrlib.rlib.R_getEmbeddingDllInfo(), ffi.NULL, ffi.NULL, ffi.NULL, externalmethods)
def _dlopen_rlib(r_home: typing.Optional[str]): """Open R's shared C library. This is only relevant in ABI mode.""" if r_home is None: raise ValueError('r_home is None. ' 'Try python -m rpy2.situation') lib_path = rpy2.situation.get_rlib_path(r_home, platform.system()) if lib_path is None: raise ValueError('The library path cannot be None.') else: rlib = ffi.dlopen(lib_path) return rlib if ffi_proxy.get_ffi_mode(_rinterface_cffi) == ffi_proxy.InterfaceType.API: rlib = _rinterface_cffi.lib else: rlib = _dlopen_rlib(R_HOME) # R macros and functions def _get_symbol_or_fallback(symbol: str, fallback: typing.Any): """Get a cffi object from rlib, or the fallback if missing.""" try: res = getattr(rlib, symbol) except (ffi.error, AttributeError): res = fallback return res
from rpy2.rinterface_lib import embedded from rpy2.rinterface_lib import memorymanagement from _cffi_backend import FFI # type: ignore logger = logging.getLogger(__name__) ffi = openrlib.ffi # TODO: How can I reliably get MAX_INT from limits.h ? _MAX_INT = 2**32 - 1 _R_PRESERVED = dict() # type: typing.Dict[int, int] _PY_PASSENGER = dict() FFI_MODE = ffi_proxy.get_ffi_mode(openrlib._rinterface_cffi) def get_rid(cdata: FFI.CData) -> int: """Get the identifier for the R object. This is intended to be like Python's `id()`, but for R objects mapped by rpy2.""" return int(ffi.cast('uintptr_t', cdata)) def protected_rids() -> Tuple[Tuple[int, int], ...]: """Sequence of R IDs protected from collection by rpy2.""" keys = tuple(_R_PRESERVED.keys()) res = [] for k in keys: