# HG changeset patch # User Paul Boddie # Date 1624824743 -7200 # Node ID bbcea70e1cde6573f3291bf05f93ee95fc13bdf1 # Parent acf2b78e9ee587e96631182102d0cd35622f96d0 Fixed int instantiation by special-casing the type in the translator. This also avoids allocating an instance which would never be used with the tagged value representation. diff -r acf2b78e9ee5 -r bbcea70e1cde generator.py --- a/generator.py Sun Jun 27 22:10:02 2021 +0200 +++ b/generator.py Sun Jun 27 22:12:23 2021 +0200 @@ -1200,18 +1200,36 @@ for name in parameters: l.append("__attr %s" % name) - print >>f_code, """\ + # Special-case the integer type. + + if path == self.int_type: + print >>f_code, """\ +__attr %s(__attr __self, __attr number_or_string) +{ + if (!__BOOL(__fn_native_int_is_int(__self, number_or_string))) + __raise_value_error(number_or_string); + + return number_or_string; +} +""" % ( + encode_instantiator_pointer(path), + ) + + # Generic instantiation support. + + else: + print >>f_code, """\ __attr %s(__attr __self%s) { return %s(__NEWINSTANCE(%s)%s); } """ % ( - encode_instantiator_pointer(path), - l and ", %s" % ",".join(l) or "", - encode_function_pointer(initialiser), - encode_path(path), - parameters and ", %s" % ", ".join(parameters) or "" - ) + encode_instantiator_pointer(path), + l and ", %s" % ",".join(l) or "", + encode_function_pointer(initialiser), + encode_path(path), + parameters and ", %s" % ", ".join(parameters) or "" + ) # Signature: __new_typename(__attr __self, ...) diff -r acf2b78e9ee5 -r bbcea70e1cde lib/__builtins__/int.py --- a/lib/__builtins__/int.py Sun Jun 27 22:10:02 2021 +0200 +++ b/lib/__builtins__/int.py Sun Jun 27 22:12:23 2021 +0200 @@ -3,7 +3,7 @@ """ Integer objects. -Copyright (C) 2015, 2016, 2017, 2018 Paul Boddie +Copyright (C) 2015, 2016, 2017, 2018, 2021 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -34,7 +34,7 @@ "Initialise the integer with the given 'number_or_string'." - # NOTE: To be implemented. + # Implemented in the translator. pass diff -r acf2b78e9ee5 -r bbcea70e1cde tests/int.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/int.py Sun Jun 27 22:12:23 2021 +0200 @@ -0,0 +1,13 @@ +i = int(123) +j = 123 +print i, j, i == j # 123 123 True +k = 456 +print i, k, i == k # 123 456 False +h = int(789) +print i, h, i == h # 123 789 False +print j, h, j == h # 123 789 False + +try: + a = int("a") # should raise an exception +except ValueError, exc: + print 'int("a") failed:', exc.value