Re: Changing functions in a running program?

Wes Turner <[email protected]> Sat, 12 Jan 2019 20:37:52 -0500
Newsgroups gmane.comp.python.ipython.devel
Message-ID <CACfEFw_QhB24j+NDkWjqgymcFxhV1i+YmVrQ2QNCpeQCxaVoFg@mail.gmail.com>
--===============7877076541220110413==
Content-Type: multipart/alternative; boundary="000000000000aa8989057f4cfaf8"

--000000000000aa8989057f4cfaf8
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

https://en.wikipedia.org/wiki/Monkey_patch#Pitfalls

https://pypi.org/search/?q=3Dmonkeypatch

https://docs.pytest.org/en/latest/monkeypatch.html#monkeypatching-mocking-m=
odules-and-environments

https://stackoverflow.com/questions/48112226/update-a-function-during-debug=
ging-pdb-or-ipdb
- Does this work from just a plain ipdb shell (when not sending the new
function definition from emacs)?


On Sat, Jan 12, 2019 at 6:11 PM Andreas Yankopolus <[email protected]> wrote:

> Is it possible to run a program to a breakpoint in IPython and then chang=
e
> functions within that program? This would be for interactively
> experimenting with program logic without having to restart the program fr=
om
> scratch. I=E2=80=99m running IPython3 from Emacs using Elpy for interacti=
ng with
> the IPython3 process.
>
> Consider foo.py as follows:
>
> #!/usr/bin/env ipython3
>
> import sys
> from IPython.core.debugger import Pdb
>
>
> def print_name():
>     print ("Alice")
>
> def name():
>     print_name()
>
> def main(argv):
>     print ("In main.")
>     Pdb().set_trace()
>     name()
>
> if __name__ =3D=3D "__main__":
>     main(sys.argv[1:])
>
> Running it gives me:
>
> *ayank@snorri*:*~/Documents*$ ./foo.py
> In main.
> > /home/ayank/Documents/foo.py(16)main()
>      14     print ("In main.")
>      15     Pdb().set_trace()
> ---> 16     name()
>      17
>      18 if __name__ =3D=3D "__main__":
>
>
> ipdb>
>
> I now want to change the definition of print_name() to print =E2=80=9CBob=
=E2=80=9D
> instead of =E2=80=9CAlice=E2=80=9D. I can make this change in Emacs, send=
 the new function
> to IPython with C-c C-y f, but when I then type name(), I get =E2=80=9CAl=
ice=E2=80=9D.
> The reference to print_name() in name() is not getting updated to point
> to the new definition of print_name(). Likely I=E2=80=99m going about thi=
s
> process incorrectly, as I can make these kind of changes at an IPython3
> prompt but not at an ipdb one.
>
> Thanks,
>
> Andreas
> _______________________________________________
> IPython-dev mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/ipython-dev
>

--000000000000aa8989057f4cfaf8
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div di=
r=3D"ltr"><div dir=3D"ltr"><a href=3D"https://en.wikipedia.org/wiki/Monkey_=
patch#Pitfalls">https://en.wikipedia.org/wiki/Monkey_patch#Pitfalls</a><br>=
</div><div dir=3D"ltr"><br></div><div dir=3D"ltr"><a href=3D"https://pypi.o=
rg/search/?q=3Dmonkeypatch">https://pypi.org/search/?q=3Dmonkeypatch</a><br=
><br><a href=3D"https://docs.pytest.org/en/latest/monkeypatch.html#monkeypa=
tching-mocking-modules-and-environments">https://docs.pytest.org/en/latest/=
monkeypatch.html#monkeypatching-mocking-modules-and-environments</a><br><br=
><a href=3D"https://stackoverflow.com/questions/48112226/update-a-function-=
during-debugging-pdb-or-ipdb">https://stackoverflow.com/questions/48112226/=
update-a-function-during-debugging-pdb-or-ipdb</a></div><div>- Does this wo=
rk from just a plain ipdb shell (when not sending the new function definiti=
on from emacs)?<br><br></div></div></div></div></div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr">On Sat, Jan 12, 2019 at 6:11 PM Andreas Y=
ankopolus &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&gt; wr=
ote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px=
 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style=
