summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2015-11-24 00:10:45 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2015-11-24 00:10:45 (GMT)
commit11bb1ad15502849f126af3f35de7eaeebd617fc5 (patch)
tree640d8bc75c1950df2ef4da0039ca7013f963495f /Lib
parent1f847659f33ee3d51db3cb85cf408b5bba696347 (diff)
parented92910852ad7a3006bc264ef6cb061868e5a82c (diff)
downloadcpython-11bb1ad15502849f126af3f35de7eaeebd617fc5.zip
cpython-11bb1ad15502849f126af3f35de7eaeebd617fc5.tar.gz
cpython-11bb1ad15502849f126af3f35de7eaeebd617fc5.tar.bz2
Issue #25663: Merge rlcompleter fix from 3.4 into 3.5
Diffstat (limited to 'Lib')
-rw-r--r--Lib/rlcompleter.py7
-rw-r--r--Lib/test/test_rlcompleter.py21
2 files changed, 26 insertions, 2 deletions
diff --git a/Lib/rlcompleter.py b/Lib/rlcompleter.py
index be8aee0..378f5aa 100644
--- a/Lib/rlcompleter.py
+++ b/Lib/rlcompleter.py
@@ -103,13 +103,16 @@ class Completer:
"""
import keyword
matches = []
+ seen = {"__builtins__"}
n = len(text)
for word in keyword.kwlist:
if word[:n] == text:
+ seen.add(word)
matches.append(word)
- for nspace in [builtins.__dict__, self.namespace]:
+ for nspace in [self.namespace, builtins.__dict__]:
for word, val in nspace.items():
- if word[:n] == text and word != "__builtins__":
+ if word[:n] == text and word not in seen:
+ seen.add(word)
matches.append(self._callable_postfix(val, word))
return matches
diff --git a/Lib/test/test_rlcompleter.py b/Lib/test/test_rlcompleter.py
index 2022ed6..2d5d9c1 100644
--- a/Lib/test/test_rlcompleter.py
+++ b/Lib/test/test_rlcompleter.py
@@ -85,5 +85,26 @@ class TestRlcompleter(unittest.TestCase):
self.assertEqual(completer.complete('as', 2), 'assert')
self.assertEqual(completer.complete('an', 0), 'and')
+ def test_duplicate_globals(self):
+ namespace = {
+ 'False': None, # Keyword vs builtin vs namespace
+ 'assert': None, # Keyword vs namespace
+ 'try': lambda: None, # Keyword vs callable
+ 'memoryview': None, # Callable builtin vs non-callable
+ 'Ellipsis': lambda: None, # Non-callable builtin vs callable
+ }
+ completer = rlcompleter.Completer(namespace)
+ self.assertEqual(completer.complete('False', 0), 'False')
+ self.assertIsNone(completer.complete('False', 1)) # No duplicates
+ self.assertEqual(completer.complete('assert', 0), 'assert')
+ self.assertIsNone(completer.complete('assert', 1))
+ self.assertEqual(completer.complete('try', 0), 'try')
+ self.assertIsNone(completer.complete('try', 1))
+ # No opening bracket "(" because we overrode the built-in class
+ self.assertEqual(completer.complete('memoryview', 0), 'memoryview')
+ self.assertIsNone(completer.complete('memoryview', 1))
+ self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis(')
+ self.assertIsNone(completer.complete('Ellipsis', 1))
+
if __name__ == '__main__':
unittest.main()