<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><a href="https://en.wikipedia.org/wiki/Monkey_patch#Pitfalls">https://en.wikipedia.org/wiki/Monkey_patch#Pitfalls</a><br></div><div dir="ltr"><br></div><div dir="ltr"><a href="https://pypi.org/search/?q=monkeypatch">https://pypi.org/search/?q=monkeypatch</a><br><br><a href="https://docs.pytest.org/en/latest/monkeypatch.html#monkeypatching-mocking-modules-and-environments">https://docs.pytest.org/en/latest/monkeypatch.html#monkeypatching-mocking-modules-and-environments</a><br><br><a href="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 work from just a plain ipdb shell (when not sending the new function definition from emacs)?<br><br></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Sat, Jan 12, 2019 at 6:11 PM Andreas Yankopolus <<a href="mailto:andreas@yank.to">andreas@yank.to</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;">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’m running IPython3 from Emacs using Elpy for interacting with the IPython3 process.<div><br></div><div>Consider foo.py as follows:</div><div><br></div><div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">#!/usr/bin/env ipython3</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">import sys</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">from IPython.core.debugger import Pdb</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">def print_name():</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    print ("Alice")</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">def name():</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    print_name()</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">def main(argv):</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    print ("In main.")</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    Pdb().set_trace()</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    name()</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">if __name__ == "__main__":</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    main(sys.argv[1:])</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures">Running it gives me:</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span style="font-variant-ligatures:no-common-ligatures"><b>ayank@snorri</b></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(64,11,217)"><b>~/Documents</b></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">$ ./foo.py </span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">In main.</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">> </span><span style="font-variant-ligatures:no-common-ligatures">/home/ayank/Documents/foo.py</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(16)</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(46,174,187)">main</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(64,11,217)">()</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(64,11,217)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(47,180,29)">     14 </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,36,25)">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">print </span><span style="font-variant-ligatures:no-common-ligatures">("In main.")</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(47,180,29)">     15 </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,36,25)">    </span><span style="font-variant-ligatures:no-common-ligatures">Pdb</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(64,11,217)">().</span><span style="font-variant-ligatures:no-common-ligatures">set_trace</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(64,11,217)">()</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span style="font-variant-ligatures:no-common-ligatures">---> 16 </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,36,25)">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">name</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(64,11,217)">()</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span style="font-variant-ligatures:no-common-ligatures">     17 </span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(64,11,217)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(47,180,29)">     18 if</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> __name__ </span><span style="font-variant-ligatures:no-common-ligatures">==</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">"__main__":</span></div></span><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">ipdb> </span></div></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-family:Helvetica;font-size:12px">I now want to change the definition of</span><span style="font-family:Menlo;font-size:11px"> print_name()</span> to print “Bob” instead of “Alice”. I can make this change in Emacs, send the new function to IPython with <span style="font-family:Menlo;font-size:11px">C-c C-y f</span>, but when I then type <span style="font-family:Menlo;font-size:11px">name()</span>, I get “Alice”. The reference to <span style="font-family:Menlo;font-size:11px">print_name()</span> in <span style="font-family:Menlo;font-size:11px">name()</span> is not getting updated to point to the new definition of <span style="font-family:Menlo;font-size:11px">print_name</span><font face="Menlo"><span style="font-size:11px">(</span>)</font>. Likely I’m 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="margin:0px;font-stretch:normal;line-height:normal"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">Thanks,</div><div style="margin:0px;font-stretch:normal;line-height:normal"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">Andreas</div></div>_______________________________________________<br>
IPython-dev mailing list<br>
<a href="mailto:IPython-dev@python.org" target="_blank">IPython-dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/ipython-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/ipython-dev</a><br>
</blockquote></div>