# HG changeset patch # User Paul Boddie # Date 1624824891 -7200 # Node ID b47da767f70404e0c2b0186b7a9403f8ba5f8d62 # Parent 326570523de539c28bcd6f70ab87e0f63f8f1d83# Parent bbcea70e1cde6573f3291bf05f93ee95fc13bdf1 Merged changes from the default branch. diff -r 326570523de5 -r b47da767f704 generator.py --- a/generator.py Tue Jun 22 23:04:00 2021 +0200 +++ b/generator.py Sun Jun 27 22:14:51 2021 +0200 @@ -1263,18 +1263,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 326570523de5 -r b47da767f704 lib/__builtins__/int.py --- a/lib/__builtins__/int.py Tue Jun 22 23:04:00 2021 +0200 +++ b/lib/__builtins__/int.py Sun Jun 27 22:14:51 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 326570523de5 -r b47da767f704 templates/progops.c --- a/templates/progops.c Tue Jun 22 23:04:00 2021 +0200 +++ b/templates/progops.c Sun Jun 27 22:14:51 2021 +0200 @@ -1,6 +1,6 @@ /* Operations depending on program specifics. -Copyright (C) 2015, 2016, 2017, 2018, 2019 Paul Boddie +Copyright (C) 2015-2019, 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 @@ -171,6 +171,13 @@ __Raise(__new___builtins___core_UnderflowError(__NULL)); } +void __raise_value_error(__attr value) +{ +#ifdef __HAVE___builtins___exception_base_ValueError + __Raise(__new___builtins___exception_base_ValueError(__NULL, value)); +#endif /* __HAVE___builtins___exception_base_ValueError */ +} + void __raise_zero_division_error() { __Raise(__new___builtins___core_ZeroDivisionError(__NULL)); diff -r 326570523de5 -r b47da767f704 templates/progops.h --- a/templates/progops.h Tue Jun 22 23:04:00 2021 +0200 +++ b/templates/progops.h Sun Jun 27 22:14:51 2021 +0200 @@ -1,6 +1,6 @@ /* Operations depending on program specifics. -Copyright (C) 2015, 2016, 2017, 2018, 2019 Paul Boddie +Copyright (C) 2015-2019, 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 @@ -55,6 +55,7 @@ void __raise_overflow_error(); void __raise_unbound_method_error(); void __raise_underflow_error(); +void __raise_value_error(__attr value); void __raise_zero_division_error(); void __raise_type_error(); diff -r 326570523de5 -r b47da767f704 tests/int.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/int.py Sun Jun 27 22:14:51 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