Re: Accessing get_ipython() within an embedded shell
Wes Turner <[email protected]> Tue, 10 Apr 2018 02:22:12 -0400
| Newsgroups | gmane.comp.python.ipython.devel |
|---|---|
| Message-ID | <CACfEFw8YoSYJNVNSvGTJG4wYKvEMnpn2HOHZWWw1VWuFe3oqnw@mail.gmail.com> |
--===============4903284215769485062== Content-Type: multipart/alternative; boundary="f4f5e80c784c0afb000569788b03" --f4f5e80c784c0afb000569788b03 Content-Type: text/plain; charset="UTF-8" ipdb or pytest-ipdb may have a helpful example? https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py https://github.com/mverteuil/pytest-ipdb/blob/master/pytestipdb/ptipdb.py These all require nose: https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_magic_terminal.py https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_magic_arguments.py https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_interactiveshell.py On Tuesday, April 10, 2018, Jacob Vanderplas <[email protected]> wrote: > I'm trying to create automated tests for some magic functions within a > pytest framework. I've figured out how to do part of what I need using an > embedded interactive shell; briefly, I do something like this: > > from IPython.terminal.embed import InteractiveShellEmbed > ipshell = InteractiveShellEmbed() > out = ipshell.run_cell("%my_magic_command") > assert out.result == expected_result > > The problem I encounter is that my magic function optionally uses IPython > .get_ipython().user_ns to access variables in the user namespace, and > IPython.get_ipython() returns None when run within an embedded shell in > this way. > > Is there a way to create a simple embedded shell like this one that will > behave as if it is an actual IPython shell, with IPython.get_ipython() def > ined? > > Thanks, > > Jake > --f4f5e80c784c0afb000569788b03 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div>ipdb or pytest-ipdb may have a helpful example?</div><div><br></div><a= href=3D"https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py">https= ://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py</a><div><br></div><d= iv><a href=3D"https://github.com/mverteuil/pytest-ipdb/blob/master/pytestip= db/ptipdb.py">https://github.com/mverteuil/pytest-ipdb/blob/master/pytestip= db/ptipdb.py</a></div><div><br></div><div>These all require nose:</div><div= ><br></div><div><a href=3D"https://github.com/ipython/ipython/blob/master/I= Python/core/tests/test_magic_terminal.py">https://github.com/ipython/ipytho= n/blob/master/IPython/core/tests/test_magic_terminal.py</a></div><div><br><= /div><div><a href=3D"https://github.com/ipython/ipython/blob/master/IPython= /core/tests/test_magic_arguments.py">https://github.com/ipython/ipython/blo= b/master/IPython/core/tests/test_magic_arguments.py</a></div><div><br></div= ><div><a href=3D"https://github.com/ipython/ipython/blob/master/IPython/cor= e/tests/test_interactiveshell.py">https://github.com/ipython/ipython/blob/m= aster/IPython/core/tests/test_interactiveshell.py</a><br><br>On Tuesday, Ap= ril 10, 2018, Jacob Vanderplas <<a href=3D"mailto:[email protected].= edu">[email protected]</a>> wrote:<br><blockquote class=3D"gmail= _quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:= 1ex"><div dir=3D"ltr"><div><div style=3D"color:rgb(34,34,34);font-family:ar= ial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-sp= acing:normal;text-align:start;text-indent:0px;text-transform:none;white-spa= ce:normal;word-spacing:0px;background-color:rgb(255,255,255)"><span style= =3D"font-size:12.8px">I'm trying to create automated tests for some mag= ic functions within a pytest framework</span><span style=3D"font-size:12.8p= x">. I've figured out how to do part of what I need using an embedded i= nteractive shell; briefly, I do something like this:</span></div><div style= =3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-= style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-in= dent:0px;text-transform:none;white-space:normal;word-spacing:0px;background= -color:rgb(255,255,255)"><br></div><div style=3D"color:rgb(34,34,34);font-f= amily:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;l= etter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;w= hite-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><div>= <font face=3D"monospace, monospace">from<span>=C2=A0</span><span>IPython</s= pan>.terminal.embed import InteractiveShellEmbed</font></div><div><font fac= e=3D"monospace, monospace">ipshell =3D InteractiveShellEmbed()</font></div>= <div><font face=3D"monospace, monospace">out =3D ipshell.run_cell("%my= _magic_co<wbr>mmand")</font></div><div><font face=3D"monospace, monosp= ace">assert out.result =3D=3D expected_result</font></div></div><div style= =3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-= style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-in= dent:0px;text-transform:none;white-space:normal;word-spacing:0px;background= -color:rgb(255,255,255)"><font face=3D"monospace, monospace"><br></font></d= iv><div style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size= :12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:= start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0= px;background-color:rgb(255,255,255)"><font face=3D"arial, helvetica, sans-= serif">The problem I encounter is that my magic function optionally uses=C2= =A0</font><font face=3D"monospace, monospace"><span>IPython</span>.get_ipyt= hon().use<wbr>r_ns</font><font face=3D"arial, helvetica, sans-serif"><span>= =C2=A0</span>to access variables in the user namespace, and<span>=C2=A0</sp= an></font><font face=3D"monospace, monospace"><span>IPython</span>.get_ipyt= hon()</font><font face=3D"arial, helvetica, sans-serif"><span>=C2=A0</span>= retu<wbr>rns<span>=C2=A0</span></font><font face=3D"monospace, monospace">N= one=C2=A0</font><font face=3D"arial, helvetica, sans-serif">when run within= an embedded shell in this way.</font></div><div style=3D"color:rgb(34,34,3= 4);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-wei= ght:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transfo= rm:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,25= 5)"><font face=3D"arial, helvetica, sans-serif"><br></font></div><div style= =3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-= style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-in= dent:0px;text-transform:none;white-space:normal;word-spacing:0px;background= -color:rgb(255,255,255)"><font face=3D"arial, helvetica, sans-serif">Is the= re a way to create a simple embedded shell like this one that will behave a= s if it is an actual<span>=C2=A0</span><span>IPython</span><span>=C2=A0</sp= an>shell, with<span>=C2=A0</span></font><font face=3D"monospace, monospace"= ><span>IPython</span>.get_ipython()</font><font face=3D"arial, helvetica, s= ans-serif"><span>=C2=A0</span>def<wbr>ined?</font></div><div style=3D"color= :rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:nor= mal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;= text-transform:none;white-space:normal;word-spacing:0px;background-color:rg= b(255,255,255)"><font face=3D"arial, helvetica, sans-serif"><br></font></di= v><div style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:= 12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:s= tart;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0p= x;background-color:rgb(255,255,255)"><font face=3D"arial, helvetica, sans-s= erif">Thanks,</font></div><div style=3D"color:rgb(34,34,34);font-family:ari= al,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spa= cing:normal;text-align:start;text-indent:0px;text-transform:none;white-spac= e:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face=3D"= arial, helvetica, sans-serif"><br></font></div><div style=3D"color:rgb(34,3= 4,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-= weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-tran= sform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255= ,255)"><font face=3D"arial, helvetica, sans-serif">=C2=A0 Jake</font></div>= </div> </div> </blockquote></div> --f4f5e80c784c0afb000569788b03-- --===============4903284215769485062== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ IPython-dev mailing list [email protected] https://mail.python.org/mailman/listinfo/ipython-dev --===============4903284215769485062==--