##// END OF EJS Templates
Backport PR #2738: Unicode content crashes the pager (console)...
Backport PR #2738: Unicode content crashes the pager (console) We've run into an interesting bug in the astropy project. https://github.com/astropy/astropy/issues/600 When displaying a docstring that contains Unicode and is also long enough that it gets sent to the pager it fails since the docstring can't be sent to the pager as ascii. This crashes in the middle of sending content to the pager, so the shell ends up in an inconsistent state and stops echoing the keyboard etc. The fix (attached) is merely to encode the content sent to the pager in the same encoding as the terminal (`sys.stdout.encoding`). Strictly speaking, this isn't always the right thing to do, since the pager may be configured to expect a different encoding than the terminal, but that is sort of an irrational way to configure a machine... ;) For example, `less`, in the absence of any special environment variables to tell it otherwise, uses the standard `LC*` environment variables to determine what to do, which should be the same mechanism the terminal also uses by default. If anyone can suggest a better fix, I'm all for it. Perhaps it should be configurable, defaulting to `sys.stdout.encoding`?

File last commit:

r4872:34c10438
r9853:7f9a133e
Show More
envpersist.py
92 lines | 2.4 KiB | text/x-python | PythonLexer
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 # -*- coding: utf-8 -*-
vivainio
docstring update
r638 """ %env magic command for storing environment variables persistently
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 """
Brian Granger
ipapi.py => core/ipapi.py and imports updated.
r2027 from IPython.core import ipapi
Brian Granger
Continuing a massive refactor of everything.
r2205 from IPython.core.error import TryNext
Brian Granger
ipapi.py => core/ipapi.py and imports updated.
r2027 ip = ipapi.get()
vivainio
envpersist.py extension - boosts %env to remember env variables
r508
import os,sys
Bernardo B. Marques
remove all trailling spaces
r4872 def restore_env(self):
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 ip = self.getapi()
vivainio
sanitize %env options, add prepend and print
r509 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 for k,v in env['set'].items():
os.environ[k] = v
for k,v in env['add']:
os.environ[k] = os.environ.get(k,"") + v
vivainio
sanitize %env options, add prepend and print
r509 for k,v in env['pre']:
os.environ[k] = v + os.environ.get(k,"")
Brian Granger
Continuing a massive refactor of everything.
r2205 raise TryNext
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 ip.set_hook('late_startup_hook', restore_env)
def persist_env(self, parameter_s=''):
""" Store environment variables persistently
Bernardo B. Marques
remove all trailling spaces
r4872
IPython remembers the values across sessions, which is handy to avoid
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 editing startup files.
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 %env - Show all environment variables
%env VISUAL=jed - set VISUAL to jed
%env PATH+=;/foo - append ;foo to PATH
vivainio
sanitize %env options, add prepend and print
r509 %env PATH+=;/bar - also append ;bar to PATH
vivainio
typofix
r510 %env PATH-=/wbin; - prepend /wbin; to PATH
vivainio
sanitize %env options, add prepend and print
r509 %env -d VISUAL - forget VISUAL persistent val
Bernardo B. Marques
remove all trailling spaces
r4872 %env -p - print all persistent env modifications
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 """
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 if not parameter_s.strip():
return os.environ.data
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 ip = self.getapi()
db = ip.db
vivainio
sanitize %env options, add prepend and print
r509 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
vivainio
envpersist.py extension - boosts %env to remember env variables
r508
vivainio
sanitize %env options, add prepend and print
r509 if parameter_s.startswith('-p'):
return env
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
sanitize %env options, add prepend and print
r509 elif parameter_s.startswith('-d'):
parts = (parameter_s.split()[1], '<del>')
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
sanitize %env options, add prepend and print
r509 else:
Bernardo B. Marques
remove all trailling spaces
r4872 parts = parameter_s.strip().split('=')
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 if len(parts) == 2:
vivainio
%env 'append on set' bug fixed, strip spaces to allow %env A = 10 instead of %env A=10
r693 k,v = [p.strip() for p in parts]
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
sanitize %env options, add prepend and print
r509 if v == '<del>':
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 if k in env['set']:
del env['set'][k]
vivainio
sanitize %env options, add prepend and print
r509 env['add'] = [el for el in env['add'] if el[0] != k]
env['pre'] = [el for el in env['pre'] if el[0] != k]
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
sanitize %env options, add prepend and print
r509 print "Forgot '%s' (for next session)" % k
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 elif k.endswith('+'):
k = k[:-1]
env['add'].append((k,v))
os.environ[k] += v
vivainio
sanitize %env options, add prepend and print
r509 print k,"after append =",os.environ[k]
elif k.endswith('-'):
k = k[:-1]
env['pre'].append((k,v))
os.environ[k] = v + os.environ.get(k,"")
print k,"after prepend =",os.environ[k]
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 else:
env['set'][k] = v
print "Setting",k,"to",v
vivainio
%env 'append on set' bug fixed, strip spaces to allow %env A = 10 instead of %env A=10
r693 os.environ[k] = v
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
envpersist.py extension - boosts %env to remember env variables
r508 db['stored_env'] = env
vivainio
%env completer
r793 def env_completer(self,event):
""" Custom completer that lists all env vars """
return os.environ.keys()
vivainio
envpersist.py extension - boosts %env to remember env variables
r508
Brian Granger
Continuing a massive refactor of everything.
r2205 ip.define_magic('env', persist_env)
vivainio
%env completer
r793 ip.set_hook('complete_command',env_completer, str_key = '%env')