=3D"overflow-wrap: break-word;">Is it possible to run a program to a breakp=
oint in IPython and then change functions within that program? This would b=
e for interactively experimenting with program logic without having to rest=
art the program from scratch. I=E2=80=99m running IPython3 from Emacs using=
 Elpy for interacting with the IPython3 process.<div><br></div><div>Conside=
r foo.py as follows:</div><div><br></div><div><div style=3D"margin:0px;font=
-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span =
style=3D"font-variant-ligatures:no-common-ligatures">#!/usr/bin/env ipython=
3</span></div><div style=3D"margin:0px;font-stretch:normal;font-size:11px;l=
ine-height:normal;font-family:Menlo;min-height:13px"><span style=3D"font-va=
riant-ligatures:no-common-ligatures"></span><br></div><div style=3D"margin:=
0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo=
"><span style=3D"font-variant-ligatures:no-common-ligatures">import sys</sp=
an></div><div style=3D"margin:0px;font-stretch:normal;font-size:11px;line-h=
eight:normal;font-family:Menlo"><span style=3D"font-variant-ligatures:no-co=
mmon-ligatures">from IPython.core.debugger import Pdb</span></div><div styl=
e=3D"margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-=
family:Menlo;min-height:13px"><span style=3D"font-variant-ligatures:no-comm=
on-ligatures"></span><br></div><div style=3D"margin:0px;font-stretch:normal=
;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span=
 style=3D"font-variant-ligatures:no-common-ligatures"></span><br></div><div=
 style=3D"margin:0px;font-stretch:normal;font-size:11px;line-height:normal;=
font-family:Menlo"><span style=3D"font-variant-ligatures:no-common-ligature=
s">def print_name():</span></div><div style=3D"margin:0px;font-stretch:norm=
al;font-size:11px;line-height:normal;font-family:Menlo"><span style=3D"font=
-variant-ligatures:no-common-ligatures">=C2=A0 =C2=A0 print (&quot;Alice&qu=
ot;)</span></div><div style=3D"margin:0px;font-stretch:normal;font-size:11p=
x;line-height:normal;font-family:Menlo;min-height:13px"><span style=3D"font=
-variant-ligatures:no-common-ligatures"></span><br></div><div style=3D"marg=
in:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Me=
nlo"><span style=3D"font-variant-ligatures:no-common-ligatures">def name():=
</span></div><div style=3D"margin:0px;font-stretch:normal;font-size:11px;li=
ne-height:normal;font-family:Menlo"><span style=3D"font-variant-ligatures:n=
o-common-ligatures">=C2=A0 =C2=A0 print_name()</span></div><div style=3D"ma=
rgin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:=
Menlo;min-height:13px"><span style=3D"font-variant-ligatures:no-common-liga=
tures"></span><br></div><div style=3D"margin:0px;font-stretch:normal;font-s=
ize:11px;line-height:normal;font-family:Menlo"><span style=3D"font-variant-=
ligatures:no-common-ligatures">def main(argv):</span></div><div style=3D"ma=
rgin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:=
Menlo"><span style=3D"font-variant-ligatures:no-common-ligatures">=C2=A0 =
=C2=A0 print (&quot;In main.&quot;)</span></div><div style=3D"margin:0px;fo=
nt-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><spa=
n style=3D"font-variant-ligatures:no-common-ligatures">=C2=A0 =C2=A0 Pdb().=
set_trace()</span></div><div style=3D"margin:0px;font-stretch:normal;font-s=
ize:11px;line-height:normal;font-family:Menlo"><span style=3D"font-variant-=
ligatures:no-common-ligatures">=C2=A0 =C2=A0 name()</span></div><div style=
=3D"margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-f=
amily:Menlo;min-height:13px"><span style=3D"font-variant-ligatures:no-commo=
n-ligatures"></span><br></div><div style=3D"margin:0px;font-stretch:normal;=
font-size:11px;line-height:normal;font-family:Menlo"><span style=3D"font-va=
riant-ligatures:no-common-ligatures">if __name__ =3D=3D &quot;__main__&quot=
;:</span></div><div style=3D"margin:0px;font-stretch:normal;font-size:11px;=
line-height:normal;font-family:Menlo"><span style=3D"font-variant-ligatures=
:no-common-ligatures">=C2=A0 =C2=A0 main(sys.argv[1:])</span></div></div><d=
iv><span style=3D"font-variant-ligatures:no-common-ligatures"><br></span></=
div><div><span style=3D"font-variant-ligatures:no-common-ligatures">Running=
 it gives me:</span></div><div><span style=3D"font-variant-ligatures:no-com=
