From 06622ead8072f3602bba8cd1924f0897873ad8b1 Mon Sep 17 00:00:00 2001 From: Martin Panter Date: Fri, 13 Nov 2015 22:47:00 +0000 Subject: [PATCH] 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. --- Lib/rlcompleter.py | 17 ++++++++++------- Lib/test/test_rlcompleter.py | 13 +++++++++++++ Misc/NEWS | 3 +++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Lib/rlcompleter.py b/Lib/rlcompleter.py index d517c0e2d39..be8aee0f7e9 100644 --- a/Lib/rlcompleter.py +++ b/Lib/rlcompleter.py @@ -136,20 +136,23 @@ class Completer: return [] # get the content of the object, except __builtins__ - words = dir(thisobject) - if "__builtins__" in words: - words.remove("__builtins__") + words = set(dir(thisobject)) + words.discard("__builtins__") if hasattr(thisobject, '__class__'): - words.append('__class__') - words.extend(get_class_members(thisobject.__class__)) + words.add('__class__') + words.update(get_class_members(thisobject.__class__)) matches = [] n = len(attr) for word in words: - if word[:n] == attr and hasattr(thisobject, word): - val = getattr(thisobject, word) + if word[:n] == attr: + try: + val = getattr(thisobject, word) + except Exception: + continue # Exclude properties that are not set word = self._callable_postfix(val, "%s.%s" % (expr, word)) matches.append(word) + matches.sort() return matches def get_class_members(klass): diff --git a/Lib/test/test_rlcompleter.py b/Lib/test/test_rlcompleter.py index 2da7fce3f03..927df345348 100644 --- a/Lib/test/test_rlcompleter.py +++ b/Lib/test/test_rlcompleter.py @@ -64,6 +64,19 @@ class TestRlcompleter(unittest.TestCase): ['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_complete(self): completer = rlcompleter.Completer() self.assertEqual(completer.complete('', 0), '\t') diff --git a/Misc/NEWS b/Misc/NEWS index aacd51ea079..ec2b4afecd6 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -103,6 +103,9 @@ Core and Builtins Library ------- +- Issue #25590: In the Readline completer, only call getattr() once per + attribute. + - Issue #25498: Fix a crash when garbage-collecting ctypes objects created by wrapping a memoryview. This was a regression made in 3.4.3. Based on patch by Eryksun.