def unicode_to_decimal_w(space, w_unistr): if not isinstance(w_unistr, W_UnicodeObject): raise OperationError(space.w_TypeError, space.wrap("expected unicode")) unistr = w_unistr._value result = ['\0'] * len(unistr) digits = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] for i in xrange(len(unistr)): uchr = ord(unistr[i]) if unicodedb.isspace(uchr): result[i] = ' ' continue try: result[i] = digits[unicodedb.decimal(uchr)] except KeyError: if 0 < uchr < 256: result[i] = chr(uchr) else: w_encoding = space.wrap('decimal') w_start = space.wrap(i) w_end = space.wrap(i+1) w_reason = space.wrap('invalid decimal Unicode string') raise OperationError(space.w_UnicodeEncodeError,space.newtuple ([w_encoding, w_unistr, w_start, w_end, w_reason])) return ''.join(result)
def unicode_to_decimal_w(space, w_unistr): if not isinstance(w_unistr, W_UnicodeObject): raise OperationError(space.w_TypeError, space.wrap("expected unicode")) unistr = w_unistr._value result = ['\0'] * len(unistr) digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] for i in xrange(len(unistr)): uchr = ord(unistr[i]) if unicodedb.isspace(uchr): result[i] = ' ' continue try: result[i] = digits[unicodedb.decimal(uchr)] except KeyError: if 0 < uchr < 256: result[i] = chr(uchr) else: w_encoding = space.wrap('decimal') w_start = space.wrap(i) w_end = space.wrap(i + 1) w_reason = space.wrap('invalid decimal Unicode string') raise OperationError( space.w_UnicodeEncodeError, space.newtuple( [w_encoding, w_unistr, w_start, w_end, w_reason])) return ''.join(result)
def test_compare_methods(self): for code in range(0x10000): char = unichr(code) assert char.isalnum() == unicodedb_4_1_0.isalnum(code) assert char.isalpha() == unicodedb_4_1_0.isalpha(code) assert char.isdecimal() == unicodedb_4_1_0.isdecimal(code) assert char.isdigit() == unicodedb_4_1_0.isdigit(code) assert char.islower() == unicodedb_4_1_0.islower(code) assert (code in self.diff_numeric or char.isnumeric()) == unicodedb_4_1_0.isnumeric(code) assert code in self.diff_isspace or char.isspace() == unicodedb_4_1_0.isspace(code), hex(code) assert char.istitle() == (unicodedb_4_1_0.isupper(code) or unicodedb_4_1_0.istitle(code)), code assert char.isupper() == unicodedb_4_1_0.isupper(code) assert char.lower() == unichr(unicodedb_4_1_0.tolower(code)) assert char.upper() == unichr(unicodedb_4_1_0.toupper(code)) assert code in self.diff_title or char.title() == unichr(unicodedb_4_1_0.totitle(code)), hex(code)
def _isspace(uchar): return unicodedb.isspace(ord(uchar))