Skip to content

Commit fd0ba56

Browse files
JoBeGamingtanloong
authored andcommitted
Added default parameter to lookup_special in types.py
See https://discuss.python.org/t/expose-special-method-lookup-at-python-level/106236 for more.
1 parent 10ff83f commit fd0ba56

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

Lib/types.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
try:
1111
from _types import *
1212
except ImportError:
13+
import inspect
1314
import sys
1415

1516
def _f(): pass
@@ -79,7 +80,37 @@ def _m(self): pass
7980
# LazyImportType in pure Python cannot be guaranteed
8081
# without overriding the filter, so there is no fallback.
8182

82-
del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export
83+
def enclose_lookup_special():
84+
_sentinel = object()
85+
86+
def lookup_special(object, name, default=_sentinel):
87+
"""Lookup method name `name` on `object` skipping the instance
88+
dictionary.
89+
90+
`name` must be a string. If the named special attribute does not exist,
91+
`default` is returned if provided, otherwise AttributeError is raised.
92+
"""
93+
94+
cls = type(object)
95+
if not isinstance(name, str):
96+
raise TypeError(
97+
f"attribute name must be string, not '{type(name).__name__}'"
98+
)
99+
try:
100+
descr = inspect.getattr_static(cls, name)
101+
except AttributeError:
102+
if not default is _sentinel:
103+
return default
104+
raise
105+
if hasattr(descr, "__get__"):
106+
return descr.__get__(object, cls)
107+
return descr
108+
109+
return lookup_special
110+
111+
lookup_special = enclose_lookup_special()
112+
113+
del sys, inspect _f, _g, _C, _c, _ag, _cell_factory # Not for export
83114

84115
def lookup_special(object, name, *args):
85116
"""Lookup method name `name` on `object` skipping the instance

0 commit comments

Comments
 (0)