Issue #25590: Make rlcompleter only call getattr() once per attribute
Previously it was called another time via hasattr(), and both calls were
made once for dir(f) and again for dir(f.__class__). This includes a
backport of changing from a list to a set from revision 4dbb315fe667.
diff --git a/Lib/test/test_rlcompleter.py b/Lib/test/test_rlcompleter.py
index ac0e70d..c969d27 100644
--- a/Lib/test/test_rlcompleter.py
+++ b/Lib/test/test_rlcompleter.py
@@ -65,6 +65,19 @@
['egg.{}('.format(x) for x in dir(str)
if x.startswith('s')])
+ def test_excessive_getattr(self):
+ # Ensure getattr() is invoked no more than once per attribute
+ class Foo:
+ calls = 0
+ @property
+ def bar(self):
+ self.calls += 1
+ return None
+ f = Foo()
+ completer = rlcompleter.Completer(dict(f=f))
+ self.assertEqual(completer.complete('f.b', 0), 'f.bar')
+ self.assertEqual(f.calls, 1)
+
def test_main():
support.run_unittest(TestRlcompleter)