mailr22775 - /trunk/lib/warnings.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on April 15, 2014 - 18:33:
Author: bugman
Date: Tue Apr 15 18:33:30 2014
New Revision: 22775

URL: http://svn.gna.org/viewcvs/relax?rev=22775&view=rev
Log:
Improved support for printing stack tracebacks with RelaxWarnings.

The '--traceback' command line option will now show a full traceback.  A 
replacement
warnings.showwarning() function has been written to write out the traceback 
before the
warnings.formatwarning() replacement function is called.


Modified:
    trunk/lib/warnings.py

Modified: trunk/lib/warnings.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/lib/warnings.py?rev=22775&r1=22774&r2=22775&view=diff
==============================================================================
--- trunk/lib/warnings.py       (original)
+++ trunk/lib/warnings.py       Tue Apr 15 18:33:30 2014
@@ -26,6 +26,8 @@
 
 # Python module imports.
 import inspect
+import sys
+import traceback
 import warnings
 
 # relax module imports.
@@ -39,28 +41,10 @@
 
 # The warning formatting function.
 def format(message, category, filename, lineno, line=None):
-    """ Replacement for warnings.formatwarning to customise output format."""
+    """Replacement for warnings.formatwarning to customise output format."""
 
     # Add the text 'RelaxWarning: ' to the start of the warning message.
     message = "RelaxWarning: %s\n" % message
-
-    # Print stack-trace in escalate mode.
-    if TRACEBACK:
-        tb = ""
-        for frame in inspect.stack()[4:]:
-            file = frame[1]
-            lineNo = frame[2]
-            func = frame[3]
-            tb_frame = '  File "%s", line %i, in %s\n' % (file, lineNo, func)
-            try:
-                context = frame[4][frame[5]]
-            except TypeError:
-                pass
-            else:
-                tb_frame = '%s    %s\n' % (tb_frame, context.strip())
-            tb = tb_frame + tb
-        tb = "Traceback (most recent call last):\n%s" % tb
-        message = tb + message
 
     # Text colouring
     if ansi.enable_control_chars(stream=2):
@@ -75,11 +59,46 @@
     return message
 
 
+def showwarning_tb(message, category, filename, lineno, file=None, 
line=None):
+    """Replacement for warnings.showwarning to show tracebacks."""
+
+    # Set up the output file if needed.
+    if file is None:
+        file = sys.stderr
+
+    # Print the stack traceback.
+    tb = ""
+    for frame in inspect.stack()[1:]:
+        file_name = frame[1]
+        lineNo = frame[2]
+        func = frame[3]
+        tb_frame = '  File "%s", line %i, in %s\n' % (file_name, lineNo, 
func)
+        try:
+            context = frame[4][frame[5]]
+        except TypeError:
+            pass
+        else:
+            tb_frame = '%s    %s\n' % (tb_frame, context.strip())
+        tb = tb_frame + tb
+    tb = "Traceback (most recent call last):\n%s" % tb
+    file.write(tb)
+
+    # Replicating the failsafe mode of the base Python warnings function 
here.
+    try:
+        file.write(format(message, category, filename, lineno, line))
+    except IOError:
+        pass
+
+
 def setup():
     """Set up the warning system."""
 
     # Format warning messages.
     warnings.formatwarning = format
+
+    # Tracebacks.
+    if TRACEBACK:
+        warnings.showwarning = showwarning_tb
 
     # Set warning filters.
     if ESCALATE:




Related Messages


Powered by MHonArc, Updated Tue Apr 15 19:00:03 2014