예제 #1
0
def HPyTupleBuilder_Set(space, ctx, builder, index, h_item):
    # XXX if builder==0, there was an error inside _New. The C code just exits
    # here, but there is no tests for it. Write it
    w_builder = handles.deref(space, builder)
    assert isinstance(w_builder, W_TupleBuilder)
    w_item = handles.deref(space, h_item)
    w_builder.items_w[index] = w_item
예제 #2
0
def HPyList_Append(space, ctx, h_list, h_item):
    w_list = handles.deref(space, h_list)
    # XXX the tests should check what happens in this case
    assert isinstance(w_list, W_ListObject)
    w_item = handles.deref(space, h_item)
    w_list.append(w_item)
    return API.int(0)
예제 #3
0
def HPy_RichCompareBool(space, ctx, v, w, op):
    w_o1 = handles.deref(space, v)
    w_o2 = handles.deref(space, w)
    # Quick result when objects are the same.
    # Guarantees that identity implies equality.
    if space.is_w(w_o1, w_o2):
        opid = rffi.cast(lltype.Signed, op)
        if opid == llapi.HPy_EQ:
            return API.int(1)
        if opid == llapi.HPy_NE:
            return API.int(0)
    w_result = rich_compare(space, w_o1, w_o2, op)
    return API.int(space.is_true(w_result))
예제 #4
0
def HPy_InPlacePower(space, ctx, h1, h2, h3):
    # CPython seems to have a weird semantics for InPlacePower: if __ipow__ is
    # defined, the 3rd argument is always ignored (contrarily to what the
    # documentation says). If now, it falls back to pow, so the 3rd arg is
    # handled correctly. Here we try to be bug-to-bug compatible
    w_o1 = handles.deref(space, h1)
    w_o2 = handles.deref(space, h2)
    w_o3 = handles.deref(space, h3)
    w_ipow = space.lookup(w_o1, '__ipow__')
    if w_ipow is None:
        w_res = space.pow(w_o1, w_o2, w_o3)
    else:
        w_res = space.inplace_pow(w_o1, w_o2)
    return handles.new(space, w_res)
예제 #5
0
def _HPy_Cast(space, ctx, h):
    w_obj = handles.deref(space, h)
    if not isinstance(w_obj, W_HPyObject):
        # XXX: write a test for this
        raise oefmt(space.w_TypeError,
                    "Object of type '%T' is not a valid HPy object.", w_obj)
    return w_obj.hpy_data
예제 #6
0
def HPyNumber_Check(space, ctx, h):
    # XXX: write proper tests
    w_obj = handles.deref(space, h)
    if (space.lookup(w_obj, '__int__') or space.lookup(w_obj, '__float__')
            or 0):  # XXX in py3.8: space.lookup(w_obj, '__index__')):
        return API.int(1)
    return API.int(0)
예제 #7
0
def _HPy_New(space, ctx, h_type, data):
    w_type = handles.deref(space, h_type)
    w_result = _create_instance(space, w_type)
    data = llapi.cts.cast('void**', data)
    data[0] = w_result.hpy_data
    h = handles.new(space, w_result)
    return h
예제 #8
0
def HPyBytes_AsString(space, ctx, h):
    w_obj = handles.deref(space, h)
    s = space.bytes_w(w_obj)
    llbuf, llstring, flag = rffi.get_nonmovingbuffer_ll_final_null(s)
    cb = handles.FreeNonMovingBuffer(llbuf, llstring, flag)
    handles.attach_release_callback(space, h, cb)
    return llbuf
예제 #9
0
def HPyLong_AsLong(space, ctx, h):
    w_obj = handles.deref(space, h)
    #w_obj = space.int(w_obj)     --- XXX write a test for this
    value = space.int_w(w_obj)
    result = rffi.cast(rffi.LONG, value)
    #if rffi.cast(lltype.Signed, result) != value: --- XXX on Windows 64
    #    ...
    return result
예제 #10
0
def HPy_Bytes(space, ctx, h_obj):
    # XXX: cpyext checks and returns <NULL>. Add a test to HPy and fix here
    w_obj = handles.deref(space, h_obj)
    if space.type(w_obj) is space.w_bytes:
        # XXX write a test for this case
        return handles.dup(space, h_obj)
    w_result = invoke_bytes_method(space, w_obj)
    if w_result is not None:
        return handles.new(space, w_result)
    # return PyBytes_FromObject(space, w_obj)
    # XXX: write a test for this case
    buffer = space.buffer_w(w_obj, space.BUF_FULL_RO)
    w_res = space.newbytes(buffer.as_str())
    return handles.new(space, w_res)
예제 #11
0
def get_bases_from_params(space, ctx, params):
    KIND = llapi.cts.gettype('HPyType_SpecParam_Kind')
    params = rffi.cast(rffi.CArrayPtr(llapi.cts.gettype('HPyType_SpecParam')),
                       params)
    if not params:
        return []
    found_base = False
    found_basestuple = False
    bases_w = []
    i = 0
    while True:
        # in llapi.py, HPyType_SpecParam.object is declared of type "struct
        # _HPy_s", so we need to manually fish the ._i inside
        p_kind = rffi.cast(lltype.Signed, params[i].c_kind)
        p_h = params[i].c_object.c__i
        if p_kind == 0:
            break
        i += 1
        if p_kind == KIND.HPyType_SpecParam_Base:
            found_base = True
            w_base = handles.deref(space, p_h)
            bases_w.append(w_base)
        elif p_kind == KIND.HPyType_SpecParam_BasesTuple:
            found_basestuple = True
            w_bases = handles.deref(space, p_h)
            bases_w = space.unpackiterable(w_bases)
        else:
            raise NotImplementedError('XXX write a test')

    if found_basestuple > 1:
        raise NotImplementedError('XXX write a test')
    if found_basestuple and found_base:
        raise NotImplementedError('XXX write a test')

    # return a copy of bases_w to ensure that it's a not-resizable list
    return make_sure_not_resized(bases_w[:])
