def check_cpaste(code, should_fail=False):
 ip.user_ns['code_ran'] = False
 src = StringIO()
 if (not hasattr(src, 'encoding')):
  src.encoding = None
 src.write(code)
 src.write('\n--\n')
 src.seek(0)
 stdin_save = sys.stdin
 sys.stdin = src
 try:
  context = (tt.AssertPrints if should_fail else tt.AssertNotPrints)
  with context('Traceback (most recent call last)'):
   ip.magic('cpaste')
  if (not should_fail):
   assert ip.user_ns['code_ran'], ('%r failed' % code)
 finally:
  sys.stdin = stdin_save

def check_cpaste ( code , should_fail = False ) : ip . user_ns [ 'code_ran' ] = False src = StringIO ( ) if ( not hasattr ( src , 'encoding' ) ) : src . encoding = None src . write ( code ) src . write ( '\n--\n' ) src . seek ( 0 ) stdin_save = sys . stdin sys . stdin = src try : context = ( tt . AssertPrints if should_fail else tt . AssertNotPrints ) with context ( 'Traceback (most recent call last)' ) : ip . magic ( 'cpaste' ) if ( not should_fail ) : assert ip . user_ns [ 'code_ran' ] , ( '%r failed' % code ) finally : sys . stdin = stdin_save
execute code via cpaste and ensure it was executed .