diff --git a/IPython/Magic.py b/IPython/Magic.py index 9045b79..b43f604 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -3143,7 +3143,9 @@ Defaulting color scheme to 'NoColor'""" opts,args = self.parse_options(parameter_s,'s:',mode='string') par = args.strip() sentinel = opts.get('s','--') - + + strip_from_start = [re.compile(e) for e in + ['^(.?>)+','^In \[\d+\]:','^\++']] from IPython import iplib lines = [] print "Pasting code; enter '%s' alone on the line to stop." % sentinel @@ -3151,7 +3153,11 @@ Defaulting color scheme to 'NoColor'""" l = iplib.raw_input_original(':') if l ==sentinel: break - lines.append(l.lstrip('>').lstrip('+')) + + for pat in strip_from_start: + l = pat.sub('',l) + lines.append(l) + block = "\n".join(lines) + '\n' #print "block:\n",block if not par: diff --git a/test/test_cpaste.ipy b/test/test_cpaste.ipy new file mode 100644 index 0000000..90811f7 --- /dev/null +++ b/test/test_cpaste.ipy @@ -0,0 +1,61 @@ +"""Test cpaste magic""" + +tests = {'pass': ["> > > run()", + ">>> > run()", + "+++ run()", + "++ run()"], + + 'fail': ["+ + run()", + " ++ run()"]} + +from StringIO import StringIO +import sys + +stdin_save = sys.stdin + +# NOTE: no blank lines allowed in function definition +def testcase(code,should_fail=False): + """Execute code via 'cpaste' and ensure it was executed, unless + should_fail is set. + + """ + _ip.user_ns['code_ran'] = False + # + src = StringIO() + src.write('\n') + src.write(code) + src.write('\n--\n') + src.seek(0) + # + sys.stdin = src + + try: + cpaste + except: + if not should_fail: + raise AssertionError("Failure not expected : '%s'" % + code) + else: + assert code_ran + if should_fail: + raise AssertionError("Failure expected : '%s'" % code) + # + finally: + sys.stdin = stdin_save + # + +def run(): + """Marker function: sets a flag when executed. + + """ + _ip.user_ns['code_ran'] = True + return 'run' # return string so '+ run()' doesn't result in success + + +### Actual testing happens here + +for code in tests['pass']: + testcase(code) + +for code in tests['fail']: + testcase(code,should_fail=True)