Exemple #1
0
def new_array_type(space, w_ctptr, w_length):
    if not isinstance(w_ctptr, ctypeptr.W_CTypePointer):
        raise OperationError(space.w_TypeError,
                             space.wrap("first arg must be a pointer ctype"))
    ctitem = w_ctptr.ctitem
    if ctitem.size < 0:
        raise oefmt(space.w_ValueError, "array item of unknown size: '%s'",
                    ctitem.name)
    if space.is_w(w_length, space.w_None):
        length = -1
        arraysize = -1
        extra = '[]'
    else:
        length = space.getindex_w(w_length, space.w_OverflowError)
        if length < 0:
            raise OperationError(space.w_ValueError,
                                 space.wrap("negative array length"))
        try:
            arraysize = ovfcheck(length * ctitem.size)
        except OverflowError:
            raise OperationError(
                space.w_OverflowError,
                space.wrap("array size would overflow a ssize_t"))
        extra = '[%d]' % length
    #
    ctype = ctypearray.W_CTypeArray(space, w_ctptr, length, arraysize, extra)
    return ctype
Exemple #2
0
def _new_array_type(space, w_ctptr, length):
    _setup_wref(rweakref.has_weakref_support())
    if not isinstance(w_ctptr, ctypeptr.W_CTypePointer):
        raise oefmt(space.w_TypeError, "first arg must be a pointer ctype")
    arrays = w_ctptr._array_types
    if arrays is None:
        arrays = rweakref.RWeakValueDictionary(int, ctypearray.W_CTypeArray)
        w_ctptr._array_types = arrays
    else:
        ctype = arrays.get(length)
        if ctype is not None:
            return ctype
    #
    ctitem = w_ctptr.ctitem
    if ctitem.size < 0:
        raise oefmt(space.w_ValueError, "array item of unknown size: '%s'",
                    ctitem.name)
    if length < 0:
        assert length == -1
        arraysize = -1
        extra = '[]'
    else:
        try:
            arraysize = ovfcheck(length * ctitem.size)
        except OverflowError:
            raise oefmt(space.w_OverflowError,
                        "array size would overflow a ssize_t")
        extra = '[%d]' % length
    #
    ctype = ctypearray.W_CTypeArray(space, w_ctptr, length, arraysize, extra)
    arrays.set(length, ctype)
    return ctype
Exemple #3
0
def _new_array_type(space, w_ctptr, length):
    unique_cache = space.fromcache(UniqueCache)
    unique_key = (w_ctptr, length)
    try:
        return unique_cache.arrays[unique_key]
    except KeyError:
        pass
    #
    if not isinstance(w_ctptr, ctypeptr.W_CTypePointer):
        raise OperationError(space.w_TypeError,
                             space.wrap("first arg must be a pointer ctype"))
    ctitem = w_ctptr.ctitem
    if ctitem.size < 0:
        raise oefmt(space.w_ValueError, "array item of unknown size: '%s'",
                    ctitem.name)
    if length < 0:
        assert length == -1
        arraysize = -1
        extra = '[]'
    else:
        try:
            arraysize = ovfcheck(length * ctitem.size)
        except OverflowError:
            raise OperationError(
                space.w_OverflowError,
                space.wrap("array size would overflow a ssize_t"))
        extra = '[%d]' % length
    #
    ctype = ctypearray.W_CTypeArray(space, w_ctptr, length, arraysize, extra)
    unique_cache.arrays[unique_key] = ctype
    return ctype