예제 #1
0
def buffer__Unicode(space, w_unicode):
    from pypy.rlib.rstruct.unichar import pack_unichar
    charlist = []
    for unich in w_unicode._value:
        pack_unichar(unich, charlist)
    from pypy.interpreter.buffer import StringBuffer
    return space.wrap(StringBuffer(''.join(charlist)))
예제 #2
0
def descr_buffer__new__(space, w_subtype, w_object, offset=0, size=-1):
    # w_subtype can only be exactly 'buffer' for now
    if not space.is_w(w_subtype, space.gettypefor(Buffer)):
        raise OperationError(space.w_TypeError,
                             space.wrap("argument 1 must be 'buffer'"))

    if space.isinstance_w(w_object, space.w_unicode):
        # unicode objects support the old buffer interface
        # but not the new buffer interface (change in python  2.7)
        from pypy.rlib.rstruct.unichar import pack_unichar, UNICODE_SIZE
        unistr = space.unicode_w(w_object)
        builder = StringBuilder(len(unistr) * UNICODE_SIZE)
        for unich in unistr:
            pack_unichar(unich, builder)
        from pypy.interpreter.buffer import StringBuffer
        w_buffer = space.wrap(StringBuffer(builder.build()))
    else:
        w_buffer = space.buffer(w_object)

    buffer = space.interp_w(Buffer, w_buffer)  # type-check
    if offset == 0 and size == -1:
        return w_buffer
    # handle buffer slices
    if offset < 0:
        raise OperationError(space.w_ValueError,
                             space.wrap("offset must be zero or positive"))
    if size < -1:
        raise OperationError(space.w_ValueError,
                             space.wrap("size must be zero or positive"))
    if isinstance(buffer, RWBuffer):
        buffer = RWSubBuffer(buffer, offset, size)
    else:
        buffer = SubBuffer(buffer, offset, size)
    return space.wrap(buffer)
예제 #3
0
def buffer__RopeUnicode(space, w_unicode):
    from pypy.rlib.rstruct.unichar import pack_unichar
    charlist = []
    node = w_unicode._node
    iter = rope.ItemIterator(node)
    for idx in range(node.length()):
        unich = unichr(iter.nextint())
        pack_unichar(unich, charlist)
    from pypy.interpreter.buffer import StringBuffer
    return space.wrap(StringBuffer(''.join(charlist)))
예제 #4
0
def buffer__RopeUnicode(space, w_unicode):
    from pypy.rlib.rstruct.unichar import pack_unichar, UNICODE_SIZE
    node = w_unicode._node
    iter = rope.ItemIterator(node)
    length = node.length()
    builder = StringBuilder(length * UNICODE_SIZE)
    for idx in range(length):
        unich = unichr(iter.nextint())
        pack_unichar(unich, builder)
    from pypy.interpreter.buffer import StringBuffer
    return space.wrap(StringBuffer(builder.build()))
예제 #5
0
def buffer__Rope(space, w_string):
    from pypy.interpreter.buffer import StringBuffer
    value = w_string._node.flatten_string()      # XXX inefficient
    return space.wrap(StringBuffer(value))
예제 #6
0
 def buffer_w(self, space, flags):
     return SimpleView(StringBuffer("foobar"))
예제 #7
0
def buffer__String(space, w_string):
    from pypy.interpreter.buffer import StringBuffer
    return space.wrap(StringBuffer(w_string._value))
예제 #8
0
def buffer__String(space, w_string):
    return space.wrap(StringBuffer(w_string._value))
예제 #9
0
 def descr_hex(self, space):
     from pypy.objspace.std.bytearrayobject import _array_to_hexstring
     return _array_to_hexstring(space, StringBuffer(self._value), 0, 1,
                                len(self._value))
예제 #10
0
 def buffer_w(self, space, flags):
     space.check_buf_flags(flags, True)
     return SimpleView(StringBuffer(self._value))