def test_unicode(self): sb = UnicodeBuilderRepr.ll_new(32) UnicodeBuilderRepr.ll_append_char(sb, u'x') UnicodeBuilderRepr.ll_append(sb, llunicode(u"abc")) UnicodeBuilderRepr.ll_append_slice(sb, llunicode(u"foobar"), 2, 5) UnicodeBuilderRepr.ll_append_multiple_char(sb, u'y', 30) u = UnicodeBuilderRepr.ll_build(sb) assert hlunicode(u) == u"xabcoba" + u"y" * 30
def convert_array_from_object(self, cdata, w_ob): space = self.space if (space.isinstance_w(w_ob, space.w_list) or space.isinstance_w(w_ob, space.w_tuple)): self._convert_array_from_listview(cdata, w_ob) elif (self.can_cast_anything or (self.ctitem.is_primitive_integer and self.ctitem.size == rffi.sizeof(lltype.Char))): if not space.isinstance_w(w_ob, space.w_str): raise self._convert_error("str or list or tuple", w_ob) s = space.str_w(w_ob) n = len(s) if self.length >= 0 and n > self.length: raise oefmt(space.w_IndexError, "initializer string is too long for '%s' (got %d " "characters)", self.name, n) copy_string_to_raw(llstr(s), cdata, 0, n) if n != self.length: cdata[n] = '\x00' elif isinstance(self.ctitem, ctypeprim.W_CTypePrimitiveUniChar): if not space.isinstance_w(w_ob, space.w_unicode): raise self._convert_error("unicode or list or tuple", w_ob) s = space.unicode_w(w_ob) n = len(s) if self.length >= 0 and n > self.length: raise oefmt(space.w_IndexError, "initializer unicode string is too long for '%s' " "(got %d characters)", self.name, n) unichardata = rffi.cast(rffi.CWCHARP, cdata) copy_unicode_to_raw(llunicode(s), unichardata, 0, n) if n != self.length: unichardata[n] = u'\x00' else: raise self._convert_error("list or tuple", w_ob)
def _unicode_to_wchar(u, target_ptr, target_length, add_final_zero): # 'target_ptr' is a raw pointer to 'target_length' wchars; # we assume here that target_length == len(u). unichardata = rffi.cast(rffi.CWCHARP, target_ptr) copy_unicode_to_raw(llunicode(u), unichardata, 0, target_length) if add_final_zero: unichardata[target_length] = u'\x00'
def check_latin1(s, expected, test_prebuilt=False): with choosen_seed(0x8a9f065a358479f4, 0x11cb1e9ee7f40e1f, test_misaligned_path=True, test_prebuilt=test_prebuilt): z = ll_hash_string_siphash24(llunicode(s)) assert z == intmask(expected)
def get_const_ptr_for_unicode(s): from rpython.rtyper.annlowlevel import llunicode if not we_are_translated(): try: return _const_ptr_for_unicode[s] except KeyError: pass if isinstance(s, str): s = unicode(s) result = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, llunicode(s))) if not we_are_translated(): _const_ptr_for_unicode[s] = result return result
def convert_array_from_object(self, cdata, w_ob): space = self.space if (space.isinstance_w(w_ob, space.w_list) or space.isinstance_w(w_ob, space.w_tuple)): if self.ctitem.pack_list_of_items(cdata, w_ob): # fast path pass else: self._convert_array_from_listview(cdata, space.listview(w_ob)) elif self.accept_str: if not space.isinstance_w(w_ob, space.w_bytes): raise self._convert_error("str or list or tuple", w_ob) s = space.bytes_w(w_ob) n = len(s) if self.length >= 0 and n > self.length: raise oefmt( space.w_IndexError, "initializer string is too long for '%s' (got %d " "characters)", self.name, n) if isinstance(self.ctitem, ctypeprim.W_CTypePrimitiveBool): self._must_be_string_of_zero_or_one(s) copy_string_to_raw(llstr(s), cdata, 0, n) if n != self.length: cdata[n] = '\x00' elif isinstance(self.ctitem, ctypeprim.W_CTypePrimitiveUniChar): if not space.isinstance_w(w_ob, space.w_unicode): raise self._convert_error("unicode or list or tuple", w_ob) s = space.unicode_w(w_ob) n = len(s) if self.length >= 0 and n > self.length: raise oefmt( space.w_IndexError, "initializer unicode string is too long for '%s' " "(got %d characters)", self.name, n) unichardata = rffi.cast(rffi.CWCHARP, cdata) copy_unicode_to_raw(llunicode(s), unichardata, 0, n) if n != self.length: unichardata[n] = u'\x00' else: raise self._convert_error("list or tuple", w_ob)
def f(arg): s = llunicode(hlunicode(arg)) return len(s.chars)
def test_llunicode(self): s = llunicode(u"abc") assert len(s.chars) == 3 assert s.chars[0] == u"a" assert s.chars[1] == u"b" assert s.chars[2] == u"c"