예제 #12
0
def HPy_GetAttr(space, ctx, h_obj, h_name):
    w_obj = handles.deref(space, h_obj)
    w_name = handles.deref(space, h_name)
    w_res = space.getattr(w_obj, w_name)
    return handles.new(space, w_res)
예제 #13
0
def HPy_GetItem_s(space, ctx, h_obj, key):
    w_obj = handles.deref(space, h_obj)
    w_key = API.ccharp2text(space, key)
    w_res = space.getitem(w_obj, w_key)
    return handles.new(space, w_res)
예제 #14
0
def HPy_GetItem(space, ctx, h_obj, h_key):
    w_obj = handles.deref(space, h_obj)
    w_key = handles.deref(space, h_key)
    w_res = space.getitem(w_obj, w_key)
    return handles.new(space, w_res)
예제 #15
0
def HPy_HasAttr_s(space, ctx, h_obj, name):
    w_obj = handles.deref(space, h_obj)
    w_name = API.ccharp2text(space, name)
    return _HasAttr(space, w_obj, w_name)
예제 #16
0
def HPy_Hash(space, ctx, h_obj):
    w_obj = handles.deref(space, h_obj)
    return API.cts.cast('HPy_hash_t', space.hash_w(w_obj))
예제 #17
0
def HPyTracker_RemoveAll(space, ctx, ht):
    w_tracker = handles.deref(space, ht)
    assert isinstance(w_tracker, W_Tracker)
    w_tracker.remove_all()
예제 #18
0
def HPy_RichCompare(space, ctx, v, w, op):
    w_o1 = handles.deref(space, v)
    w_o2 = handles.deref(space, w)
    w_result = rich_compare(space, w_o1, w_o2, op)
    return handles.new(space, w_result)
예제 #19
0
def HPyTracker_Free(space, ctx, ht):
    w_tracker = handles.deref(space, ht)
    assert isinstance(w_tracker, W_Tracker)
    w_tracker.free(space)
    handles.close(space, ht)
예제 #20
0
def HPy_GetAttr_s(space, ctx, h_obj, name):
    w_obj = handles.deref(space, h_obj)
    w_name = API.ccharp2text(space, name)
    w_res = space.getattr(w_obj, w_name)
    return handles.new(space, w_res)
예제 #21
0
def HPy_Length(space, ctx, h_obj):
    w_obj = handles.deref(space, h_obj)
    return space.len_w(w_obj)
예제 #22
0
def HPy_IsTrue(space, ctx, h_obj):
    w_obj = handles.deref(space, h_obj)
    return API.int(space.is_true(w_obj))
예제 #23
0
def HPy_HasAttr(space, ctx, h_obj, h_name):
    w_obj = handles.deref(space, h_obj)
    w_name = handles.deref(space, h_name)
    return _HasAttr(space, w_obj, w_name)
예제 #24
0
def HPy_SetItem_i(space, ctx, h_obj, idx, h_val):
    w_obj = handles.deref(space, h_obj)
    w_key = space.newint(idx)
    w_val = handles.deref(space, h_val)
    space.setitem(w_obj, w_key, w_val)
    return API.int(0)
예제 #25
0
def HPy_SetAttr_s(space, ctx, h_obj, name, h_value):
    w_obj = handles.deref(space, h_obj)
    w_name = API.ccharp2text(space, name)
    w_value = handles.deref(space, h_value)
    operation.setattr(space, w_obj, w_name, w_value)
    return API.int(0)
예제 #26
0
def HPy_SetItem_s(space, ctx, h_obj, key, h_val):
    w_obj = handles.deref(space, h_obj)
    w_key = API.ccharp2text(space, key)
    w_val = handles.deref(space, h_val)
    space.setitem(w_obj, w_key, w_val)
    return API.int(0)
예제 #27
0
def HPy_GetItem_i(space, ctx, h_obj, idx):
    w_obj = handles.deref(space, h_obj)
    w_key = space.newint(idx)
    w_res = space.getitem(w_obj, w_key)
    return handles.new(space, w_res)
예제 #28
0
def HPy_Str(space, ctx, h_obj):
    # XXX: cpyext checks and returns <NULL>. Add a test to HPy and fix here
    w_obj = handles.deref(space, h_obj)
    w_res = space.str(w_obj)
    return handles.new(space, w_res)
예제 #29
0
def HPy_SetItem(space, ctx, h_obj, h_key, h_val):
    w_obj = handles.deref(space, h_obj)
    w_key = handles.deref(space, h_key)
    w_val = handles.deref(space, h_val)
    space.setitem(w_obj, w_key, w_val)
    return API.int(0)
예제 #30
0
def HPy_ASCII(space, ctx, h_obj):
    w_obj = handles.deref(space, h_obj)
    w_res = operation.ascii(space, w_obj)
    return handles.new(space, w_res)