Example #1
0
 def fetch_global_var_addr(self):
     if self.ptr:
         result = self.ptr
     else:
         if not we_are_translated():
             FNPTR = rffi.CCallback([], rffi.VOIDP)
             fetch_addr = rffi.cast(FNPTR, self.fetch_addr)
             result = fetch_addr()
         else:
             # careful in translated versions: we need to call fetch_addr,
             # but in a GIL-releasing way.  The easiest is to invoke a
             # llexternal() helper.
             result = pypy__cffi_fetch_var(self.fetch_addr)
         result = rffi.cast(rffi.CCHARP, result)
     if not result:
         from pypy.module._cffi_backend import ffi_obj
         ffierror = ffi_obj.get_ffi_error(self.space)
         raise oefmt(ffierror, "global variable '%s' is at address NULL",
                     self.name)
     return result
Example #2
0
@specialize.ll()
def free(struct):
    # This is how to free nearly everything. Only some specific things need
    # teardown before freeing.
    lltype.free(struct, flavor="raw")


uv_strerror = rffi.llexternal("uv_strerror", [rffi.INT],
                              rffi.CCHARP,
                              compilation_info=eci)
uv_err_name = rffi.llexternal("uv_err_name", [rffi.INT],
                              rffi.CCHARP,
                              compilation_info=eci)

walk_cb = rffi.CCallback([handle_tp, rffi.VOIDP], lltype.Void)

loop_init = rffi.llexternal("uv_loop_init", [loop_tp],
                            rffi.INT,
                            compilation_info=eci)
loop_close = rffi.llexternal("uv_loop_close", [loop_tp],
                             rffi.INT,
                             compilation_info=eci)
loopClose = checking("loop_close", loop_close)
loop_alive = rffi.llexternal("uv_loop_alive", [loop_tp],
                             rffi.INT,
                             compilation_info=eci)


def loopAlive(loop):
    return bool(intmask(loop_alive(loop)))
Example #3
0
File: clibffi.py Project: Mu-L/pypy
    rffi.INT)
c_ffi_closure_alloc = external('ffi_closure_alloc', [rffi.SIZE_T, rffi.VOIDPP],
                               rffi.VOIDP,
                               _nowrapper=True)
c_ffi_closure_free = external('ffi_closure_free', [rffi.VOIDP],
                              lltype.Void,
                              _nowrapper=True)
if 0 and _MSVC:
    c_ffi_call_return_type = rffi.INT
else:
    c_ffi_call_return_type = lltype.Void
c_ffi_call = external('ffi_call', [FFI_CIFP, rffi.VOIDP, rffi.VOIDP, VOIDPP],
                      c_ffi_call_return_type,
                      save_err=rffi.RFFI_ERR_ALL | rffi.RFFI_ALT_ERRNO)
# Note: the RFFI_ALT_ERRNO flag matches the one in pyjitpl.direct_libffi_call
CALLBACK_TP = rffi.CCallback([FFI_CIFP, rffi.VOIDP, rffi.VOIDPP, rffi.VOIDP],
                             lltype.Void)
c_ffi_prep_closure_loc = external(
    'ffi_prep_closure_loc',
    [FFI_CLOSUREP, FFI_CIFP, CALLBACK_TP, rffi.VOIDP, rffi.VOIDP], rffi.INT)

FFI_STRUCT_P = lltype.Ptr(
    lltype.Struct('FFI_STRUCT', ('ffistruct', FFI_TYPE_P.TO),
                  ('members', lltype.Array(FFI_TYPE_P))))


