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)))
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)
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)))
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()))
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))
def buffer_w(self, space, flags): return SimpleView(StringBuffer("foobar"))
def buffer__String(space, w_string): from pypy.interpreter.buffer import StringBuffer return space.wrap(StringBuffer(w_string._value))
def buffer__String(space, w_string): return space.wrap(StringBuffer(w_string._value))
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))
def buffer_w(self, space, flags): space.check_buf_flags(flags, True) return SimpleView(StringBuffer(self._value))