sexp_cache
Worker-side cache for rpy2 Sexp objects (internal).
The main process must not hold live rpy2 objects. Instead, the worker replaces R
objects with lightweight SexpWrapper handles and
stores the real Sexp in a local cache keyed by rid.
This module also installs pickle reducers so that any accidental pickling of a Sexp
turns into a wrapper rather than attempting to serialize the R object.
Attributes¶
_SEXP_CACHE = {}
module-attribute
¶
Classes¶
SexpWrapper
dataclass
¶
Lightweight handle for an R object stored in the worker.
The worker keeps the real rpy2 Sexp in an internal cache and replaces it in
results with this wrapper. When passed back to the worker, the wrapper is
resolved to the original Sexp again.
Notes
SexpWrapperinstances are only meaningful within the lifetime of the worker process that produced them. After a worker restart, previously returned wrappers can no longer be reattached.- This type exists to keep the main process free of rpy2 / embedded-R state.
Source code in brmspy/types/session.py
Functions¶
get_sexp(rid)
¶
Fetch a cached Sexp by rid.
Returns NULL when the rid is not present.
_cache_single(obj)
¶
Store obj in the cache and return a lightweight wrapper for IPC.
Source code in brmspy/_session/worker/sexp_cache.py
_extract_sexp(o)
¶
Source code in brmspy/_session/worker/sexp_cache.py
cache_sexp(obj)
¶
Replace any embedded-R objects inside obj with SexpWrapper handles.
Supports:
- plain rpy2.rinterface_lib.sexp.Sexp
- rpy2.robjects wrappers (e.g. vectors/matrices), by extracting the underlying Sexp
- objects with an .r attribute
- list/dict containers (recursively)
This keeps the main process free of rpy2/embedded-R objects.
Source code in brmspy/_session/worker/sexp_cache.py
reattach_sexp(obj)
¶
Replace any SexpWrapper handles inside obj with the cached Sexp.
If a wrapper cannot be resolved (rid not in cache), the wrapper is replaced
with None.
Source code in brmspy/_session/worker/sexp_cache.py
_reduce_sexp(obj)
¶
Pickle reducer for Sexp (worker-side).
Converts the Sexp into a cached SexpWrapper so
the main process never receives a live rpy2 object.
Source code in brmspy/_session/worker/sexp_cache.py
_reduce_sexpwrapper(obj)
¶
Pickle reducer for SexpWrapper (worker-side).
On unpickle, attempts to resolve back to a cached Sexp via get_sexp().
Source code in brmspy/_session/worker/sexp_cache.py
register_global_pickle_overrides()
¶
Register global pickle reducers for Sexp and SexpWrapper.