On Tue, 08 Jan 2013 17:05:50 +0100, Stefan Krah <stefan@bytereef.org> wrote:
">

(original) (raw)




On Tue, Jan 8, 2013 at 8:37 AM, R. David Murray <rdmurray@bitdance.com> wrote:
On Tue, 08 Jan 2013 17:05:50 +0100, Stefan Krah <stefan@bytereef.org> wrote:

> Eli Bendersky <eliben@gmail.com> wrote:

> > Everything works fine. However, if I add import_fresh_module for the C module:

> >

> > from test.support import import_fresh_module

> > import pickle

> > C = import_fresh_module('xml.etree.ElementTree', fresh=['_elementtree'])

> > P = import_fresh_module('xml.etree.ElementTree', blocked=['_elementtree'])

>

> sys.modules still contains the C version at this point, so:

>

> sys.modules['xml.etree.ElementTree'] = P

>

>

> > tb = P.TreeBuilder()

> > print(pickle.dumps(tb))

>

> > This interaction only seems to happen with pickle. What's going on here? Can we

> > somehow improve import_fresh_module to avoid this? Perhaps actually deleting

> > previously imported modules with some special keyword flag?

>

> pickle always looks up sys.modules['xml.etree.ElementTree']. Perhaps we

> could improve something, but this requirement is rather special; personally

> I'm okay with switching sys.modules explicitly in the tests, because that

> reminds me of what pickle does.


Handling this case is why having a context-manager form of
import\_fresh\_module was suggested earlier in this meta-thread. �At
least, I think that would solve it, I haven't tried it :)

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.


Eli