##// END OF EJS Templates
Lots of work on exception handling, including tests for traceback printing....
Lots of work on exception handling, including tests for traceback printing. We finally have some tests for various exception mode printing, via doctests that exercise all three modes! Also changed handling of sys.exit(X) to only print the summary message, as SystemExit is most often a 'handled' exception. It can still be 100% silenced via '%run -e', but now it's much less intrusive. Added a new %tb magic to print the last available traceback with the current xmode. One can then re-print the last traceback with more detail if desired, without having to cause it again.

File last commit:

r2256:b7bb7522
r2440:0caaf43a
Show More
splitinput.py
83 lines | 2.6 KiB | text/x-python | PythonLexer
Brian Granger
More work on refactoring things into components....
r2244 #!/usr/bin/env python
# encoding: utf-8
"""
Simple utility for splitting user input.
Authors:
* Brian Granger
* Fernando Perez
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2009 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import re
#-----------------------------------------------------------------------------
# Main function
#-----------------------------------------------------------------------------
# RegExp for splitting line contents into pre-char//first word-method//rest.
# For clarity, each group in on one line.
# WARNING: update the regexp if the escapes in iplib are changed, as they
# are hardwired in.
# Although it's not solely driven by the regex, note that:
# ,;/% only trigger if they are the first character on the line
# ! and !! trigger if they are first char(s) *or* follow an indent
# ? triggers as first or last char.
# The three parts of the regex are:
# 1) pre: pre_char *or* initial whitespace
# 2) ifun: first word/method (mix of \w and '.')
# 3) the_rest: rest of line (separated from ifun by space if non-empty)
line_split = re.compile(r'^([,;/%?]|!!?|\s*)'
r'\s*([\w\.]+)'
r'(\s+.*$|$)')
Brian Granger
First go an implementing a=!ls and a=%who syntax....
r2256 # r'[\w\.]+'
# r'\s*=\s*%.*'
Brian Granger
More work on refactoring things into components....
r2244
def split_user_input(line, pattern=None):
Brian Granger
First go an implementing a=!ls and a=%who syntax....
r2256 """Split user input into pre-char/whitespace, function part and rest.
This is currently handles lines with '=' in them in a very inconsistent
manner.
"""
Brian Granger
More work on refactoring things into components....
r2244
if pattern is None:
pattern = line_split
match = pattern.match(line)
if not match:
Brian Granger
First go an implementing a=!ls and a=%who syntax....
r2256 # print "match failed for line '%s'" % line
Brian Granger
More work on refactoring things into components....
r2244 try:
ifun, the_rest = line.split(None,1)
except ValueError:
Brian Granger
First go an implementing a=!ls and a=%who syntax....
r2256 # print "split failed for line '%s'" % line
Brian Granger
More work on refactoring things into components....
r2244 ifun, the_rest = line,''
pre = re.match('^(\s*)(.*)',line).groups()[0]
else:
pre,ifun,the_rest = match.groups()
# ifun has to be a valid python identifier, so it better be only pure
# ascii, no unicode:
try:
ifun = ifun.encode('ascii')
except UnicodeEncodeError:
the_rest = ifun + u' ' + the_rest
ifun = u''
#print 'line:<%s>' % line # dbg
#print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
return pre, ifun.strip(), the_rest.lstrip()