Skip to content

bpo-44752: Make rlcompleter not call @property methods #27401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/rlcompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ def attr_matches(self, text):
if (word[:n] == attr and
not (noprefix and word[:n+1] == noprefix)):
match = "%s.%s" % (expr, word)
if isinstance(getattr(type(thisobject), word, None),
property):
# bpo-44752: thisobject.word is a method decorated by
# `@property`. What follows applies a postfix if
# thisobject.word is callable, but know we know that
# this is not callable (because it is a property).
# Also, getattr(thisobject, word) will evaluate the
# property method, which is not desirable.
matches.append(match)
continue
try:
val = getattr(thisobject, word)
except Exception:
Expand Down
32 changes: 28 additions & 4 deletions Lib/test/test_rlcompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,41 @@ def test_attr_matches(self):
if x.startswith('s')])

def test_excessive_getattr(self):
# Ensure getattr() is invoked no more than once per attribute
"""Ensure getattr() is invoked no more than once per attribute"""

# note the special case for @property methods below; that is why
# we use __dir__ and __getattr__ in class Foo to create a "magic"
# class attribute 'bar'. This forces `getattr` to call __getattr__
# (which is doesn't necessarily do).
class Foo:
calls = 0
bar = ''
def __getattribute__(self, name):
if name == 'bar':
self.calls += 1
return None
return super().__getattribute__(name)

f = Foo()
completer = rlcompleter.Completer(dict(f=f))
self.assertEqual(completer.complete('f.b', 0), 'f.bar')
self.assertEqual(f.calls, 1)

def test_property_method_not_called(self):
class Foo:
_bar = 0
property_called = False

@property
def bar(self):
self.calls += 1
return None
self.property_called = True
return self._bar

f = Foo()
completer = rlcompleter.Completer(dict(f=f))
self.assertEqual(completer.complete('f.b', 0), 'f.bar')
self.assertEqual(f.calls, 1)
self.assertFalse(f.property_called)


def test_uncreated_attr(self):
# Attributes like properties and slots should be completed even when
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`rcompleter` does not call :func:`getattr` on :class:`property` objects
to avoid the side-effect of evaluating the corresponding method.