Skip to content

bpo-29630: don't run code on tab-completion #248

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 8 additions & 1 deletion Lib/rlcompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import atexit
import builtins
import __main__
from inspect import getattr_static

__all__ = ["Completer"]

Expand Down Expand Up @@ -170,7 +171,7 @@ def attr_matches(self, text):
not (noprefix and word[:n+1] == noprefix)):
match = "%s.%s" % (expr, word)
try:
val = getattr(thisobject, word)
val = getattr_static_prop(thisobject, word)
except Exception:
pass # Include even if attribute not set
else:
Expand All @@ -192,6 +193,12 @@ def get_class_members(klass):
ret = ret + get_class_members(base)
return ret

def getattr_static_prop(obj, attr):
val = getattr_static(obj, attr)
if isinstance(val, property):
return val
return getattr(obj, attr)

try:
import readline
except ImportError:
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_rlcompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def test_attr_matches(self):
['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
def test_no_code_execution_triggered(self):
# Ensure running the completer won't invoke property getters
class Foo:
calls = 0
@property
Expand All @@ -91,7 +91,7 @@ def bar(self):
f = Foo()
completer = rlcompleter.Completer(dict(f=f))
self.assertEqual(completer.complete('f.b', 0), 'f.bar')
self.assertEqual(f.calls, 1)
self.assertEqual(f.calls, 0)

def test_uncreated_attr(self):
# Attributes like properties and slots should be completed even when
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ Vincent Delft
Arnaud Delobelle
Konrad Delong
Erik Demaine
Francisco Demartino
Martin Dengler
John Dennis
L. Peter Deutsch
Expand Down