def test_unichr(): a = runicode.UNICHR(0xffff) assert a == u'\uffff' if runicode.MAXUNICODE > 0xffff: a = runicode.UNICHR(0x10000) if sys.maxunicode < 0x10000: assert len(a) == 2 # surrogates else: assert len(a) == 1 else: py.test.raises(ValueError, runicode.UNICHR, 0x10000)
def test_encode_surrogate_pair(self): u = runicode.UNICHR(0xD800) + runicode.UNICHR(0xDC00) if runicode.MAXUNICODE < 65536: # Narrow unicode build, consider utf16 surrogate pairs assert runicode.unicode_encode_unicode_escape( u, len(u), True) == r'\U00010000' assert runicode.unicode_encode_raw_unicode_escape( u, len(u), True) == r'\U00010000' else: # Wide unicode build, don't merge utf16 surrogate pairs assert runicode.unicode_encode_unicode_escape( u, len(u), True) == r'\ud800\udc00' assert runicode.unicode_encode_raw_unicode_escape( u, len(u), True) == r'\ud800\udc00'
def _format_int_or_long(self, w_num, kind): space = self.space if self._precision != -1: msg = "precision not allowed in integer type" raise OperationError(space.w_ValueError, space.wrap(msg)) sign_char = "\0" tp = self._type if tp == "c": if self._sign != "\0": msg = "sign not allowed with 'c' presentation type" raise OperationError(space.w_ValueError, space.wrap(msg)) value = space.int_w(w_num) if self.is_unicode: result = runicode.UNICHR(value) else: result = chr(value) n_digits = 1 n_remainder = 1 to_remainder = 0 n_prefix = 0 to_prefix = 0 to_numeric = 0 else: if tp == "b": base = 2 skip_leading = 2 elif tp == "o": base = 8 skip_leading = 2 elif tp == "x" or tp == "X": base = 16 skip_leading = 2 elif tp == "n" or tp == "d": base = 10 skip_leading = 0 else: raise AssertionError("shouldn't reach") if kind == INT_KIND: result = self._int_to_base(base, space.int_w(w_num)) else: result = self._long_to_base(base, space.bigint_w(w_num)) n_prefix = skip_leading if self._alternate else 0 to_prefix = 0 if result[0] == "-": sign_char = "-" skip_leading += 1 to_prefix += 1 n_digits = len(result) - skip_leading n_remainder = 0 to_remainder = 0 to_numeric = skip_leading self._get_locale(tp) spec = self._calc_num_width(n_prefix, sign_char, to_numeric, n_digits, n_remainder, False, result) fill = self._lit( " ") if self._fill_char == "\0" else self._fill_char upper = self._type == "X" return self.space.wrap( self._fill_number(spec, result, to_numeric, to_prefix, fill, to_remainder, upper))
def f(x): u = runicode.UNICHR(x) t = runicode.ORD(u) return t
def PyUnicode_GetMax(space): """Get the maximum ordinal for a Unicode character.""" return runicode.UNICHR(runicode.MAXUNICODE)