# readline/pyreadline
- pyreadlinew32_startup = os.path.join(
- sys.prefix, 'lib', 'site-packages',
- 'pyreadline', 'configuration', 'startup.py')
+ # From Bruce Edge:
+ # https://mail.python.org/pipermail/python-list/2001-March/062888.html
- if os.path.exists(pyreadlinew32_startup):
- execfile(pyreadlinew32_startup)
-
- else:
- # From Bruce Edge:
- # https://mail.python.org/pipermail/python-list/2001-March/062888.html
-
- try:
- import rlcompleter # noqa: need for completion
- import readline
- initfile = os.environ.get('INPUTRC') \
- or os.path.expanduser('~/.inputrc')
- readline.read_init_file(initfile)
-
- # if 'libedit' in readline.__doc__:
- # readline.parse_and_bind("bind ^I rl_complete")
- # else:
- # readline.parse_and_bind("tab: complete")
-
- histfiles = ['~/.python_history']
- # if 'VIRTUAL_ENV' in os.environ:
- # histfiles.append('$VIRTUAL_ENV/.python_history')
- for histfile in histfiles:
- try:
- histfile = os.path.expandvars(histfile)
- histfile = os.path.expanduser(histfile)
- readline.read_history_file(histfile)
- except IOError:
- pass # No such file
-
- def savehist():
- histsize = os.environ.get('HISTSIZE')
- if histsize:
- try:
- histsize = int(histsize)
- except ValueError:
- pass
- else:
- readline.set_history_length(histsize)
- histfile = histfiles[-1]
+ try:
+ import rlcompleter # noqa: need for completion
+ import readline
+ initfile = os.environ.get('INPUTRC') \
+ or os.path.expanduser('~/.inputrc')
+ readline.read_init_file(initfile)
+
+ # if 'libedit' in readline.__doc__:
+ # readline.parse_and_bind("bind ^I rl_complete")
+ # else:
+ # readline.parse_and_bind("tab: complete")
+
+ histfiles = ['~/.python_history']
+ # if 'VIRTUAL_ENV' in os.environ:
+ # histfiles.append('$VIRTUAL_ENV/.python_history')
+ for histfile in histfiles:
+ try:
histfile = os.path.expandvars(histfile)
histfile = os.path.expanduser(histfile)
+ readline.read_history_file(histfile)
+ except IOError:
+ pass # No such file
+
+ def savehist():
+ histsize = os.environ.get('HISTSIZE')
+ if histsize:
try:
- readline.write_history_file(histfile)
- except IOError:
+ histsize = int(histsize)
+ except ValueError:
pass
+ else:
+ readline.set_history_length(histsize)
+ histfile = histfiles[-1]
+ histfile = os.path.expandvars(histfile)
+ histfile = os.path.expanduser(histfile)
+ try:
+ readline.write_history_file(histfile)
+ except IOError:
+ pass
+
+ import atexit
+ atexit.register(savehist)
+ def unset_history():
import atexit
- atexit.register(savehist)
+ atexit._exithandlers = []
+ builtins.unset_history = unset_history
- except (ImportError, AttributeError):
- # no readline or atexit, or readline doesn't have
- # {read,write}_history_file - ignore the error
- pass
+ except (ImportError, AttributeError):
+ # no readline or atexit, or readline doesn't have
+ # {read,write}_history_file - ignore the error
+ pass
# terminal
term = os.environ.get('TERM', '')
- if 'linux' in term:
- background = 'dark'
+ for _term in ['cygwin', 'linux', 'putty']:
+ if _term in term:
+ background = 'dark'
+ break
else:
background = os.environ.get('BACKGROUND', 'light').lower()
# https://mail.python.org/pipermail/python-list/2001-March/112696.html
_term_found = False
- for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
+ for _term in ['cygwin', 'linux', 'putty', 'rxvt',
+ 'screen', 'term', 'vt100']:
if _term in term:
_term_found = True
break
def myinput(prompt=None):
save_stdout = sys.stdout
sys.stdout = sys.__stdout__
- try:
- result = builtin_input(prompt)
- except EOFError:
- result = None
+ result = builtin_input(prompt)
sys.stdout = save_stdout
return result
return "Usage: cat('filename')"
def __call__(self, filename):
- fp = open(filename, 'rU')
+ fp = open(filename, 'r')
text = fp.read()
fp.close()
print(text)