Error: This application has requested the Runtime to terminate it in an unusual way

Hello, I have the following small problem when terminating a

    > matplotlib script. Basically I do the following
    > (interactive set to False): from pylab import *
    > plot([1,2,3]) savefig('bla.ps')

    > Now, this script does produce the PS file but then
    > terminates with the error message:

    >> Fatal Python error: PyEval_RestoreThread: NULL tstate This
    >> application has requested the Runtime to terminate it in an
    > unusual way.
    >> Please contact the application's support team for more
    >> information.

This error has cropped up before in other contexts, always on win32,
and I think a common cause is when you import the tk backend but do
not start the tk mainloop (which is what show does), which is what I
suspect you are doing since this is the default backend on win32. My
guess is the bug will go away if you call "show" at the end of the
script, but then you'll get the GUI window in addition to the ps file.

I'm not sure if there is a fix for this. Basically, you want to
create postscript output using the tk backend without having the tk
window popup. Perhaps Todd has some ideas about what to do with tk
internals to avoid this problem, but I don't know offhand. I'll try
and take a look next week when I have access to a win32 box. Does the
minimal script, which does nothing but "import pylab" replicate the
problem?

Do you know that you can run the script with -dPS from the shell to
switch the backend? Then you'll get the PS, no GUI popup, no error,
and you won't have to alter your script. You can also place an rc
file in the directory where the script resides and make PS the default
backend. When you want to switch to GUI mode, you can replace the
backend parameter with TkAgg again. But you'll need the show in that
case.

If you get any additional information from some of the tests suggested
above, let me know, because as I said this PyEval_RestoreThread has
popped up in many contexts, including multiple calls to show or not
calling it at all. I would like to fix these problems if possible so
that it "just works".

See related threads

http://sourceforge.net/mailarchive/message.php?msg_id=10271482
http://sourceforge.net/mailarchive/message.php?msg_id=10114470

JDH

Hi John,

   >> Fatal Python error: PyEval_RestoreThread: NULL tstate This
   >> application has requested the Runtime to terminate it in an
   > unusual way.
   >> Please contact the application's support team for more
   >> information.

This error has cropped up before in other contexts, always on win32,
and I think a common cause is when you import the tk backend but do
not start the tk mainloop (which is what show does), which is what I
suspect you are doing since this is the default backend on win32.

Yes, that's exactly what's happening. I'm using winXP with matplotlib 0.65 and Activestate Python 2.3.2

Does the
minimal script, which does nothing but "import pylab" replicate the
problem?

This replictes the problem:

#!/usr/bin/env python
from pylab import *
rcParams['interactive'] = False
plot([1,2,3])
savefig('bla.ps')

Do you know that you can run the script with -dPS from the shell to
switch the backend?

This is not really appropriate for me, since I read some user input and decide then (after the script is running) if I produce screen output or only a PS file :frowning:

If you or anyone else finds a solution, please let me know.

Many thanks,
                         Axel