mirror of
https://github.com/python/cpython.git
synced 2024-11-24 10:24:35 +08:00
4118174315
svn+ssh://pythondev@svn.python.org/python/trunk ........ r64475 | raymond.hettinger | 2008-06-22 22:29:28 -0500 (Sun, 22 Jun 2008) | 1 line Issue 3161: Missing import and test. ........ r64544 | georg.brandl | 2008-06-26 16:12:55 -0500 (Thu, 26 Jun 2008) | 2 lines Use newer versions of externals. ........ r64545 | benjamin.peterson | 2008-06-26 16:23:30 -0500 (Thu, 26 Jun 2008) | 1 line add a htmlview directive ........ r64550 | brett.cannon | 2008-06-26 19:32:16 -0500 (Thu, 26 Jun 2008) | 2 lines Ignore .pyc and .pyo files. ........ r64557 | mark.dickinson | 2008-06-27 05:11:52 -0500 (Fri, 27 Jun 2008) | 3 lines Remove trailing 'L's from numerator and denominator in the repr() of a Fraction instance. ........ r64558 | mark.dickinson | 2008-06-27 06:03:21 -0500 (Fri, 27 Jun 2008) | 2 lines Add Jean Brouwers for his work on math.sum ........ r64565 | raymond.hettinger | 2008-06-27 16:34:24 -0500 (Fri, 27 Jun 2008) | 1 line Fix whitespace in example code. ........ r64570 | hyeshik.chang | 2008-06-27 20:04:31 -0500 (Fri, 27 Jun 2008) | 8 lines Give information for compililation of _multiprocessing.SemLock on FreeBSD: FreeBSD's P1003.1b semaphore support is highly experimental and it's disabled by default. Even if a user loads the experimental kernel module manually, _multiprocessing doesn't work correctly due to several known incompatibilities around sem_unlink and sem_getvalue, yet. ........ r64577 | raymond.hettinger | 2008-06-28 17:16:53 -0500 (Sat, 28 Jun 2008) | 1 line Issue 3230: Do not the set specific size macro. ........ r64582 | benjamin.peterson | 2008-06-28 18:06:05 -0500 (Sat, 28 Jun 2008) | 2 lines convert test_audioop to unittest. Thanks to Giampaolo Rodola. ........ r64583 | benjamin.peterson | 2008-06-28 18:06:49 -0500 (Sat, 28 Jun 2008) | 1 line rewrap ........ r64585 | benjamin.peterson | 2008-06-28 18:35:31 -0500 (Sat, 28 Jun 2008) | 1 line fix typo ........ r64590 | benjamin.peterson | 2008-06-29 08:43:07 -0500 (Sun, 29 Jun 2008) | 1 line reinstate the ending backtick. thanks Nick :) ........ r64592 | vinay.sajip | 2008-06-29 16:25:28 -0500 (Sun, 29 Jun 2008) | 2 lines Removed out-of-date comment in _install_handlers and used issubclass in place of equality comparison of classes. ........ r64593 | vinay.sajip | 2008-06-29 16:27:15 -0500 (Sun, 29 Jun 2008) | 1 line Updated to reflect change in logging.config to remove out-of-date comment in _install_handlers and the use of issubclass in place of equality comparison of classes. ........ r64625 | georg.brandl | 2008-07-01 14:59:00 -0500 (Tue, 01 Jul 2008) | 2 lines Add a link to PEP 324. ........ r64630 | georg.brandl | 2008-07-01 15:18:10 -0500 (Tue, 01 Jul 2008) | 2 lines #3216: fix Execute's parameter description. ........ r64638 | georg.brandl | 2008-07-01 15:50:02 -0500 (Tue, 01 Jul 2008) | 2 lines #1410739: add a footnote about "is" and "unusual" behavior. ........ r64647 | benjamin.peterson | 2008-07-01 18:33:06 -0500 (Tue, 01 Jul 2008) | 1 line add ABC to the glossary ........ r64655 | mark.dickinson | 2008-07-02 04:37:01 -0500 (Wed, 02 Jul 2008) | 7 lines Replace occurrences of '\d' with '[0-9]' in Decimal regex, to make sure that the behaviour of Decimal doesn't change if/when re.UNICODE becomes assumed in Python 3.0. Also add a check that alternative Unicode digits (e.g. u'\N{FULLWIDTH DIGIT ONE}') are *not* accepted in a numeric string. ........ r64656 | nick.coghlan | 2008-07-02 08:09:19 -0500 (Wed, 02 Jul 2008) | 1 line Issue 3190: pydoc now hides module __package__ attributes ........ r64663 | jesse.noller | 2008-07-02 11:44:09 -0500 (Wed, 02 Jul 2008) | 1 line Reenable the manager tests with Amaury's threading fix ........ r64664 | facundo.batista | 2008-07-02 11:52:55 -0500 (Wed, 02 Jul 2008) | 4 lines Issue #449227: Now with the rlcompleter module, callable objects are added a '(' when completed. ........
160 lines
5.3 KiB
Python
160 lines
5.3 KiB
Python
"""Word completion for GNU readline 2.0.
|
|
|
|
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 sys" type "sys.", hit the
|
|
completion key (twice), and see the list of names defined by the
|
|
sys module!
|
|
|
|
Tip: to use the tab key as the completion key, call
|
|
|
|
readline.parse_and_bind("tab: complete")
|
|
|
|
Notes:
|
|
|
|
- Exceptions raised by the completer function are *ignored* (and
|
|
generally cause the completion to fail). This is a feature -- since
|
|
readline sets the tty device in raw (or cbreak) mode, printing a
|
|
traceback wouldn't work well without some complicated hoopla to save,
|
|
reset and restore the tty state.
|
|
|
|
- The evaluation of the NAME.NAME... form may cause arbitrary
|
|
application defined code to be executed if an object with a
|
|
__getattr__ hook is found. Since it is the responsibility of the
|
|
application (or the user) to enable this feature, I consider this an
|
|
acceptable risk. More complicated expressions (e.g. function calls or
|
|
indexing operations) are *not* evaluated.
|
|
|
|
- When the original stdin is not a tty device, GNU readline is never
|
|
used, and this module (and the readline module) are silently inactive.
|
|
|
|
"""
|
|
|
|
import builtins
|
|
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'.
|
|
|
|
This is called successively with state == 0, 1, 2, ... until it
|
|
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)
|
|
else:
|
|
self.matches = self.global_matches(text)
|
|
try:
|
|
return self.matches[state]
|
|
except IndexError:
|
|
return None
|
|
|
|
def _callable_postfix(self, val, word):
|
|
if callable(val):
|
|
word = word + "("
|
|
return word
|
|
|
|
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
|
|
defined in self.namespace that match.
|
|
|
|
"""
|
|
import keyword
|
|
matches = []
|
|
n = len(text)
|
|
for word in keyword.kwlist:
|
|
if word[:n] == text:
|
|
matches.append(word)
|
|
for nspace in [builtins.__dict__, self.namespace]:
|
|
for word, val in nspace.items():
|
|
if word[:n] == text and word != "builtins":
|
|
matches.append(self._callable_postfix(val, word))
|
|
return matches
|
|
|
|
def attr_matches(self, text):
|
|
"""Compute matches when text contains a dot.
|
|
|
|
Assuming the text is of the form NAME.NAME....[NAME], and is
|
|
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 also considered.)
|
|
|
|
WARNING: this can still invoke arbitrary C code, if an object
|
|
with a __getattr__ hook is evaluated.
|
|
|
|
"""
|
|
import re
|
|
m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
|
|
if not m:
|
|
return []
|
|
expr, attr = m.group(1, 3)
|
|
try:
|
|
object = eval(expr, self.namespace)
|
|
except Exception:
|
|
return []
|
|
words = dir(object)
|
|
if hasattr(object,'__class__'):
|
|
words.append('__class__')
|
|
words = words + get_class_members(object.__class__)
|
|
matches = []
|
|
n = len(attr)
|
|
for word in words:
|
|
if word[:n] == attr and word != "__builtins__":
|
|
val = getattr(object, word)
|
|
word = self._callable_postfix(val, "%s.%s" % (expr, word))
|
|
matches.append(word)
|
|
return matches
|
|
|
|
def get_class_members(klass):
|
|
ret = dir(klass)
|
|
if hasattr(klass,'__bases__'):
|
|
for base in klass.__bases__:
|
|
ret = ret + get_class_members(base)
|
|
return ret
|
|
|
|
try:
|
|
import readline
|
|
except ImportError:
|
|
pass
|
|
else:
|
|
readline.set_completer(Completer().complete)
|