MCPcopy Create free account
hub / github.com/ipython/ipython / check_cpaste

Function check_cpaste

IPython/core/tests/test_magic_terminal.py:22–47  ·  view source on GitHub ↗

Execute code via 'cpaste' and ensure it was executed, unless should_fail is set.

(code, should_fail=False)

Source from the content-addressed store, hash-verified

20#-----------------------------------------------------------------------------
21
22def check_cpaste(code, should_fail=False):
23 """Execute code via 'cpaste' and ensure it was executed, unless
24 should_fail is set.
25 """
26 ip.user_ns['code_ran'] = False
27
28 src = StringIO()
29 if not hasattr(src, 'encoding'):
30 # IPython expects stdin to have an encoding attribute
31 src.encoding = None
32 src.write(code)
33 src.write('\n--\n')
34 src.seek(0)
35
36 stdin_save = sys.stdin
37 sys.stdin = src
38
39 try:
40 context = tt.AssertPrints if should_fail else tt.AssertNotPrints
41 with context("Traceback (most recent call last)"):
42 ip.magic('cpaste')
43
44 if not should_fail:
45 assert ip.user_ns['code_ran'], "%r failed" % code
46 finally:
47 sys.stdin = stdin_save
48
49def test_cpaste():
50 """Test cpaste magic"""

Callers 1

test_cpasteFunction · 0.85

Calls 3

seekMethod · 0.80
writeMethod · 0.45
magicMethod · 0.45

Tested by

no test coverage detected