MCPcopy Index your code
hub / github.com/python/cpython / test_special_method_lookup

Method test_special_method_lookup

Lib/test/test_descr.py:2190–2292  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2188 self.assertEqual(cm.exception.args[0], expected_errmsg)
2189
2190 def test_special_method_lookup(self):
2191 # The lookup of special methods bypasses __getattr__ and
2192 # __getattribute__, but they still can be descriptors.
2193
2194 def run_context(manager):
2195 with manager:
2196 pass
2197 def iden(self):
2198 return self
2199 def hello(self):
2200 return b"hello"
2201 def empty_seq(self):
2202 return []
2203 def zero(self):
2204 return 0
2205 def complex_num(self):
2206 return 1j
2207 def stop(self):
2208 raise StopIteration
2209 def return_true(self, thing=None):
2210 return True
2211 def do_isinstance(obj):
2212 return isinstance(int, obj)
2213 def do_issubclass(obj):
2214 return issubclass(int, obj)
2215 def do_dict_missing(checker):
2216 class DictSub(checker.__class__, dict):
2217 pass
2218 self.assertEqual(DictSub()["hi"], 4)
2219 def some_number(self_, key):
2220 self.assertEqual(key, "hi")
2221 return 4
2222 def swallow(*args): pass
2223 def format_impl(self, spec):
2224 return "hello"
2225
2226 # It would be nice to have every special method tested here, but I'm
2227 # only listing the ones I can remember outside of typeobject.c, since it
2228 # does it right.
2229 specials = [
2230 ("__bytes__", bytes, hello, set(), {}),
2231 ("__reversed__", reversed, empty_seq, set(), {}),
2232 ("__length_hint__", list, zero, set(),
2233 {"__iter__" : iden, "__next__" : stop}),
2234 ("__sizeof__", sys.getsizeof, zero, set(), {}),
2235 ("__instancecheck__", do_isinstance, return_true, set(), {}),
2236 ("__missing__", do_dict_missing, some_number,
2237 set(("__class__",)), {}),
2238 ("__subclasscheck__", do_issubclass, return_true,
2239 set(("__bases__",)), {}),
2240 ("__enter__", run_context, iden, set(), {"__exit__" : swallow}),
2241 ("__exit__", run_context, swallow, set(), {"__enter__" : iden}),
2242 ("__complex__", complex, complex_num, set(), {}),
2243 ("__format__", format, format_impl, set(), {}),
2244 ("__floor__", math.floor, zero, set(), {}),
2245 ("__trunc__", math.trunc, zero, set(), {}),
2246 ("__ceil__", math.ceil, zero, set(), {}),
2247 ("__dir__", dir, empty_seq, set(), {}),

Callers

nothing calls this directly

Calls 8

setFunction · 0.85
runnerFunction · 0.85
SpecialDescrClass · 0.85
ErrDescrClass · 0.85
XClass · 0.70
itemsMethod · 0.45
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected