Matplotlib and subinterpreter for embedded python in c++
Question: I just added subinterpreter to my c++ embedded python editor to have a clean
interpreter for each execution.

    
    
      PyThreadState* tmpstate = Py_NewInterpreter();
      PyThreadState_Swap(tmpstate);
      ... run the script ...
      Py_EndInterpreter(tmpstate);
    

My own module are working and I tested numpy without having any problem. The
problem is with matplotlib

if I run that the first time everything looks fine. The second time i get :

    
    
    <class 'TypeError'>: attribute of type 'NoneType' is not callable:   File "<string>", line 1, in <module>
    
      File "C:\Python33\lib\site-packages\pylab.py", line 1, in <module>
    from matplotlib.pylab import *
    
      File "C:\Python33\lib\site-packages\matplotlib\__init__.py", line 152, in <module>
    from matplotlib.rcsetup import (defaultParams,
    
      File "C:\Python33\lib\site-packages\matplotlib\rcsetup.py", line 19, in <module>
    from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
    
      File "C:\Python33\lib\site-packages\matplotlib\fontconfig_pattern.py", line 25, in <module>
    from matplotlib.pyparsing_py3 import Literal, ZeroOrMore, \
    
      File "C:\Python33\lib\site-packages\matplotlib\pyparsing_py3.py", line 3275, in <module>
    _escapedHexChar = Combine( Suppress(_bslash + "0x") + Word(hexnums) ).setParseAction(lambda s,l,t:unichr(int(t[0],16)))
    
       File "C:\Python33\lib\site-packages\matplotlib\pyparsing_py3.py", line 2961, in __init__
    self.leaveWhitespace()
    
    File "C:\Python33\lib\site-packages\matplotlib\pyparsing_py3.py", line 2587, in      leaveWhitespace
    self.expr = self.expr.copy()
    
      File "C:\Python33\lib\site-packages\matplotlib\pyparsing_py3.py", line 705, in copy
    cpy = copy.copy( self )
    
      File "C:\Python33\lib\copy.py", line 89, in copy
    rv = reductor(2)
    


Answer: This seem to be a bug in Python 3.3 : <http://bugs.python.org/issue17408>

The problem is :

    
    
    The reason is in Objects/typeobject.c: import_copyreg() has a static cache of the copyreg      module.
    When the interpreter stops, the module is filled with None... but gets reused in the next     instance.
    
    Resetting this "mod_copyreg" variable to NULL fixes the issue.
    pickle is also broken for the same reason.
    

