From 1c451d99e6bdeaa179d4fa8cdc02efb6284e38e7 Mon Sep 17 00:00:00 2001
From: Arnaud Fontaine <arnaud.fontaine@nexedi.com>
Date: Wed, 22 Jan 2014 16:58:54 +0900
Subject: [PATCH] zExceptions: Monkey patch for Zope to display source code in
 Zope Exceptions tracebacks.

---
 product/ERP5Type/ZopePatch.py                 |  1 +
 .../ERP5Type/patches/ExceptionFormatter.py    | 51 +++++++++++++++++++
 2 files changed, 52 insertions(+)
 create mode 100644 product/ERP5Type/patches/ExceptionFormatter.py

diff --git a/product/ERP5Type/ZopePatch.py b/product/ERP5Type/ZopePatch.py
index fc1618a53e..c4f01ea3ef 100644
--- a/product/ERP5Type/ZopePatch.py
+++ b/product/ERP5Type/ZopePatch.py
@@ -78,6 +78,7 @@ from Products.ERP5Type.patches import DCWorkflowGraph
 from Products.ERP5Type.patches import AceEditorZMI
 from Products.ERP5Type.patches import CachingPolicyManager
 from Products.ERP5Type.patches import AcceleratedHTTPCacheManager
+from Products.ERP5Type.patches import ExceptionFormatter
 
 # These symbols are required for backward compatibility
 from Products.ERP5Type.patches.PropertyManager import ERP5PropertyManager
diff --git a/product/ERP5Type/patches/ExceptionFormatter.py b/product/ERP5Type/patches/ExceptionFormatter.py
new file mode 100644
index 0000000000..094054f47e
--- /dev/null
+++ b/product/ERP5Type/patches/ExceptionFormatter.py
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# Copyright (c) 2014 Arnaud Fontaine <arnaud.fontaine@nexedi.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsibility of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# guarantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+##############################################################################
+
+from zExceptions.ExceptionFormatter import TextExceptionFormatter
+
+TextExceptionFormatter_formatLine = TextExceptionFormatter.formatLine
+def formatLine(self, tb, *args, **kwargs):
+  """
+  Monkey patched to display source code lines when an Exception traceback is
+  displayed in Zope
+  """
+  f = tb.tb_frame
+  filename = f.f_code.co_filename
+  lineno = tb.tb_lineno
+  f_globals = f.f_globals
+  line_str = TextExceptionFormatter_formatLine(self, tb, *args, **kwargs)
+  try:
+    import linecache
+    source_code_line = linecache.getline(filename, lineno, f_globals)
+    line_str += self.line_sep + '    ' + self.escape(source_code_line.strip())
+  except Exception:
+    pass
+
+  return line_str
+
+TextExceptionFormatter.formatLine = formatLine
-- 
2.30.9