# HG changeset patch # User Paul Boddie # Date 1480027339 -3600 # Node ID c048070e6a62f1b4dfc2299093c9e9b45fb435fb # Parent 2d23fd9e64ec4b854fb198548e9338c2c9e3b89d Raise TypeError when context tests fail. diff -r 2d23fd9e64ec -r c048070e6a62 generator.py --- a/generator.py Thu Nov 24 22:43:29 2016 +0100 +++ b/generator.py Thu Nov 24 23:42:19 2016 +0100 @@ -52,6 +52,7 @@ "A code generator." function_type = "__builtins__.core.function" + type_error_type = "__builtins__.core.TypeError" # NOTE: These must be synchronised with the library. @@ -348,10 +349,12 @@ #define __FUNCTION_TYPE %s #define __FUNCTION_INSTANCE_SIZE %s +#define __TYPE_ERROR_INSTANTIATOR %s #endif /* __PROGTYPES_H__ */""" % ( encode_path(self.function_type), - encode_size("", self.function_type) + encode_size("", self.function_type), + encode_instantiator_pointer(self.type_error_type) ) print >>f_signatures, """\ diff -r 2d23fd9e64ec -r c048070e6a62 lib/__builtins__/__init__.py --- a/lib/__builtins__/__init__.py Thu Nov 24 22:43:29 2016 +0100 +++ b/lib/__builtins__/__init__.py Thu Nov 24 23:42:19 2016 +0100 @@ -20,7 +20,8 @@ """ from __builtins__.core import function, get_using, object, type, \ - BaseException, Exception, Warning + BaseException, Exception, TypeError, \ + UnboundMethodInvocation, Warning # Exceptions. @@ -58,7 +59,6 @@ SystemError, SystemExit, TabError, - TypeError, UnboundLocalError, UnicodeDecodeError, UnicodeEncodeError, diff -r 2d23fd9e64ec -r c048070e6a62 lib/__builtins__/core.py --- a/lib/__builtins__/core.py Thu Nov 24 22:43:29 2016 +0100 +++ b/lib/__builtins__/core.py Thu Nov 24 23:42:19 2016 +0100 @@ -87,8 +87,14 @@ __repr__ = __str__ -class BaseException(object): pass +class BaseException: + + "The root of all exception types." + + pass + class Exception(BaseException): pass +class TypeError(Exception): pass class UnboundMethodInvocation(Exception): pass class Warning(object): pass diff -r 2d23fd9e64ec -r c048070e6a62 lib/__builtins__/exception/__init__.py --- a/lib/__builtins__/exception/__init__.py Thu Nov 24 22:43:29 2016 +0100 +++ b/lib/__builtins__/exception/__init__.py Thu Nov 24 23:42:19 2016 +0100 @@ -32,7 +32,6 @@ ReferenceError, StandardError, StopIteration, - TypeError, ValueError ) diff -r 2d23fd9e64ec -r c048070e6a62 lib/__builtins__/exception/base.py --- a/lib/__builtins__/exception/base.py Thu Nov 24 22:43:29 2016 +0100 +++ b/lib/__builtins__/exception/base.py Thu Nov 24 23:42:19 2016 +0100 @@ -3,7 +3,7 @@ """ Base exception objects. -Copyright (C) 2015 Paul Boddie +Copyright (C) 2015, 2016 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 @@ -27,7 +27,6 @@ class ReferenceError(Exception): pass class StandardError(Exception): pass class StopIteration(Exception): pass -class TypeError(Exception): pass class ValueError(Exception): pass # vim: tabstop=4 expandtab shiftwidth=4 diff -r 2d23fd9e64ec -r c048070e6a62 templates/ops.c --- a/templates/ops.c Thu Nov 24 22:43:29 2016 +0100 +++ b/templates/ops.c Thu Nov 24 23:42:19 2016 +0100 @@ -82,6 +82,14 @@ /* Attribute testing and retrieval operations. */ +static __attr __check_and_load_via_object_null(__ref obj, int pos, int code) +{ + if (__HASATTR(obj, pos, code)) + return __load_via_object(obj, pos); + else + return __NULL; +} + __attr __check_and_load_via_class(__ref obj, int pos, int code) { return __check_and_load_via_object(__get_class(obj), pos, code); @@ -89,12 +97,16 @@ __attr __check_and_load_via_object(__ref obj, int pos, int code) { - return __HASATTR(obj, pos, code) ? __load_via_object(obj, pos) : __NULL; + if (__HASATTR(obj, pos, code)) + return __load_via_object(obj, pos); + + __raise_type_error(); + return __NULL; } __attr __check_and_load_via_any(__ref obj, int pos, int code) { - __attr out = __check_and_load_via_object(obj, pos, code); + __attr out = __check_and_load_via_object_null(obj, pos, code); if (out.value == 0) out = __check_and_load_via_class(obj, pos, code); return out; @@ -132,8 +144,7 @@ if (!__is_instance(context) && __test_common_type(context, __TYPEPOS(attr.context), __TYPECODE(attr.context))) return __update_context(context, attr); - /* NOTE: An error may be more appropriate. */ - + __raise_type_error(); return __NULL; } diff -r 2d23fd9e64ec -r c048070e6a62 templates/progops.c --- a/templates/progops.c Thu Nov 24 22:43:29 2016 +0100 +++ b/templates/progops.c Thu Nov 24 23:42:19 2016 +0100 @@ -42,6 +42,15 @@ return attr; } +/* A helper for raising type errors within common operations. */ + +void __raise_type_error() +{ + __attr args[1]; + __attr exc = __TYPE_ERROR_INSTANTIATOR(args); + __Raise(exc); +} + /* Generic invocation operations. */ /* Invoke the given callable, supplying keyword argument details in the given