Skip to content

Commit 89dde8f

Browse files
committed
Extract safe_getattr into a separate function
1 parent a0c57a8 commit 89dde8f

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

Lib/_pyrepl/fancycompleter.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
import keyword
99
import types
1010

11+
12+
def safe_getattr(obj, name):
13+
# Mirror rlcompleter's safeguards so completion does not
14+
# call properties or reify lazy module attributes.
15+
if isinstance(getattr(type(obj), name, None), property):
16+
return None
17+
if (isinstance(obj, types.ModuleType)
18+
and isinstance(obj.__dict__.get(name), types.LazyImportType)
19+
):
20+
return obj.__dict__.get(name)
21+
return getattr(obj, name, None)
22+
23+
1124
class Completer(rlcompleter.Completer):
1225
"""
1326
When doing something like a.b.<tab>, keep the full a.b.attr completion
@@ -143,21 +156,7 @@ def _attr_matches(self, text):
143156
word[:n] == attr
144157
and not (noprefix and word[:n+1] == noprefix)
145158
):
146-
# Mirror rlcompleter's safeguards so completion does not
147-
# call properties or reify lazy module attributes.
148-
if isinstance(getattr(type(thisobject), word, None), property):
149-
value = None
150-
elif (
151-
isinstance(thisobject, types.ModuleType)
152-
and isinstance(
153-
thisobject.__dict__.get(word),
154-
types.LazyImportType,
155-
)
156-
):
157-
value = thisobject.__dict__.get(word)
158-
else:
159-
value = getattr(thisobject, word, None)
160-
159+
value = safe_getattr(thisobject, word)
161160
names.append(word)
162161
values.append(value)
163162
if names or not noprefix:

0 commit comments

Comments
 (0)