changeset 590:f8d29730f146

fixed the behavior when raising errors and redirecting stderr
author Olivier Breuleux <breuleuo@iro.umontreal.ca>
date Wed, 17 Dec 2008 18:16:30 -0500
parents 6a7f3d83c72b
children 3f9ec536f2c1 5398a7a1eca7
files pylearn/dbdict/newstuff.py
diffstat 1 files changed, 13 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/dbdict/newstuff.py	Wed Dec 17 15:39:30 2008 -0500
+++ b/pylearn/dbdict/newstuff.py	Wed Dec 17 18:16:30 2008 -0500
@@ -3,6 +3,7 @@
 
 from collections import defaultdict
 import re, sys, inspect, os, signal, tempfile, shutil, socket
+import traceback
 
 import sql
 
@@ -272,12 +273,19 @@
         signal.signal(signal.SIGINT, self.on_sigterm)
         return self
 
-    def __exit__(self, type, value, traceback):
+    def __exit__(self, type, value, tb_traceback, save = True):
+        if type:
+            try:
+                raise type, value, tb_traceback
+            except:
+                traceback.print_exc()
         signal.signal(signal.SIGTERM, self.prev_sigterm)
         signal.signal(signal.SIGINT, self.prev_sigint)
         self.prev_sigterm = None
         self.prev_sigint = None
-        self.save()
+        if save:
+            self.save()
+        return True
 
 
 class StandardChannel(SingleChannel):
@@ -305,6 +313,7 @@
         return super(StandardChannel, self).__enter__()
 
     def __exit__(self, type, value, traceback):
+        rval = super(StandardChannel, self).__exit__(type, value, traceback, save = False)
         if self.redirect_stdout:
             sys.stdout.close()
             sys.stdout = self.old_stdout
@@ -312,7 +321,8 @@
             sys.stderr.close()
             sys.stderr = self.old_stderr
         os.chdir(self.old_cwd)
-        return super(StandardChannel, self).__exit__(type, value, traceback)
+        self.save()
+        return rval
 
     def setup(self):
         if not os.path.isdir(self.path):