Hello,
I'm still having some struggles with the interaction between pickle and import overriding with import_fresh_module.

_elementtree.TreeBuilder can't be pickled at this point. When I do this:

from test.support import import_fresh_module
import pickle
P = import_fresh_module('xml.etree.ElementTree', blocked=['_elementtree'])
tb = P.TreeBuilder()
print(pickle.dumps(tb))

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'])
tb = P.TreeBuilder()
print(pickle.dumps(tb))

I get an error from pickle.dumps:

Traceback (most recent call last):
  File "mix_c_py_etree.py", line 10, in <module>
    print(pickle.dumps(tb))
_pickle.PicklingError: Can't pickle <class 'xml.etree.ElementTree.TreeBuilder'>: it's not the same object as xml.etree.ElementTree.TreeBuilder


Note that I didn't change the executed code sequence. All I did was import the C version of ET before the Python version. I was under the impression this had to keep working since P = import_fresh_module uses blocked=['_elementtree'] properly.

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?

Thanks in advance,
Eli