mon-ligatures"><br></span></div><div><span style=3D"font-variant-ligatures:=
no-common-ligatures"><div style=3D"margin:0px;font-stretch:normal;font-size=
:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span styl=
e=3D"font-variant-ligatures:no-common-ligatures"><b>ayank@snorri</b></span>=
<span style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"=
>:</span><span style=3D"font-variant-ligatures:no-common-ligatures;color:rg=
b(64,11,217)"><b>~/Documents</b></span><span style=3D"font-variant-ligature=
s:no-common-ligatures;color:rgb(0,0,0)">$ ./foo.py=C2=A0</span></div><div s=
tyle=3D"margin:0px;font-stretch:normal;font-size:11px;line-height:normal;fo=
nt-family:Menlo"><span style=3D"font-variant-ligatures:no-common-ligatures"=
>In main.</span></div><div style=3D"margin:0px;font-stretch:normal;font-siz=
e:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span sty=
le=3D"font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">&gt; </s=
pan><span style=3D"font-variant-ligatures:no-common-ligatures">/home/ayank/=
Documents/foo.py</span><span style=3D"font-variant-ligatures:no-common-liga=
tures;color:rgb(0,0,0)">(16)</span><span style=3D"font-variant-ligatures:no=
-common-ligatures;color:rgb(46,174,187)">main</span><span style=3D"font-var=
iant-ligatures:no-common-ligatures;color:rgb(64,11,217)">()</span></div><di=
v style=3D"margin:0px;font-stretch:normal;font-size:11px;line-height:normal=
;font-family:Menlo;color:rgb(64,11,217)"><span style=3D"font-variant-ligatu=
res:no-common-ligatures;color:rgb(47,180,29)">=C2=A0=C2=A0 =C2=A0 14 </span=
><span style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(180,36=
,25)">=C2=A0 =C2=A0 </span><span style=3D"font-variant-ligatures:no-common-=
ligatures;color:rgb(0,0,0)">print </span><span style=3D"font-variant-ligatu=
res:no-common-ligatures">(&quot;In main.&quot;)</span></div><div style=3D"m=
argin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family=
:Menlo"><span style=3D"font-variant-ligatures:no-common-ligatures;color:rgb=
(47,180,29)">=C2=A0=C2=A0 =C2=A0 15 </span><span style=3D"font-variant-liga=
tures:no-common-ligatures;color:rgb(180,36,25)">=C2=A0 =C2=A0 </span><span =
style=3D"font-variant-ligatures:no-common-ligatures">Pdb</span><span style=
=3D"font-variant-ligatures:no-common-ligatures;color:rgb(64,11,217)">().</s=
pan><span style=3D"font-variant-ligatures:no-common-ligatures">set_trace</s=
pan><span style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(64,=
11,217)">()</span></div><div style=3D"margin:0px;font-stretch:normal;font-s=
ize:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span s=
tyle=3D"font-variant-ligatures:no-common-ligatures">---&gt; 16 </span><span=
 style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(180,36,25)">=
