rlineimpl.py
55 lines
| 1.7 KiB
| text/x-python
|
PythonLexer
vivainio
|
r503 | # -*- coding: utf-8 -*- | ||
fperez
|
r644 | """ Imports and provides the 'correct' version of readline for the platform. | ||
vivainio
|
r503 | |||
Brian Granger
|
r2044 | Readline is used throughout IPython as 'import IPython.utils.rlineimpl as readline'. | ||
vivainio
|
r503 | |||
fperez
|
r644 | In addition to normal readline stuff, this module provides have_readline | ||
boolean and _outputfile variable used in genutils. | ||||
Fernando Perez
|
r1853 | """ | ||
vivainio
|
r503 | |||
import sys | ||||
fperez
|
r644 | try: | ||
from readline import * | ||||
import readline as _rl | ||||
have_readline = True | ||||
except ImportError: | ||||
vivainio
|
r671 | try: | ||
from pyreadline import * | ||||
import pyreadline as _rl | ||||
have_readline = True | ||||
except ImportError: | ||||
have_readline = False | ||||
vivainio
|
r503 | |||
fperez
|
r644 | if sys.platform == 'win32' and have_readline: | ||
vivainio
|
r503 | try: | ||
fperez
|
r644 | _outputfile=_rl.GetOutputFile() | ||
vivainio
|
r671 | except AttributeError: | ||
fperez
|
r644 | print "Failed GetOutputFile" | ||
have_readline = False | ||||
bgranger
|
r884 | |||
# Test to see if libedit is being used instead of GNU readline. | ||||
# Thanks to Boyd Waters for this patch. | ||||
uses_libedit = False | ||||
if sys.platform == 'darwin' and have_readline: | ||||
bgranger
|
r890 | import commands | ||
(status, result) = commands.getstatusoutput( "otool -L %s | grep libedit" % _rl.__file__ ) | ||||
if status == 0 and len(result) > 0: | ||||
# we are bound to libedit - new in Leopard | ||||
_rl.parse_and_bind("bind ^I rl_complete") | ||||
print "Leopard libedit detected." | ||||
uses_libedit = True | ||||
bgranger
|
r884 | |||
fperez
|
r644 | # the clear_history() function was only introduced in Python 2.4 and is | ||
# actually optional in the readline API, so we must explicitly check for its | ||||
# existence. Some known platforms actually don't have it. This thread: | ||||
# http://mail.python.org/pipermail/python-dev/2003-August/037845.html | ||||
# has the original discussion. | ||||
if have_readline: | ||||
vivainio
|
r503 | try: | ||
vivainio
|
r671 | _rl.clear_history | ||
fperez
|
r644 | except AttributeError: | ||
def clear_history(): pass | ||||
Fernando Perez
|
r1853 | _rl.clear_history = clear_history | ||