@specialize.arg(3)
def make_struct_ffitype_e(size, aligment, field_types, track_allocation=True):
    """Compute the type of a structure.  Returns a FFI_STRUCT_P out of
       which the 'ffistruct' member is a regular FFI_TYPE.
    """
    tpe = lltype.malloc(FFI_STRUCT_P.TO,
Example #4
0
#include "src/precommondefs.h"

RPY_EXTERN Signed compute(Signed arg, Signed(*callback)(Signed))
{
    Signed res = callback(arg);
    if (res == -1)
        return -1;
    return res;
}
"""

eci = ExternalCompilationInfo(includes=['callback.h'],
                              include_dirs=[str(udir), cdir],
                              separate_module_sources=[c_source_callback])

args = [rffi.SIGNED, rffi.CCallback([rffi.SIGNED], rffi.SIGNED)]
compute = rffi.llexternal('compute', args, rffi.SIGNED, compilation_info=eci)

########################### callback end #############################


def rffi_callback():
    def g(i):
        return i + 3

    print compute(1, g)


def rffi_more_examples():
    rffi_callback()
Example #5
0
def fetch_constant_five(p):
    p[0] = rffi.cast(rffi.ULONGLONG, 5)
    return rffi.cast(rffi.INT, 0)


def fetch_constant_zero(p):
    p[0] = rffi.cast(rffi.ULONGLONG, 0)
    return rffi.cast(rffi.INT, 1)


def fetch_constant_neg(p):
    p[0] = rffi.cast(rffi.ULONGLONG, 123321)
    return rffi.cast(rffi.INT, 1)


FETCH_CB_P = rffi.CCallback([rffi.ULONGLONGP], rffi.INT)

ctx_globals = lltype.malloc(rffi.CArray(parse_c_type.GLOBAL_S),
                            len(global_names),
                            flavor='raw',
                            zero=True,
                            track_allocation=False)
c_glob_names = [rffi.str2charp(_n.encode('ascii')) for _n in global_names]
_helpers_keepalive = []
for _i, _fn in enumerate(
    [fetch_constant_five, fetch_constant_neg, fetch_constant_zero]):
    llf = llhelper(FETCH_CB_P, _fn)
    _helpers_keepalive.append(llf)
    ctx_globals[_i].c_name = c_glob_names[_i]
    ctx_globals[_i].c_address = rffi.cast(rffi.VOIDP, llf)
    type_op = (cffi_opcode.OP_CONSTANT_INT if _i != 1 else cffi_opcode.OP_ENUM)
Example #6
0
class cConfig:
    pass


for k, v in rffi_platform.configure(CConfig).items():
    setattr(cConfig, k, v)

for constant in CONSTANTS:
    locals()[constant] = getattr(cConfig, constant)
del constant, CONSTANTS

SIZE_T_TP = cConfig.size_t
EMJS_HANDLE_TP = cConfig.emjs_handle
EMJS_TYPE_TP = cConfig.emjs_type
CALLBACK_TP = rffi.CCallback([rffi.VOIDP, EMJS_HANDLE_TP], EMJS_HANDLE_TP)
CALLBACK_V_TP = rffi.CCallback([rffi.VOIDP, EMJS_HANDLE_TP], lltype.Void)


# This will on-demand load the javascript library for the implementation
# of all supporting functions.  it uses the PyV8 engine to run the javascript.

ctx = None
_strings = {}
_string_id = 1

def load_javascript_ctx():
    global ctx
    if ctx is not None:
        return ctx
Example #7
0
    "EAI_MEMORY", "EAI_NODATA", "EAI_NONAME", "EAI_OVERFLOW",
    "EAI_PROTOCOL", "EAI_SERVICE", "EAI_SOCKTYPE", "EALREADY",
    "EBADF", "EBUSY", "ECANCELED", "ECHARSET", "ECONNABORTED",
    "ECONNREFUSED", "ECONNRESET", "EDESTADDRREQ", "EEXIST", "EFAULT",
    "EFBIG", "EHOSTDOWN", "EHOSTUNREACH", "EINTR", "EINVAL", "EIO",
    "EISCONN", "EISDIR", "ELOOP", "EMFILE", "EMLINK", "EMSGSIZE",
    "ENAMETOOLONG", "ENETDOWN", "ENETUNREACH", "ENFILE", "ENOBUFS",
    "ENODEV", "ENOENT", "ENOMEM", "ENONET", "ENOPROTOOPT", "ENOSPC",
    "ENOSYS", "ENOTCONN", "ENOTDIR", "ENOTEMPTY", "ENOTSOCK",
    "ENOTSUP", "ENXIO", "EOF", "EPERM", "EPIPE", "EPROTO",
    "EPROTONOSUPPORT", "EPROTOTYPE", "ERANGE", "EROFS", "ESHUTDOWN",
    "ESPIPE", "ESRCH", "ETIMEDOUT", "ETXTBSY", "EXDEV", "UNKNOWN",
]

exit_cb_arg0 = lltype.Ptr(lltype.ForwardReference())
exit_cb = rffi.CCallback([exit_cb_arg0, int64_t, rffi.INT],      lltype.Void)

class CConfig:
    _compilation_info_ = eci

    timespec_t = rffi_platform.Struct("uv_timespec_t", [
        ("tv_sec",  rffi.LONG),
        ("tv_nsec", rffi.LONG)])

    stat_t = rffi_platform.Struct("uv_stat_t", [
        ("st_dev",      uint64_t  ),
        ("st_mode",     uint64_t  ),
        ("st_nlink",    uint64_t  ),
        ("st_uid",      uint64_t  ),
        ("st_gid",      uint64_t  ),
        ("st_rdev",     uint64_t  ),
Example #8
0
class cConfig:
    pass


for k, v in rffi_platform.configure(CConfig).items():
    setattr(cConfig, k, v)

for constant in CONSTANTS:
    locals()[constant] = getattr(cConfig, constant)
del constant, CONSTANTS

SIZE_T_TP = cConfig.size_t
EMJS_HANDLE_TP = cConfig.emjs_handle
EMJS_TYPE_TP = cConfig.emjs_type
CALLBACK_TP = rffi.CCallback([rffi.VOIDP, EMJS_HANDLE_TP], EMJS_HANDLE_TP)

# This will on-demand load the javascript library for the implementation
# of all supporting functions.  it uses the PyV8 engine to run the javascript.

ctx = None
_strings = {}
_string_id = 1


def load_javascript_ctx():
    global ctx
    if ctx is not None:
        return ctx

    import PyV8
Example #9
0
TIMELIB_ZONETYPE_OFFSET = rffi.cast(rffi.UINT, conf['TIMELIB_ZONETYPE_OFFSET'])

timelib_builtin_db = external('timelib_builtin_db', [], timelib_tzdb)
timelib_error_container_dtor = external('timelib_error_container_dtor',
                                        [timelib_error_container], lltype.Void)
timelib_update_ts = external('timelib_update_ts', [timelib_time, rffi.VOIDP],
                             lltype.Void)

timelib_update_from_sse = external('timelib_update_from_sse', [timelib_time],
                                   lltype.Void)

INTP = rffi.CArrayPtr(lltype.Signed)
timelib_date_to_int = external('timelib_date_to_int', [timelib_time, INTP],
                               lltype.Signed)

timelib_tz_get_wrapper = rffi.CCallback([rffi.CCHARP, timelib_tzdb],
                                        timelib_tzinfo)

timelib_strtotime = external('timelib_strtotime', [
    rffi.CCHARP, lltype.Signed, timelib_error_containerP, timelib_tzdb,
    timelib_tz_get_wrapper
], timelib_time)

timelib_diff = external('timelib_diff', [timelib_time, timelib_time],
                        timelib_rel_timeP)

timelib_time_clone = external('timelib_time_clone', [timelib_time],
                              timelib_time)
timelib_tzinfo_clone = external('timelib_tzinfo_clone', [timelib_tzinfo],
                                timelib_tzinfo)

timelib_parse_from_format = external('timelib_parse_from_format', [