=C2=A0 =C2=A0 </span><span style=3D"font-variant-ligatures:no-common-ligatu=
res;color:rgb(0,0,0)">name</span><span style=3D"font-variant-ligatures:no-c=
ommon-ligatures;color:rgb(64,11,217)">()</span></div><div style=3D"margin:0=
px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;=
color:rgb(47,180,29)"><span style=3D"font-variant-ligatures:no-common-ligat=
ures">=C2=A0=C2=A0 =C2=A0 17=C2=A0</span></div><div style=3D"margin:0px;fon=
t-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:=
rgb(64,11,217)"><span style=3D"font-variant-ligatures:no-common-ligatures;c=
olor:rgb(47,180,29)">=C2=A0=C2=A0 =C2=A0 18 if</span><span style=3D"font-va=
riant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> __name__ </span><spa=
n style=3D"font-variant-ligatures:no-common-ligatures">=3D=3D</span><span s=
tyle=3D"font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </spa=
n><span style=3D"font-variant-ligatures:no-common-ligatures">&quot;__main__=
&quot;:</span></div></span><div style=3D"margin:0px;font-stretch:normal;fon=
t-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span sty=
le=3D"font-variant-ligatures:no-common-ligatures"></span><br></div><div sty=
le=3D"margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font=
-family:Menlo;min-height:13px"><span style=3D"font-variant-ligatures:no-com=
mon-ligatures"></span><br></div><div style=3D"margin:0px;font-stretch:norma=
l;font-size:11px;line-height:normal;font-family:Menlo"><span style=3D"font-=
variant-ligatures:no-common-ligatures">ipdb&gt;=C2=A0</span></div></div><di=
v style=3D"margin:0px;font-stretch:normal;font-size:11px;line-height:normal=
;font-family:Menlo"><span style=3D"font-variant-ligatures:no-common-ligatur=
es"><br></span></div><div style=3D"margin:0px;font-stretch:normal;line-heig=
ht:normal"><span style=3D"font-family:Helvetica;font-size:12px">I now want =
to change the definition of</span><span style=3D"font-family:Menlo;font-siz=
e:11px">=C2=A0print_name()</span> to print=C2=A0=E2=80=9CBob=E2=80=9D inste=
ad of =E2=80=9CAlice=E2=80=9D. I can make this change in Emacs, send the ne=
w function to IPython with <span style=3D"font-family:Menlo;font-size:11px"=
>C-c C-y f</span>, but when I then type <span style=3D"font-family:Menlo;fo=
nt-size:11px">name()</span>, I get =E2=80=9CAlice=E2=80=9D. The reference t=
o <span style=3D"font-family:Menlo;font-size:11px">print_name()</span> in <=
span style=3D"font-family:Menlo;font-size:11px">name()</span> is not gettin=
g updated to point to the new definition of <span style=3D"font-family:Menl=
o;font-size:11px">print_name</span><font face=3D"Menlo"><span style=3D"font=
-size:11px">(</span>)</font>. Likely I=E2=80=99m going about this process i=
ncorrectly, as I can make these kind of changes at an IPython3 prompt but n=
ot at an ipdb one.</div><div style=3D"margin:0px;font-stretch:normal;line-h=
eight:normal"><br></div><div style=3D"margin:0px;font-stretch:normal;line-h=
eight:normal">Thanks,</div><div style=3D"margin:0px;font-stretch:normal;lin=
e-height:normal"><br></div><div style=3D"margin:0px;font-stretch:normal;lin=
e-height:normal">Andreas</div></div>_______________________________________=
________<br>
IPython-dev mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">IPython-dev@pyt=
hon.org</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/ipython-dev" rel=3D"nor=
eferrer" target=3D"_blank">https://mail.python.org/mailman/listinfo/ipython=
-dev</a><br>
</blockquote></div>

--000000000000aa8989057f4cfaf8--

--===============7877076541220110413==
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

--===============7877076541220110413==--