From dc4976c11c55580cfa61d88cdbc7cd084031afa7 Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Wed, 3 Oct 2018 15:47:43 +0200 Subject: [PATCH 1/3] Even in Python 2 ZConfig reads the config as text when starting up Zope. --- src/Zope2/Startup/tests/test_schema.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/Zope2/Startup/tests/test_schema.py b/src/Zope2/Startup/tests/test_schema.py index 6f8ec07784..3f711b5e88 100644 --- a/src/Zope2/Startup/tests/test_schema.py +++ b/src/Zope2/Startup/tests/test_schema.py @@ -12,12 +12,12 @@ # ############################################################################## +import codecs import os import io import tempfile import unittest -import six import ZConfig from Zope2.Startup.options import ZopeWSGIOptions @@ -43,10 +43,7 @@ def load_config_text(self, text): # of the directory is checked. This handles this in a # platform-independent way. text = text.replace("<>", TEMPNAME) - if six.PY2: - sio = io.BytesIO(text) - else: - sio = io.StringIO(text) + sio = io.StringIO(text) os.mkdir(TEMPNAME) os.mkdir(TEMPVAR) @@ -62,13 +59,12 @@ def test_load_config_template(self): import Zope2.utilities base = os.path.dirname(Zope2.utilities.__file__) fn = os.path.join(base, "skel", "etc", "wsgi.conf.in") - f = open(fn) - text = f.read() - f.close() + with codecs.open(fn, encoding='utf-8') as f: + text = f.read() self.load_config_text(text) def test_environment(self): - conf, handler = self.load_config_text("""\ + conf, handler = self.load_config_text(u"""\ # instancehome is here since it's required instancehome <> @@ -82,7 +78,7 @@ def test_environment(self): items, [("FEARFACTORY", "rocks"), ("NSYNC", "doesnt")]) def test_zodb_db(self): - conf, handler = self.load_config_text("""\ + conf, handler = self.load_config_text(u"""\ instancehome <> @@ -96,25 +92,25 @@ def test_zodb_db(self): self.assertEqual(conf.databases[0].config.cache_size, 5000) def test_max_conflict_retries_default(self): - conf, handler = self.load_config_text("""\ + conf, handler = self.load_config_text(u"""\ instancehome <> """) self.assertEqual(conf.max_conflict_retries, 3) def test_max_conflict_retries_explicit(self): - conf, handler = self.load_config_text("""\ + conf, handler = self.load_config_text(u"""\ instancehome <> max-conflict-retries 15 """) self.assertEqual(conf.max_conflict_retries, 15) def test_default_zpublisher_encoding(self): - conf, dummy = self.load_config_text("""\ + conf, dummy = self.load_config_text(u"""\ instancehome <> """) self.assertEqual(conf.default_zpublisher_encoding, 'utf-8') - conf, dummy = self.load_config_text("""\ + conf, dummy = self.load_config_text(u"""\ instancehome <> default-zpublisher-encoding iso-8859-15 """) From c32991d4c55ead19a9775b8c9767d934d92cd6e7 Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Wed, 3 Oct 2018 15:48:34 +0200 Subject: [PATCH 2/3] Restore default_encoding after test run. --- src/Zope2/Startup/tests/test_schema.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Zope2/Startup/tests/test_schema.py b/src/Zope2/Startup/tests/test_schema.py index 3f711b5e88..e2665352ac 100644 --- a/src/Zope2/Startup/tests/test_schema.py +++ b/src/Zope2/Startup/tests/test_schema.py @@ -19,6 +19,8 @@ import unittest import ZConfig +import ZPublisher.HTTPRequest +import Zope2.Startup.datatypes from Zope2.Startup.options import ZopeWSGIOptions @@ -38,6 +40,13 @@ def getSchema(): class WSGIStartupTestCase(unittest.TestCase): + def setUp(self): + self.default_encoding = ZPublisher.HTTPRequest.default_encoding + + def tearDown(self): + Zope2.Startup.datatypes.default_zpublisher_encoding( + self.default_encoding) + def load_config_text(self, text): # We have to create a directory of our own since the existence # of the directory is checked. This handles this in a From 26e43b9c549333700cd9de818263498bb321061b Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Wed, 3 Oct 2018 16:02:39 +0200 Subject: [PATCH 3/3] Prevent breaking page rendering when setting `default-zpublisher-encoding` in `zope.conf` on Python 2. --- CHANGES.rst | 4 ++++ src/Zope2/Startup/datatypes.py | 3 +++ src/Zope2/Startup/tests/test_schema.py | 3 +++ 3 files changed, 10 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 434f1a6c98..b2a75ad1b9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -51,6 +51,10 @@ Bugfixes when reading request bodies not encoded as application/x-www-form-urlencoded or multipart/form-data. +- Prevent breaking page rendering when setting `default-zpublisher-encoding` + in `zope.conf` on Python 2. + (`#308 `_) + 4.0b5 (2018-05-18) ------------------ diff --git a/src/Zope2/Startup/datatypes.py b/src/Zope2/Startup/datatypes.py index 9a7abce34a..0556f9bd56 100644 --- a/src/Zope2/Startup/datatypes.py +++ b/src/Zope2/Startup/datatypes.py @@ -206,6 +206,9 @@ def default_zpublisher_encoding(value): # so a module-level call to getConfiguration in any of them # results in getting config data structure without the necessary # value in it. + if PY2: + # unicode is not acceptable as encoding in HTTP headers: + value = str(value) from ZPublisher import Converters, HTTPRequest, HTTPResponse Converters.default_encoding = value HTTPRequest.default_encoding = value diff --git a/src/Zope2/Startup/tests/test_schema.py b/src/Zope2/Startup/tests/test_schema.py index e2665352ac..f227c97fc7 100644 --- a/src/Zope2/Startup/tests/test_schema.py +++ b/src/Zope2/Startup/tests/test_schema.py @@ -124,3 +124,6 @@ def test_default_zpublisher_encoding(self): default-zpublisher-encoding iso-8859-15 """) self.assertEqual(conf.default_zpublisher_encoding, 'iso-8859-15') + self.assertEqual( + ZPublisher.HTTPRequest.default_encoding, 'iso-8859-15') + self.assertEqual(type(ZPublisher.HTTPRequest.default_encoding), str)