<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jan 8, 2013 at 8:37 AM, R. David Murray <span dir="ltr"><<a href="mailto:rdmurray@bitdance.com" target="_blank">rdmurray@bitdance.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Tue, 08 Jan 2013 17:05:50 +0100, Stefan Krah <<a href="mailto:stefan@bytereef.org">stefan@bytereef.org</a>> wrote:<br>


> Eli Bendersky <<a href="mailto:eliben@gmail.com">eliben@gmail.com</a>> wrote:<br>
> > Everything works fine. However, if I add import_fresh_module for the C module:<br>
> ><br>
> > from test.support import import_fresh_module<br>
> > import pickle<br>
> > C = import_fresh_module('xml.etree.ElementTree', fresh=['_elementtree'])<br>
> > P = import_fresh_module('xml.etree.ElementTree', blocked=['_elementtree'])<br>
><br>
> sys.modules still contains the C version at this point, so:<br>
><br>
> sys.modules['xml.etree.ElementTree'] = P<br>
><br>
><br>
> > tb = P.TreeBuilder()<br>
> > print(pickle.dumps(tb))<br>
><br>
> > This interaction only seems to happen with pickle. What's going on here? Can we<br>
> > somehow improve import_fresh_module to avoid this? Perhaps actually deleting<br>
> > previously imported modules with some special keyword flag?<br>
><br>
> pickle always looks up sys.modules['xml.etree.ElementTree']. Perhaps we<br>
> could improve something, but this requirement is rather special; personally<br>
> I'm okay with switching sys.modules explicitly in the tests, because that<br>
> reminds me of what pickle does.<br>
<br>
</div>Handling this case is why having a context-manager form of<br>
import_fresh_module was suggested earlier in this meta-thread.  At<br>
least, I think that would solve it, I haven't tried it :)<br>
</blockquote><div><br></div><div>Would you mind extracting just this idea into this discussion so we can focus on it here? I personally don't see how making import_fresh_module a context manager will solve things, unless you add some extra functionality to it? AFAIU it doesn't remove modules from sys.modules *before* importing, at this point.<br>

<br>Eli<br><br></div><div> </div></div><br><br><br></div></div>