Commit 91cc6c4e authored by Jérome Perrin's avatar Jérome Perrin

ERP5Type/Utils: configure warnings in zopewsgi

This was too late to enable all warnings, many modules were already
imported and already triggered lots of deprecation warnings that were
ignored.

Also, we no longer need to patch warnings.showwarning because we use
logging.captureWarnings(True) in runwsgi. As a result, the log events
will be slightly different, because logging's way of logging warnings
is a bit different.

test_warnings_redirected_to_event_log no longer make sense because we
only use logging.captureWarnings() in runwsgi, while running tests we
want warnings to be displayed on the console so that developer see them.
parent 0a0dc6d1
......@@ -28,7 +28,10 @@
import warnings
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload
from Products.ERP5Type import Utils
original_warnings_showwarnings = warnings.showwarning
class TestERP5PDFMerge(ERP5TypeTestCase):
......@@ -37,7 +40,7 @@ class TestERP5PDFMerge(ERP5TypeTestCase):
We should not let PdfFileReader overwrite warnings.showwarning method because we already do it in ERP5
https://github.com/mstamy2/PyPDF2/blob/18a2627adac13124d4122c8b92aaa863ccfb8c29/PyPDF2/pdf.py#L1129
"""
self.assertEqual(Utils._showwarning, warnings.showwarning)
self.assertEqual(warnings.showwarning, original_warnings_showwarnings)
document = self.portal.portal_contributions.newContent(
file=makeFileUpload('REF-en-001.pdf'))
merged_pdf_data = self.portal.ERP5Site_mergePDFList(
......@@ -46,7 +49,7 @@ class TestERP5PDFMerge(ERP5TypeTestCase):
portal_type='PDF',
data=merged_pdf_data)
self.tic()
self.assertEqual(Utils._showwarning, warnings.showwarning)
self.assertEqual(warnings.showwarning, original_warnings_showwarnings)
def test_erp5_merge_pdf(self):
document = self.portal.portal_contributions.newContent(
......
......@@ -11,6 +11,7 @@ import socket
import sys
from tempfile import TemporaryFile
import time
import warnings
from six.moves.urllib.parse import quote, urlsplit
from waitress.server import create_server
......@@ -187,6 +188,9 @@ def runwsgi():
action="store_true")
args = parser.parse_args()
if not sys.warnoptions:
warnings.simplefilter('default')
# Configure logging previously handled by ZConfig/ZServer
logging.captureWarnings(True)
root_logger = logging.getLogger()
......
......@@ -218,18 +218,8 @@ def sortValueList(value_list, sort_on=None, sort_order=None, **kw):
return value_list
#####################################################
# Logging
# Decorators
#####################################################
warnings.simplefilter("default")
def _showwarning(message, category, filename, lineno, file=None, line=None):
if file is None:
LOG(category.__name__, WARNING, "%s:%u %s" % (filename, lineno, message))
else:
file.write(warnings.formatwarning(message, category, filename, lineno, line))
warnings.showwarning = _showwarning
def deprecated(message=''):
@simple_decorator
def _deprecated(wrapped):
......
......@@ -273,16 +273,6 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
temp_object = Products.ERP5Type.Document.newTempBase(self.portal, 'id')
self._test_temp_object_persistent(temp_object)
def test_warnings_redirected_to_event_log(self):
self._catch_log_errors()
self.addCleanup(self._ignore_log_errors)
warnings.warn('user warning')
self.assertEqual(self.logged[-1].name, 'UserWarning')
self.assertIn('Products/ERP5Type/tests/testERP5Type.py', self.logged[-1].message)
self.assertIn('user warning', self.logged[-1].message)
warnings.warn('user warning', DeprecationWarning)
self.assertEqual(self.logged[-1].name, 'DeprecationWarning')
def test_objectValues(self):
person = self.portal.person_module.newContent(portal_type='Person')
createZODBPythonScript(person, 'test_script', '', '')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment