# HG changeset patch # User Paul Boddie # Date 1624831991 -7200 # Node ID c07b0dd14f8572951df710fe5e0b221ca1adf071 # Parent 8657f34be3a466a2afd1fab3dfef4eff0bf9cc87 Moved integer instantiation support to library functions. diff -r 8657f34be3a4 -r c07b0dd14f85 generator.py --- a/generator.py Sun Jun 27 22:39:01 2021 +0200 +++ b/generator.py Mon Jun 28 00:13:11 2021 +0200 @@ -1202,14 +1202,14 @@ # Special-case the integer type. + # Here, the __builtins__.int.new_int function is called with the + # initialiser's parameter. + if path == self.int_type: print >>f_code, """\ -__attr %s(__attr __self, __attr number_or_string) +__attr %s(__attr __self, __attr number_or_string, __attr base) { - if (!__BOOL(__fn_native_int_is_int(__NULL, number_or_string))) - __raise_value_error(number_or_string); - - return number_or_string; + return __fn___builtins___int_new_int(__NULL, number_or_string, base); } """ % ( encode_instantiator_pointer(path), diff -r 8657f34be3a4 -r c07b0dd14f85 lib/__builtins__/int.py --- a/lib/__builtins__/int.py Sun Jun 27 22:39:01 2021 +0200 +++ b/lib/__builtins__/int.py Mon Jun 28 00:13:11 2021 +0200 @@ -19,6 +19,7 @@ this program. If not, see . """ +from __builtins__.str import basestring from __builtins__.unicode import utf8string from native import get_maxint, get_minint, is_int, \ int_add, int_and, int_div, int_eq, int_ge, int_gt, \ @@ -26,15 +27,47 @@ int_neg, int_not, int_or, int_pow, int_rshift, int_str, \ int_sub, int_xor +def new_int(number_or_string, base=10): + + "Initialise the integer with the given 'number_or_string'." + + if is_int(number_or_string): + return number_or_string + elif isinstance(number_or_string, basestring): + return str_to_int(number_or_string, base) + else: + raise TypeError + +def str_to_int(value, base=10): + + "Decode the string 'value' using the given 'base'." + + # NOTE: Add support for lower and upper in the string classes. + + #value = value.lower() + len_value = len(value) + digits = "0123456789abcdefghijklmnopqrstuvwxyz" + + result = 0 + i = 0 + + while i < len_value: + c = value[i] + d = digits.index(c) + result = result * base + d + i += 1 + + return result + class int: "An integer abstraction." - def __init__(self, number_or_string=None): + def __init__(self, number_or_string=None, base=10): "Initialise the integer with the given 'number_or_string'." - # Implemented in the translator. + # Implemented by new_int above, invoked specially by the translator. pass diff -r 8657f34be3a4 -r c07b0dd14f85 tests/int.py --- a/tests/int.py Sun Jun 27 22:39:01 2021 +0200 +++ b/tests/int.py Mon Jun 28 00:13:11 2021 +0200 @@ -11,3 +11,20 @@ a = int("a") # should raise an exception except ValueError, exc: print 'int("a") failed:', exc.value + +try: + a = int("!") # should raise an exception +except ValueError, exc: + print 'int("!") failed:', exc.value + +a = int("a", 16) +b = int("123") +print a # 10 +print b, i, b == i # 123, 123, True +print b, j, b == j # 123, 123, True + +a_is_int = isinstance(a, int) +j_is_int = isinstance(j, int) + +print a_is_int # True +print j_is_int # True