Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
dbab3e31
Commit
dbab3e31
authored
Mar 23, 2002
by
Neil Schemenauer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add namespace selection for rlcompleter. Closes SF patch 490026.
parent
62c06ba6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
12 deletions
+40
-12
Lib/rlcompleter.py
Lib/rlcompleter.py
+40
-12
No files found.
Lib/rlcompleter.py
View file @
dbab3e31
"""Word completion for GNU readline 2.0.
This requires the latest extension to the readline module
(the
completes keywords, built-ins and globals in
__main__; when completing
NAME.NAME..., it evaluates (!) the expression up to the last dot and
completes its attributes.
This requires the latest extension to the readline module
. The completer
completes keywords, built-ins and globals in
a selectable namespace (which
defaults to __main__); when completing NAME.NAME..., it evaluates (!) the
expression up to the last dot and
completes its attributes.
It's very cool to do "import string" type "string.", hit the
completion key (twice), and see the list of names defined by the
...
...
@@ -46,6 +46,32 @@ import __main__
__all__
=
[
"Completer"
]
class
Completer
:
def
__init__
(
self
,
namespace
=
None
):
"""Create a new completer for the command line.
Completer([namespace]) -> completer instance.
If unspecified, the default namespace where completions are performed
is __main__ (technically, __main__.__dict__). Namespaces should be
given as dictionaries.
Completer instances should be used as the completion mechanism of
readline via the set_completer() call:
readline.set_completer(Completer(my_namespace).complete)
"""
if
namespace
and
not
isinstance
(
namespace
,
dict
):
raise
TypeError
,
'namespace must be a dictionary'
# Don't bind to namespace quite yet, but flag whether the user wants a
# specific namespace or to use __main__.__dict__. This will allow us
# to bind to __main__.__dict__ at completion time, not now.
if
namespace
is
None
:
self
.
use_main_ns
=
1
else
:
self
.
use_main_ns
=
0
self
.
namespace
=
namespace
def
complete
(
self
,
text
,
state
):
"""Return the next possible completion for 'text'.
...
...
@@ -54,6 +80,9 @@ class Completer:
returns None. The completion should begin with 'text'.
"""
if
self
.
use_main_ns
:
self
.
namespace
=
__main__
.
__dict__
if
state
==
0
:
if
"."
in
text
:
self
.
matches
=
self
.
attr_matches
(
text
)
...
...
@@ -67,8 +96,8 @@ class Completer:
def
global_matches
(
self
,
text
):
"""Compute matches when text is a simple name.
Return a list of all keywords, built-in functions and names
currently defines in __main__
that match.
Return a list of all keywords, built-in functions and names
currently
defined in self.namespace
that match.
"""
import
keyword
...
...
@@ -76,7 +105,7 @@ class Completer:
n
=
len
(
text
)
for
list
in
[
keyword
.
kwlist
,
__builtin__
.
__dict__
.
keys
(),
__main__
.
__dict__
.
keys
()]:
self
.
namespace
.
keys
()]:
for
word
in
list
:
if
word
[:
n
]
==
text
and
word
!=
"__builtins__"
:
matches
.
append
(
word
)
...
...
@@ -86,10 +115,9 @@ class Completer:
"""Compute matches when text contains a dot.
Assuming the text is of the form NAME.NAME....[NAME], and is
evaluatable in the globals of __main__, it will be evaluated
and its attributes (as revealed by dir()) are used as possible
completions. (For class instances, class members are are also
considered.)
evaluatable in self.namespace, it will be evaluated and its attributes
(as revealed by dir()) are used as possible completions. (For class
instances, class members are are also considered.)
WARNING: this can still invoke arbitrary C code, if an object
with a __getattr__ hook is evaluated.
...
...
@@ -100,7 +128,7 @@ class Completer:
if not m:
return
expr, attr = m.group(1, 3)
object = eval(expr,
__main__.__dict__
)
object = eval(expr,
self.namespace
)
words = dir(object)
if hasattr(object,'__class__'):
words.append('__class__')
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment