def check_cpaste(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() if not hasattr(src, 'encoding'): # IPython expects stdin to have an encoding attribute 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 testEncodingMirrors(self): """ The encoding of a stream gets mirrored through """ s = StringIO() encoding = "aoeu" try: encoding = s.encoding except: s.encoding = encoding gs = GreenStream(s) self.assertEqual(gs.encoding, encoding)