|
10 | 10 | from codecs import BOM_UTF8 |
11 | 11 | from itertools import product |
12 | 12 | from textwrap import dedent |
| 13 | +from types import ModuleType |
13 | 14 |
|
14 | 15 | from test.support import (captured_stderr, check_impl_detail, |
15 | 16 | cpython_only, gc_collect, |
@@ -2054,6 +2055,129 @@ def blech(self): |
2054 | 2055 | self.assertEqual("bluch", exc.name) |
2055 | 2056 | self.assertEqual(obj, exc.obj) |
2056 | 2057 |
|
| 2058 | + def test_getattr_error_message(self): |
| 2059 | + def fqn(type): |
| 2060 | + return f'{type.__module__}.{type.__qualname__}' |
| 2061 | + |
| 2062 | + class RaiseWithName: |
| 2063 | + def __getattr__(self, name): |
| 2064 | + raise AttributeError(name) |
| 2065 | + obj = RaiseWithName() |
| 2066 | + with self.assertRaises(AttributeError) as cm: |
| 2067 | + getattr(obj, "missing1") |
| 2068 | + self.assertEqual(str(cm.exception), |
| 2069 | + f"'{fqn(RaiseWithName)}' object has no attribute 'missing1'") |
| 2070 | + self.assertIs(cm.exception.obj, obj) |
| 2071 | + self.assertEqual(cm.exception.name, "missing1") |
| 2072 | + |
| 2073 | + class BareRaise: |
| 2074 | + def __getattr__(self, name): |
| 2075 | + raise AttributeError |
| 2076 | + obj = BareRaise() |
| 2077 | + with self.assertRaises(AttributeError) as cm: |
| 2078 | + getattr(obj, "missing2") |
| 2079 | + self.assertEqual(str(cm.exception), |
| 2080 | + f"'{fqn(BareRaise)}' object has no attribute 'missing2'") |
| 2081 | + self.assertIs(cm.exception.obj, obj) |
| 2082 | + self.assertEqual(cm.exception.name, "missing2") |
| 2083 | + |
| 2084 | + class RaiseCustom: |
| 2085 | + def __getattr__(self, name): |
| 2086 | + raise AttributeError("custom") |
| 2087 | + obj = RaiseCustom() |
| 2088 | + with self.assertRaises(AttributeError) as cm: |
| 2089 | + getattr(obj, "missing3") |
| 2090 | + self.assertEqual(str(cm.exception), "custom") |
| 2091 | + self.assertIs(cm.exception.obj, obj) |
| 2092 | + self.assertEqual(cm.exception.name, "missing3") |
| 2093 | + |
| 2094 | + def test_class_getattr_error_message(self): |
| 2095 | + def fqn(type): |
| 2096 | + return f'{type.__module__}.{type.__qualname__}' |
| 2097 | + |
| 2098 | + class MetaclassRaiseWithName(type): |
| 2099 | + def __getattr__(self, name): |
| 2100 | + raise AttributeError(name) |
| 2101 | + cls = MetaclassRaiseWithName("spam", (), {}) |
| 2102 | + with self.assertRaises(AttributeError) as cm: |
| 2103 | + getattr(cls, "missing1") |
| 2104 | + self.assertEqual(str(cm.exception), |
| 2105 | + f"type object '{fqn(cls)}' has no attribute 'missing1'") |
| 2106 | + self.assertIs(cm.exception.obj, cls) |
| 2107 | + self.assertEqual(cm.exception.name, "missing1") |
| 2108 | + |
| 2109 | + class MetaclassBareRaise(type): |
| 2110 | + def __getattr__(self, name): |
| 2111 | + raise AttributeError |
| 2112 | + cls = MetaclassBareRaise("eggs", (), {}) |
| 2113 | + with self.assertRaises(AttributeError) as cm: |
| 2114 | + getattr(cls, "missing2") |
| 2115 | + self.assertEqual(str(cm.exception), |
| 2116 | + f"type object '{fqn(cls)}' has no attribute 'missing2'") |
| 2117 | + self.assertIs(cm.exception.obj, cls) |
| 2118 | + self.assertEqual(cm.exception.name, "missing2") |
| 2119 | + |
| 2120 | + class MetaclassRaiseCustom(type): |
| 2121 | + def __getattr__(self, name): |
| 2122 | + raise AttributeError("custom") |
| 2123 | + cls = MetaclassRaiseCustom("ham", (), {}) |
| 2124 | + with self.assertRaises(AttributeError) as cm: |
| 2125 | + getattr(cls, "missing3") |
| 2126 | + self.assertEqual(str(cm.exception), "custom") |
| 2127 | + self.assertIs(cm.exception.obj, cls) |
| 2128 | + self.assertEqual(cm.exception.name, "missing3") |
| 2129 | + |
| 2130 | + def test_module_getattr_error_message(self): |
| 2131 | + raisewithname_mod = ModuleType("raisewithname") |
| 2132 | + def raise_with_name(name): |
| 2133 | + raise AttributeError(name) |
| 2134 | + raisewithname_mod.__getattr__ = raise_with_name |
| 2135 | + with self.assertRaises(AttributeError) as cm: |
| 2136 | + getattr(raisewithname_mod, "missing1") |
| 2137 | + self.assertEqual(str(cm.exception), |
| 2138 | + "module 'raisewithname' has no attribute 'missing1'") |
| 2139 | + self.assertIs(cm.exception.obj, raisewithname_mod) |
| 2140 | + self.assertEqual(cm.exception.name, "missing1") |
| 2141 | + |
| 2142 | + bareraise_mod = ModuleType("bareraise") |
| 2143 | + def bare_raise(name): |
| 2144 | + raise AttributeError |
| 2145 | + bareraise_mod.__getattr__ = bare_raise |
| 2146 | + with self.assertRaises(AttributeError) as cm: |
| 2147 | + getattr(bareraise_mod, "missing2") |
| 2148 | + self.assertEqual(str(cm.exception), |
| 2149 | + "module 'bareraise' has no attribute 'missing2'") |
| 2150 | + self.assertIs(cm.exception.obj, bareraise_mod) |
| 2151 | + self.assertEqual(cm.exception.name, "missing2") |
| 2152 | + |
| 2153 | + custom_mod = ModuleType("custom") |
| 2154 | + def raise_custom(name): |
| 2155 | + raise AttributeError("custom") |
| 2156 | + custom_mod.__getattr__ = raise_custom |
| 2157 | + with self.assertRaises(AttributeError) as cm: |
| 2158 | + getattr(custom_mod, "missing3") |
| 2159 | + self.assertEqual(str(cm.exception), "custom") |
| 2160 | + self.assertIs(cm.exception.obj, custom_mod) |
| 2161 | + self.assertEqual(cm.exception.name, "missing3") |
| 2162 | + |
| 2163 | + nameless_mod = ModuleType("forgettable") |
| 2164 | + del nameless_mod.__dict__["__name__"] |
| 2165 | + nameless_mod.__getattr__ = raise_with_name |
| 2166 | + with self.assertRaises(AttributeError) as cm: |
| 2167 | + getattr(nameless_mod, "missing4") |
| 2168 | + self.assertEqual(str(cm.exception), "module has no attribute 'missing4'") |
| 2169 | + self.assertIs(cm.exception.obj, nameless_mod) |
| 2170 | + self.assertEqual(cm.exception.name, "missing4") |
| 2171 | + |
| 2172 | + nameless_mod = ModuleType("broken") |
| 2173 | + nameless_mod.__dict__["__name__"] = 10j |
| 2174 | + nameless_mod.__getattr__ = raise_with_name |
| 2175 | + with self.assertRaises(AttributeError) as cm: |
| 2176 | + getattr(nameless_mod, "missing4") |
| 2177 | + self.assertEqual(str(cm.exception), "module has no attribute 'missing4'") |
| 2178 | + self.assertIs(cm.exception.obj, nameless_mod) |
| 2179 | + self.assertEqual(cm.exception.name, "missing4") |
| 2180 | + |
2057 | 2181 | # Note: name suggestion tests live in `test_traceback`. |
2058 | 2182 |
|
2059 | 2183 |
|
|
0 commit comments