Debian Codegraph @

pypy/interpreter/astcompiler/astbuilder.py

raw symbols SCIP
1 from pypy.interpreter import error
2 from rpython.rlib import rutf8, objectmodel
3
4 def parse_number(space, raw):
5 from pypy.objspace.std.intobject import _string_to_int_or_long
6 from pypy.objspace.std.floatobject import _string_to_float
7 base = 10
8 dot_in_raw = '.' in raw
9 if raw.startswith("-"):
10 negative = True
11 raw = raw.lstrip("-")
12 else:
13 negative = False
14 if not dot_in_raw and raw.startswith("0"):
15 # find first non-zero, usually a base specifier
16 i = 0
17 #string must end with at least one digit after the base
18 limit = len(raw) - 1
19 # if no base specifier, assume 8 (like CPython)
20 base = 8
21 while i < limit:
22 if raw[i] == "0":
23 pass
24 elif raw[i] in "Xx":
25 base = 16
26 i += 1
27 if raw[i] == '_':
28 i += 1
29 break
30 elif raw[i] in "Bb":
31 base = 2
32 i += 1
33 if raw[i] == '_':
34 i += 1
35 break
36 elif raw[i] in "Ee":
37 base = 10
38 # leave the leading 0
39 i -= 1
40 break
41 elif raw[i] in "Oo":
42 base = 8
43 i += 1
44 if raw[i] == '_':
45 i += 1
46 break
47 else:
48 # something like 077e0, which is a float
49 # or 0_0_0
50 base = 10
51 if raw[i] == '_':
52 i += 1
53 break
54 i += 1
55 else:
56 # for 00j, do not trim the last 0
57 i -= 1
58 if i > 0:
59 raw = raw[i:]
60 if negative:
61 raw = "-" + raw
62 # by construction this should not be able to fail: the tokenizer only
63 # recognizes ascii characters as parts of a number
64 if not objectmodel.we_are_translated():
65 rutf8.check_ascii(raw)
66 w_num_str = space.newtext(raw, len(raw))
67 # CPython uses the fact that strtol(raw, end) will successfully reach
68 # the end of raw if it is a proper int string. We use the next two
69 # checks instead
70 if raw[-1] in "jJ":
71 tp = space.w_complex
72 return space.call_function(tp, w_num_str)
73 if base == 10 and (dot_in_raw or 'e' in raw or 'E' in raw):
74 return space.call_function(space.w_float, w_num_str)
75 # OK, not a complex and not a float
76 return _string_to_int_or_long(space, w_num_str, raw, base)