Re: Changing functions in a running program?

Thomas Kluyver <[email protected]> Sun, 13 Jan 2019 08:13:04 +0000
Newsgroups gmane.comp.python.ipython.devel
Message-ID <CAOvn4qi8ESV9r1Qnw9UsCNL97q5tG8zxh9eQ7KUgYGO=K3gcCQ@mail.gmail.com>
--===============0851476543109331867==
Content-Type: multipart/alternative; boundary="000000000000fa6b71057f52808e"

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

Hi Andreas,

If you define a function or variable at the breakpoint, it's probably
making it a local variable inside main(), so it's not in scope for name().

You may be able to get round this by defining the new function and then
explicitly making it a global variable, something like this:

globals()['print_name'] =3D print_name

Not exactly elegant, but hopefully it works.
Thomas

On Sun, 13 Jan 2019 at 00:10, 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
>

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

<div dir=3D"ltr"><div>Hi Andreas,</div><div><br></div><div>If you define a =
function or variable at the breakpoint, it&#39;s probably making it a local=
 variable inside main(), so it&#39;s not in scope for name().</div><div><br=
></div><div>You may be able to get round this by defining the new function =
and then explicitly making it a global variable, something like this:</div>=
<div><br></div><div>globals()[&#39;print_name&#39;] =3D print_name<br></div=
><div><br></div><div>Not exactly elegant, but hopefully it works.</div><div=
>Thomas<br></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr">On S=
un, 13 Jan 2019 at 00:10, Andreas Yankopolus &lt;<a href=3D"mailto:andreas@=
yank.to">[email protected]</a>&gt; wrote:<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 pos=
sible to run a program to a breakpoint in IPython and then change functions=
 within that program? This would be for interactively experimenting with pr=
ogram logic without having to restart the program from scratch. I=E2=80=99m=
 running IPython3 from Emacs using Elpy for interacting with the IPython3 p=
rocess.<div><br></div><div>Consider foo.py as follows:</div><div><br></div>=
<div><div style=3D"margin:0px;font-stretch:normal;font-size:11px;line-heigh=
t:normal;font-family:Menlo"><span style=3D"font-variant-ligatures:no-common=
-ligatures">#!/usr/bin/env ipython3</span></div><div style=3D"margin:0px;fo=
nt-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;min-h=
eight:13px"><span style=3D"font-variant-ligatures:no-common-ligatures"></sp=
an><br></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">import sys</span></div><div style=3D"margin:0px;font-st=
retch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span sty=
le=3D"font-variant-ligatures:no-common-ligatures">from IPython.core.debugge=
r import Pdb</span></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-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">def print_name():</span></div><div sty=
le=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 print (&quot;Alice&quot;)</span></div><div style=3D"margin:0p=
x;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;m=
in-height:13px"><span style=3D"font-variant-ligatures:no-common-ligatures">=
</span><br></div><div style=3D"margin:0px;font-stretch:normal;font-size:11p=
x;line-height:normal;font-family:Menlo"><span style=3D"font-variant-ligatur=
es:no-common-ligatures">def name():</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 print_=
name()</span></div><div style=3D"margin:0px;font-stretch:normal;font-size:1=
1px;line-height:normal;font-family:Menlo;min-height:13px"><span style=3D"fo=
nt-variant-ligatures:no-common-ligatures"></span><br></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">def main(=
argv):</span></div><div style=3D"margin:0px;font-stretch:normal;font-size:1=
1px;line-height:normal;font-family:Menlo"><span style=3D"font-variant-ligat=
ures:no-common-ligatures">=C2=A0 =C2=A0 print (&quot;In main.&quot;)</span>=
</div><div style=3D"margin:0px;font-stretch:normal;font-size:11px;line-heig=
ht:normal;font-family:Menlo"><span style=3D"font-variant-ligatures:no-commo=
n-ligatures">=C2=A0 =C2=A0 Pdb().set_trace()</span></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">=C2=A0 =C2=
=A0 name()</span></div><div style=3D"margin:0px;font-stretch:normal;font-si=
ze: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-f=
amily:Menlo"><span style=3D"font-variant-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"><s=
pan style=3D"font-variant-ligatures:no-common-ligatures">=C2=A0 =C2=A0 main=
(sys.argv[1:])</span></div></div><div><span style=3D"font-variant-ligatures=
:no-common-ligatures"><br></span></div><div><span style=3D"font-variant-lig=
atures:no-common-ligatures">Running it gives me:</span></div><div><span sty=
le=3D"font-variant-ligatures:no-common-ligatures"><br></span></div><div><sp=
an style=3D"font-variant-ligatures:no-common-ligatures"><div style=3D"margi=
n:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Men=
lo;color:rgb(47,180,29)"><span style=3D"font-variant-ligatures:no-common-li=
gatures"><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-lig=
atures:no-common-ligatures;color:rgb(64,11,217)"><b>~/Documents</b></span><=
span style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">=
$ ./foo.py=C2=A0</span></div><div style=3D"margin:0px;font-stretch:normal;f=
ont-size:11px;line-height:normal;font-family:Menlo"><span style=3D"font-var=
iant-ligatures:no-common-ligatures">In main.</span></div><div style=3D"marg=
in:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Me=
nlo;color:rgb(47,180,29)"><span style=3D"font-variant-ligatures:no-common-l=
igatures;color:rgb(0,0,0)">&gt; </span><span style=3D"font-variant-ligature=
s:no-common-ligatures">/home/ayank/Documents/foo.py</span><span style=3D"fo=
nt-variant-ligatures:no-common-ligatures;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-variant-ligatures:no-common-ligatures;color=
:rgb(64,11,217)">()</span></div><div style=3D"margin:0px;font-stretch:norma=
l;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(64,11,217)"=
><span style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(47,180=
,29)">=C2=A0=C2=A0 =C2=A0 14 </span><span style=3D"font-variant-ligatures:n=
o-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 </sp=
an><span style=3D"font-variant-ligatures:no-common-ligatures">(&quot;In mai=
n.&quot;)</span></div><div style=3D"margin:0px;font-stretch:normal;font-siz=
e:11px;line-height:normal;font-family:Menlo"><span style=3D"font-variant-li=
gatures:no-common-ligatures;color:rgb(47,180,29)">=C2=A0=C2=A0 =C2=A0 15 </=
span><span style=3D"font-variant-ligatures:no-common-ligatures;color:rgb(18=
0,36,25)">=C2=A0 =C2=A0 </span><span style=3D"font-variant-ligatures:no-com=
mon-ligatures">Pdb</span><span style=3D"font-variant-ligatures:no-common-li=
gatures;color:rgb(64,11,217)">().</span><span style=3D"font-variant-ligatur=
es:no-common-ligatures">set_trace</span><span style=3D"font-variant-ligatur=
es:no-common-ligatures;color:rgb(64,11,217)">()</span></div><div style=3D"m=
argin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family=
:Menlo;color:rgb(47,180,29)"><span style=3D"font-variant-ligatures:no-commo=
n-ligatures">---&gt; 16 </span><span style=3D"font-variant-ligatures:no-com=
mon-ligatures;color:rgb(180,36,25)">=C2=A0 =C2=A0 </span><span style=3D"fon=
t-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">name</span><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-size:11px;l=
ine-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span style=3D"fo=
nt-variant-ligatures:no-common-ligatures">=C2=A0=C2=A0 =C2=A0 17=C2=A0</spa=
n></div><div style=3D"margin:0px;font-stretch:normal;font-size:11px;line-he=
ight:normal;font-family:Menlo;color:rgb(64,11,217)"><span style=3D"font-var=
iant-ligatures:no-common-ligatures;color:rgb(47,180,29)">=C2=A0=C2=A0 =C2=
=A0 18 if</span><span style=3D"font-variant-ligatures:no-common-ligatures;c=
olor:rgb(0,0,0)"> __name__ </span><span style=3D"font-variant-ligatures:no-=
common-ligatures">=3D=3D</span><span style=3D"font-variant-ligatures:no-com=
mon-ligatures;color:rgb(0,0,0)"> </span><span style=3D"font-variant-ligatur=
es:no-common-ligatures">&quot;__main__&quot;:</span></div></span><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;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;f=
ont-family:Menlo"><span style=3D"font-variant-ligatures:no-common-ligatures=
">ipdb&gt;=C2=A0</span></div></div><div style=3D"margin:0px;font-stretch:no=
rmal;font-size:11px;line-height:normal;font-family:Menlo"><span style=3D"fo=
nt-variant-ligatures:no-common-ligatures"><br></span></div><div style=3D"ma=
rgin:0px;font-stretch:normal;line-height:normal"><span style=3D"font-family=
:Helvetica;font-size:12px">I now want to change the definition of</span><sp=
an style=3D"font-family:Menlo;font-size:11px">=C2=A0print_name()</span> to =
print=C2=A0=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 styl=
e=3D"font-family:Menlo;font-size:11px">C-c C-y f</span>, but when I then ty=
pe <span style=3D"font-family:Menlo;font-size:11px">name()</span>, I get =
=E2=80=9CAlice=E2=80=9D. The reference to <span style=3D"font-family:Menlo;=
font-size:11px">print_name()</span> in <span style=3D"font-family:Menlo;fon=
t-size:11px">name()</span> is not getting updated to point to the new defin=
ition of <span style=3D"font-family:Menlo;font-size:11px">print_name</span>=
<font face=3D"Menlo"><span style=3D"font-size:11px">(</span>)</font>. Likel=
y I=E2=80=99m going about this process incorrectly, as I can make these kin=
d of changes at an IPython3 prompt but not at an ipdb one.</div><div style=
=3D"margin:0px;font-stretch:normal;line-height:normal"><br></div><div style=
=3D"margin:0px;font-stretch:normal;line-height:normal">Thanks,</div><div st=
yle=3D"margin:0px;font-stretch:normal;line-height:normal"><br></div><div st=
yle=3D"margin:0px;font-stretch:normal;line-height:normal">Andreas</div></di=
v>_______________________________________________<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>

--000000000000fa6b71057f52808e--

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

--===============0851476543109331867==--