|
From: <ef...@us...> - 2010-06-13 21:04:21
|
Revision: 8424
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8424&view=rev
Author: efiring
Date: 2010-06-13 21:04:14 +0000 (Sun, 13 Jun 2010)
Log Message:
-----------
[3015013] use atexit in _pylab_helpers to ensure orderly shutdown.
It closes all windows; in the case of Tk, this calls the root.destroy()
function, which should prevent the PyEval_RestoreThread error on Win. See
http://mail.python.org/pipermail/python-bugs-list/2002-November/014207.html
This changeset also refactors the close() functionality into
three Gcf methods, fixing a bug (or inconsistency) in which calling
close with a figure number failed to call mpl_disconnect.
Modified Paths:
--------------
trunk/matplotlib/lib/matplotlib/_pylab_helpers.py
trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py
trunk/matplotlib/lib/matplotlib/backends/backend_tkagg.py
trunk/matplotlib/lib/matplotlib/pyplot.py
Modified: trunk/matplotlib/lib/matplotlib/_pylab_helpers.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/_pylab_helpers.py 2010-06-12 23:40:19 UTC (rev 8423)
+++ trunk/matplotlib/lib/matplotlib/_pylab_helpers.py 2010-06-13 21:04:14 UTC (rev 8424)
@@ -4,8 +4,12 @@
import sys, gc
+import atexit
+import traceback
+
+
def error_msg(msg):
- print >>sys.stderr, msgs
+ print >>sys.stderr, msg
class Gcf(object):
"""
@@ -34,10 +38,10 @@
If figure manager *num* exists, make it the active
figure and return the manager; otherwise return *None*.
"""
- figManager = Gcf.figs.get(num, None)
- if figManager is not None:
- Gcf.set_active(figManager)
- return figManager
+ manager = Gcf.figs.get(num, None)
+ if manager is not None:
+ Gcf.set_active(manager)
+ return manager
@staticmethod
def destroy(num):
@@ -48,22 +52,36 @@
window "destroy" and "delete" events.
"""
if not Gcf.has_fignum(num): return
- figManager = Gcf.figs[num]
+ manager = Gcf.figs[num]
+ manager.canvas.mpl_disconnect(manager._cidgcf)
# There must be a good reason for the following careful
# rebuilding of the activeQue; what is it?
oldQue = Gcf._activeQue[:]
Gcf._activeQue = []
for f in oldQue:
- if f != figManager:
+ if f != manager:
Gcf._activeQue.append(f)
del Gcf.figs[num]
#print len(Gcf.figs.keys()), len(Gcf._activeQue)
- figManager.destroy()
+ manager.destroy()
gc.collect()
@staticmethod
+ def destroy_fig(fig):
+ "*fig* is a Figure instance"
+ for manager in Gcf.figs.values():
+ if manager.canvas.figure == fig:
+ Gcf.destroy(manager.num)
+
+ @staticmethod
+ def destroy_all():
+ for manager in Gcf.figs.values():
+ Gcf.destroy(manager.num)
+
+
+ @staticmethod
def has_fignum(num):
"""
Return *True* if figure *num* exists.
@@ -105,3 +123,7 @@
Gcf._activeQue.append(manager)
Gcf.figs[manager.num] = manager
+atexit.register(Gcf.destroy_all)
+
+
+
Modified: trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py 2010-06-12 23:40:19 UTC (rev 8423)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py 2010-06-13 21:04:14 UTC (rev 8424)
@@ -551,7 +551,7 @@
self.canvas.destroy()
if self.toolbar:
self.toolbar.destroy()
- self.__dict__.clear()
+ self.__dict__.clear() #Is this needed? Other backends don't have it.
if Gcf.get_num_fig_managers()==0 and \
not matplotlib.is_interactive() and \
Modified: trunk/matplotlib/lib/matplotlib/backends/backend_tkagg.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backends/backend_tkagg.py 2010-06-12 23:40:19 UTC (rev 8423)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_tkagg.py 2010-06-13 21:04:14 UTC (rev 8424)
@@ -491,10 +491,8 @@
if self.window is not None:
#self.toolbar.destroy()
self.window.destroy()
+ self.window = None
- pass
- self.window = None
-
def set_window_title(self, title):
self.window.wm_title(title)
Modified: trunk/matplotlib/lib/matplotlib/pyplot.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/pyplot.py 2010-06-12 23:40:19 UTC (rev 8423)
+++ trunk/matplotlib/lib/matplotlib/pyplot.py 2010-06-13 21:04:14 UTC (rev 8424)
@@ -315,21 +315,15 @@
figManager = _pylab_helpers.Gcf.get_active()
if figManager is None: return
else:
- figManager.canvas.mpl_disconnect(figManager._cidgcf)
_pylab_helpers.Gcf.destroy(figManager.num)
elif len(args)==1:
arg = args[0]
if arg=='all':
- for manager in _pylab_helpers.Gcf.get_all_fig_managers():
- manager.canvas.mpl_disconnect(manager._cidgcf)
- _pylab_helpers.Gcf.destroy(manager.num)
+ _pylab_helpers.Gcf.destroy_all()
elif isinstance(arg, int):
_pylab_helpers.Gcf.destroy(arg)
elif isinstance(arg, Figure):
- for manager in _pylab_helpers.Gcf.get_all_fig_managers():
- if manager.canvas.figure==arg:
- manager.canvas.mpl_disconnect(manager._cidgcf)
- _pylab_helpers.Gcf.destroy(manager.num)
+ _pylab_helpers.Gcf.destroy_fig(arg)
else:
raise TypeError('Unrecognized argument type %s to close'%type(arg))
else:
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|