Exemple #1
0
def slot_tp_new(space, type, w_args, w_kwds):
    from pypy.module.cpyext.tupleobject import PyTuple_Check
    pyo = rffi.cast(PyObject, type)
    w_type = from_ref(space, pyo)
    w_func = space.getattr(w_type, space.wrap("__new__"))
    assert PyTuple_Check(space, w_args)
    args_w = [w_type] + space.fixedview(w_args)
    w_args_new = space.newtuple(args_w)
    return space.call(w_func, w_args_new, w_kwds)
Exemple #2
0
 def test_tupleobject(self, space):
     assert not PyTuple_Check(space, space.w_None)
     with raises_w(space, SystemError):
         PyTuple_SetItem(space, space.w_None, 0, space.w_None)
     atuple = space.newtuple(
         [space.wrap(0), space.wrap(1),
          space.wrap('yay')])
     assert PyTuple_Size(space, atuple) == 3
     with raises_w(space, SystemError):
         PyTuple_Size(space, space.newlist([]))
Exemple #3
0
def PySequence_Fast_GET_ITEM(space, py_obj, index):
    """Return the ith element of o, assuming that o was returned by
    PySequence_Fast(), o is not NULL, and that i is within bounds.
    """
    py_obj = rffi.cast(PyObject, py_obj)
    if PyTuple_Check(space, py_obj):
        from pypy.module.cpyext.tupleobject import PyTupleObject
        py_tuple = rffi.cast(PyTupleObject, py_obj)
        return py_tuple.c_ob_item[index]
    else:
        from pypy.module.cpyext.listobject import PyList_GET_ITEM
        w_obj = from_ref(space, py_obj)
        return PyList_GET_ITEM(space, w_obj, index)
Exemple #4
0
def PySequence_Fast_GET_SIZE(space, py_obj):
    """Returns the length of o, assuming that o was returned by
    PySequence_Fast() and that o is not NULL.  The size can also be
    gotten by calling PySequence_Size() on o, but
    PySequence_Fast_GET_SIZE() is faster because it can assume o is a list
    or tuple."""
    py_obj = rffi.cast(PyObject, py_obj)
    if PyTuple_Check(space, py_obj):
        from pypy.module.cpyext.tupleobject import PyTupleObject
        py_tuple = rffi.cast(PyTupleObject, py_obj)
        return py_tuple.c_ob_size
    else:
        from pypy.module.cpyext.listobject import PyList_GET_SIZE
        w_obj = from_ref(space, py_obj)
        return PyList_GET_SIZE(space, w_obj)
Exemple #5
0
def PySequence_Fast_ITEMS(space, py_obj):
    """Return the underlying array of PyObject pointers.  Assumes that o was returned
    by PySequence_Fast() and o is not NULL.

    Note, if a list gets resized, the reallocation may relocate the items array.
    So, only use the underlying array pointer in contexts where the sequence
    cannot change.
    """
    py_obj = rffi.cast(PyObject, py_obj)
    if PyTuple_Check(space, py_obj):
        from pypy.module.cpyext.tupleobject import PyTupleObject
        py_tuple = rffi.cast(PyTupleObject, py_obj)
        return rffi.cast(PyObjectP, py_tuple.c_ob_item)
    else:
        from pypy.module.cpyext.listobject import get_list_storage
        w_obj = from_ref(space, py_obj)
        assert isinstance(w_obj, W_ListObject)
        storage = get_list_storage(space, w_obj)
        return rffi.cast(PyObjectP, storage._elems)