Esempio n. 1
0
def _preserve(cdata):
    addr = int(ffi.cast('uintptr_t', cdata))
    count = _R_PRESERVED.get(addr, 0)
    if count == 0:
        openrlib.rlib.R_PreserveObject(cdata)
    _R_PRESERVED[addr] = count + 1
    return addr
Esempio n. 2
0
def _initr_win32(interactive: bool = True) -> int:
    embedded.rstart = ffi.new('Rstart')
    rstart = embedded.rstart
    rstart.rhome = openrlib.rlib.get_R_HOME()
    rstart.home = openrlib.rlib.getRUser()
    rstart.CharacterMode = openrlib.rlib.LinkDLL
    rstart.ReadConsole = callbacks._consoleread
    rstart.WriteConsole = callbacks._consolewrite_ex
    rstart.CallBack = callbacks._callback
    rstart.ShowMessage = callbacks._showmessage
    rstart.YesNoCancel = callbacks._yesnocancel
    rstart.Busy = callbacks._busy

    rstart.R_Quiet = True
    rstart.R_Interactive = interactive
    rstart.RestoreAction = openrlib.rlib.SA_RESTORE
    rstart.SaveAction = openrlib.rlib.SA_NOSAVE

    embedded.setinitialized()

    # TODO: still needed ?
    openrlib.rlib.R_CStackLimit = ffi.cast('uintptr_t', -1)

    openrlib.rlib.setup_Rmainloop()
    return 1
Esempio n. 3
0
def _release(cdata):
    addr = int(ffi.cast('uintptr_t', cdata))
    count = _R_PRESERVED[addr] - 1
    if count == 0:
        del(_R_PRESERVED[addr])
        openrlib.rlib.R_ReleaseObject(cdata)
    else:
        _R_PRESERVED[addr] = count
Esempio n. 4
0
def _initr(interactive: bool = True) -> int:

    rlib = openrlib.rlib
    with openrlib.rlock:
        if isinitialized():
            return
        os.environ['R_HOME'] = openrlib.R_HOME
        options_c = [ffi.new('char[]', o.encode('ASCII')) for o in _options]
        n_options = len(options_c)
        status = rlib.Rf_initialize_R(ffi.cast('int', n_options),
                                      options_c)

        global rstart
        rstart = ffi.new('Rstart')
        rstart.R_Interactive = interactive

        # TODO: Conditional definition in C code
        #   (Aqua, TERM, and TERM not "dumb")
        rlib.R_Outputfile = ffi.NULL
        rlib.R_Consolefile = ffi.NULL
        rlib.ptr_R_WriteConsoleEx = callbacks._consolewrite_ex
        rlib.ptr_R_WriteConsole = ffi.NULL

        # TODO: Conditional in C code
        rlib.R_SignalHandlers = 0

        rlib.ptr_R_ShowMessage = callbacks._showmessage
        rlib.ptr_R_ReadConsole = callbacks._consoleread
        rlib.ptr_R_FlushConsole = callbacks._consoleflush
        rlib.ptr_R_ResetConsole = callbacks._consolereset

        rlib.ptr_R_ChooseFile = callbacks._choosefile
        rlib.ptr_R_ShowFiles = callbacks._showfiles

        rlib.ptr_R_CleanUp = callbacks._cleanup
        rlib.ptr_R_ProcessEvents = callbacks._processevents
        rlib.ptr_R_Busy = callbacks._busy

        setinitialized()

        # TODO: still needed ?
        rlib.R_CStackLimit = ffi.cast('uintptr_t', -1)

        rlib.setup_Rmainloop()
        return status
Esempio n. 5
0
def _register_external_symbols() -> None:
    python_cchar = ffi.new('char []', b'.Python')
    externalmethods = ffi.new(
        'R_ExternalMethodDef[]',
        [[python_cchar, ffi.cast('DL_FUNC', _evaluate_in_r), -1],
         [ffi.NULL, ffi.NULL, 0]])
    openrlib.rlib.R_registerRoutines(openrlib.rlib.R_getEmbeddingDllInfo(),
                                     ffi.NULL, ffi.NULL, ffi.NULL,
                                     externalmethods)
Esempio n. 6
0
def _COMPLEX(robj):
    return ffi.cast('Rcomplex *', DATAPTR(robj))
Esempio n. 7
0
def _REAL(robj):
    return ffi.cast('double *', DATAPTR(robj))
Esempio n. 8
0
def _RAW(x):
    return ffi.cast('Rbyte *', DATAPTR(x))
Esempio n. 9
0
def _INTEGER(x):
    return ffi.cast('int *', DATAPTR(x))
Esempio n. 10
0
def _LOGICAL(x):
    return ffi.cast('int *', DATAPTR(x))
Esempio n. 11
0
def _VECTOR_PTR(robj):
    return ffi.cast('SEXP *', DATAPTR(robj))
Esempio n. 12
0
def _STRING_PTR(robj):
    return ffi.cast('SEXP *', DATAPTR(robj))
Esempio n. 13
0
def _VECTOR_ELT(robj, i):
    return ffi.cast('SEXP *', DATAPTR(robj))[i]
Esempio n. 14
0
def get_rid(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))