Changing functions in a running program?

Andreas Yankopolus <[email protected]> Sat, 12 Jan 2019 18:07:20 -0500
Newsgroups gmane.comp.python.ipython.devel
Message-ID <[email protected]>
--===============4290399581094901614==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_E08DBEBF-DAB0-46D2-8D69-846B251DAC10"


--Apple-Mail=_E08DBEBF-DAB0-46D2-8D69-846B251DAC10
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

Is it possible to run a program to a breakpoint in IPython and then =
change functions within that program? This would be for interactively =
experimenting with program logic without having to restart the program =
from scratch. I=E2=80=99m running IPython3 from Emacs using Elpy for =
interacting 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=20
In main.
> /home/ayank/Documents/foo.py(16)main()
     14     print ("In main.")
     15     Pdb().set_trace()
---> 16     name()
     17=20
     18 if __name__ =3D=3D "__main__":


ipdb>=20

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=9CAlice=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 this process =
incorrectly, as I can make these kind of changes at an IPython3 prompt =
but not at an ipdb one.

Thanks,

Andreas=

--Apple-Mail=_E08DBEBF-DAB0-46D2-8D69-846B251DAC10
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" class=3D"">Is =
it possible to run a program to a breakpoint in IPython and then change =
functions within that program? This would be for interactively =
experimenting with program logic without having to restart the program =
from scratch. I=E2=80=99m running IPython3 from Emacs using Elpy for =
interacting with the IPython3 process.<div class=3D""><br =
class=3D""></div><div class=3D"">Consider foo.py as follows:</div><div =
class=3D""><br class=3D""></div><div class=3D""><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">#!/usr/bin/env ipython3</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo; min-height: 13px;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""></span><br class=3D""></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">import sys</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">from =
IPython.core.debugger import Pdb</span></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo; min-height: 13px;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""></span><br class=3D""></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo; min-height: 13px;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""></span><br class=3D""></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">def print_name():</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">&nbsp; =
&nbsp; print ("Alice")</span></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo; min-height: 13px;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""></span><br class=3D""></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">def name():</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">&nbsp; =
&nbsp; print_name()</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo; =
min-height: 13px;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D""></span><br class=3D""></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">def =
main(argv):</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp; &nbsp; print ("In main.")</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">&nbsp; =
&nbsp; Pdb().set_trace()</span></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">&nbsp; &nbsp; name()</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo; min-height: 13px;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""></span><br class=3D""></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">if __name__ =3D=3D =
"__main__":</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp; &nbsp; main(sys.argv[1:])</span></div></div><div =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""><br class=3D""></span></div><div class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">Running =
it gives me:</span></div><div class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D""><br =
class=3D""></span></div><div class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D""><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo; color: rgb(47, 180, 29);" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""><b class=3D"">ayank@snorri</b></span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #000000" =
class=3D"">:</span><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #400bd9" class=3D""><b =
class=3D"">~/Documents</b></span><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #000000" class=3D"">$ =
./foo.py&nbsp;</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">In main.</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo; color: =
rgb(47, 180, 29);" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #000000" class=3D"">&gt; </span><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">/home/ayank/Documents/foo.py</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #000000" =
class=3D"">(16)</span><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #2eaebb" class=3D"">main</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #400bd9" =
class=3D"">()</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo; color: =
rgb(64, 11, 217);" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #2fb41d" class=3D"">&nbsp;&nbsp; &nbsp; 14 =
</span><span style=3D"font-variant-ligatures: no-common-ligatures; =
color: #b42419" class=3D"">&nbsp; &nbsp; </span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #000000" =
class=3D"">print </span><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">("In main.")</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #2fb41d" =
class=3D"">&nbsp;&nbsp; &nbsp; 15 </span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #b42419" =
class=3D"">&nbsp; &nbsp; </span><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">Pdb</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #400bd9" =
class=3D"">().</span><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">set_trace</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #400bd9" =
class=3D"">()</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo; color: =
rgb(47, 180, 29);" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">---&gt; 16 </span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #b42419" =
class=3D"">&nbsp; &nbsp; </span><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #000000" class=3D"">name</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #400bd9" =
class=3D"">()</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo; color: =
rgb(47, 180, 29);" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">&nbsp;&nbsp; &nbsp; =
17&nbsp;</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(64, =
11, 217);" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #2fb41d" class=3D"">&nbsp;&nbsp; &nbsp; 18 =
if</span><span style=3D"font-variant-ligatures: no-common-ligatures; =
color: #000000" class=3D""> __name__ </span><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">=3D=3D</span><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #000000" class=3D""> </span><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">"__main__":</span></div></span><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo; min-height: 13px;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""></span><br class=3D""></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo; min-height: 13px;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""></span><br class=3D""></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">ipdb&gt;&nbsp;</span></div></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D""><br =
class=3D""></span></div><div style=3D"margin: 0px; font-stretch: normal; =
line-height: normal;" class=3D""><span style=3D"font-family: Helvetica; =
font-size: 12px;" class=3D"">I now want to change the definition =
of</span><span style=3D"font-family: Menlo; font-size: 11px;" =
class=3D"">&nbsp;print_name()</span> to print&nbsp;=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 <span style=3D"font-family: Menlo; =
font-size: 11px;" class=3D"">C-c C-y f</span>, but when I then type =
<span style=3D"font-family: Menlo; font-size: 11px;" =
class=3D"">name()</span>, I get =E2=80=9CAlice=E2=80=9D. The reference =
to <span style=3D"font-family: Menlo; font-size: 11px;" =
class=3D"">print_name()</span> in <span style=3D"font-family: Menlo; =
font-size: 11px;" class=3D"">name()</span> is not getting updated to =
point to the new definition of <span style=3D"font-family: Menlo; =
font-size: 11px;" class=3D"">print_name</span><font face=3D"Menlo" =
class=3D""><span style=3D"font-size: 11px;" class=3D"">(</span>)</font>. =
Likely I=E2=80=99m going about this process incorrectly, as I can make =
these kind of changes at an IPython3 prompt but not at an ipdb =
one.</div><div style=3D"margin: 0px; font-stretch: normal; line-height: =
normal;" class=3D""><br class=3D""></div><div style=3D"margin: 0px; =
font-stretch: normal; line-height: normal;" class=3D"">Thanks,</div><div =
style=3D"margin: 0px; font-stretch: normal; line-height: normal;" =
class=3D""><br class=3D""></div><div style=3D"margin: 0px; font-stretch: =
normal; line-height: normal;" class=3D"">Andreas</div></body></html>=

--Apple-Mail=_E08DBEBF-DAB0-46D2-8D69-846B251DAC10--

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

--===============4290399581094901614==--