[Zope-Checkins] CVS: Zope/lib/python/ZPublisher - HTTPRequest.py:1.64 HTTPResponse.py:1.56

Shane Hathaway shane@cvs.zope.org
Wed, 3 Apr 2002 15:44:29 -0500


Update of /cvs-repository/Zope/lib/python/ZPublisher
In directory cvs.zope.org:/tmp/cvs-serv3170/lib/python/ZPublisher

Modified Files:
	HTTPRequest.py HTTPResponse.py 
Log Message:
Merged shane-better-tracebacks-branch.  The changes are explained in http://dev.zope.org/Wikis/DevSite/Proposals/BetterTracebacks

=== Zope/lib/python/ZPublisher/HTTPRequest.py 1.63 => 1.64 ===
         return result+"</table>"
 
-    __repr__=__str__
+    def __repr__(self):
+        return "<%s, URL=%s>" % (self.__class__.__name__, self['URL'])
 
     def text(self):
         result="FORM\n\n"


=== Zope/lib/python/ZPublisher/HTTPResponse.py 1.55 => 1.56 ===
 from BaseResponse import BaseResponse
 from zExceptions import Unauthorized
+from zExceptions.ExceptionFormatter import format_exception
 
 nl2sp=maketrans('\n',' ')
 
+
+# Enable APPEND_TRACEBACKS to make Zope append tracebacks like it used to,
+# but a better solution is to make standard_error_message display error_tb.
+APPEND_TRACEBACKS = 0
+
+
 status_reasons={
 100: 'Continue',
 101: 'Switching Protocols',
@@ -92,16 +99,6 @@
 
 accumulate_header={'set-cookie': 1}.has_key
 
-tb_style = os.environ.get('HTTP_TRACEBACK_STYLE', '').lower()
-if tb_style == 'none':
-    tb_delims = None, None
-elif tb_style == 'js':
-    tb_delims = ('''<pre onclick="this.firstChild.data=this.lastChild.data">
-        &sect;<!--''',  '--></pre>')
-elif tb_style == 'plain':
-    tb_delims = '<pre>', '</pre>'
-else:
-    tb_delims = '<!--', '-->'
 
 class HTTPResponse(BaseResponse):
     """\
@@ -384,8 +381,15 @@
         else: h=value
         self.setHeader(name,h)
 
-    def isHTML(self,str):
-        return str.strip().lower()[:6] == '<html>' or str.find('</') > 0
+    def isHTML(self, s):
+        s = s.lstrip()
+        # Note that the string can be big, so s.lower().startswith() is more
+        # expensive than s[:n].lower().
+        if (s[:6].lower() == '<html>' or s[:14].lower() == '<!doctype html'):
+            return 1
+        if s.find('</') > 0:
+            return 1
+        return 0
 
     def quoteHTML(self,text,
                   subs={'&':'&amp;', "<":'&lt;', ">":'&gt;', '\"':'&quot;'}
@@ -397,42 +401,9 @@
         return text
          
 
-    def format_exception(self, etype, value, tb, limit=None):
-        import traceback
-        result=['Traceback (innermost last):']
-        if limit is None:
-                if hasattr(sys, 'tracebacklimit'):
-                        limit = sys.tracebacklimit
-        n = 0
-        while tb is not None and (limit is None or n < limit):
-                f = tb.tb_frame
-                lineno = tb.tb_lineno
-                co = f.f_code
-                filename = co.co_filename
-                name = co.co_name
-                locals=f.f_locals
-                result.append('  File %s, line %d, in %s'
-                              % (filename,lineno,name))
-                try: result.append('    (Object: %s)' %
-                                   locals[co.co_varnames[0]].__name__)
-                except: pass
-                try: result.append('    (Info: %s)' %
-                                   str(locals['__traceback_info__']))
-                except: pass
-                tb = tb.tb_next
-                n = n + 1
-        result.append(' '.join(traceback.format_exception_only(etype, value)))
-        return result
-
-    def _traceback(self, t, v, tb):
-        tb = self.format_exception(t, v, tb, 200)
-        tb = '\n'.join(tb)
-        tb = self.quoteHTML(tb)
-        if self.debug_mode: _tbopen, _tbclose = '<PRE>', '</PRE>'
-        else:               _tbopen, _tbclose = tb_delims
-        if _tbopen is None:
-            return ''
-        return "\n%s\n%s\n%s" % (_tbopen, tb, _tbclose)
+    def _traceback(self, t, v, tb, as_html=1):
+        tb = format_exception(t, v, tb, as_html=as_html)
+        return '\n'.join(tb)
 
     def redirect(self, location, status=302, lock=0):
         """Cause a redirection without raising an error"""
@@ -621,20 +592,22 @@
             if match is None:
                 body = self.setBody(
                     (str(t),
-                    'Sorry, a site error occurred.<p>'
+                     'Sorry, a site error occurred.<p>'
                      + self._traceback(t, v, tb)),
-                     is_error=1)
-            elif b.strip().lower()[:6]=='<html>' or \
-                  b.strip().lower()[:14]=='<!doctype html':
+                    is_error=1)
+            elif self.isHTML(b):
                 # error is an HTML document, not just a snippet of html
-                body = self.setBody(b + self._traceback(t, '(see above)', tb),
-                                  is_error=1)
+                if APPEND_TRACEBACKS:
+                    body = self.setBody(b + self._traceback(
+                        t, '(see above)', tb), is_error=1)
+                else:
+                    body = self.setBody(b, is_error=1)
             else:
-                body = self.setBody((str(t),
-                                   b + self._traceback(t,'(see above)', tb)),
-                                  is_error=1)
-            del tb
-            return body
+                body = self.setBody(
+                    (str(t), b + self._traceback(t,'(see above)', tb, 0)),
+                    is_error=1)
+        del tb
+        return body
 
     _wrote=None