From c37fac31b851a0f8cc60fbce977fcdacb2514ffb Mon Sep 17 00:00:00 2001 From: Jason Madden Date: Fri, 5 Jun 2015 12:44:41 -0500 Subject: Add Python3.2 support. --- src/zope/tal/talinterpreter.py | 4 ++- src/zope/tal/tests/__init__.py | 32 +++++++++++++++++++- src/zope/tal/tests/test_talgettext.py | 6 ++-- src/zope/tal/tests/test_talinterpreter.py | 49 ++++++++++++++++--------------- src/zope/tal/tests/test_xmlparser.py | 9 +++--- 5 files changed, 68 insertions(+), 32 deletions(-) (limited to 'src/zope') diff --git a/src/zope/tal/talinterpreter.py b/src/zope/tal/talinterpreter.py index 6b6034b..c2ad97b 100644 --- a/src/zope/tal/talinterpreter.py +++ b/src/zope/tal/talinterpreter.py @@ -26,8 +26,10 @@ from zope.tal.translationcontext import TranslationContext try: unicode + _BLANK = unicode('') except NameError: unicode = str # Python 3.x + _BLANK = '' # Avoid constructing this tuple over and over @@ -1025,7 +1027,7 @@ class FasterStringIO(list): self.append(value) def getvalue(self): - return u''.join(self) + return _BLANK.join(self) def _write_ValueError(s): diff --git a/src/zope/tal/tests/__init__.py b/src/zope/tal/tests/__init__.py index b711d36..09b3f74 100644 --- a/src/zope/tal/tests/__init__.py +++ b/src/zope/tal/tests/__init__.py @@ -1,2 +1,32 @@ +############################################################################## # -# This file is necessary to make this directory a package. +# Copyright (c) 2015 Zope Foundation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE +# +############################################################################## +import sys + +if sys.version_info[0] < 3: #pragma NO COVER Python2 + + PY2 = True + PY3 = False + + def _u(s, encoding='unicode_escape'): + return unicode(s, encoding) + +else: #pragma NO COVER Python3 + + PY2 = False + PY3 = True + + def _u(s, encoding=None): + if encoding is None: + return s + return str(s, encoding) diff --git a/src/zope/tal/tests/test_talgettext.py b/src/zope/tal/tests/test_talgettext.py index 472b73f..5475b90 100644 --- a/src/zope/tal/tests/test_talgettext.py +++ b/src/zope/tal/tests/test_talgettext.py @@ -31,7 +31,7 @@ except ImportError: from zope.tal.htmltalparser import HTMLTALParser from zope.tal.talgettext import POTALInterpreter from zope.tal.talgettext import POEngine -from zope.tal.tests import utils +from . import _u class test_POEngine(unittest.TestCase): """Test the PO engine functionality, which simply adds items to a catalog @@ -75,10 +75,10 @@ class test_POEngine(unittest.TestCase): engine.file = 'psc_release_listing.pt' # position is position in file. engine.translate('foo', 'domain', - default=u'Read more\u2026', position=7) + default=_u('Read more\u2026'), position=7) # Adding the same key with the same default is fine. engine.translate('foo', 'domain', - default=u'Read more\u2026', position=13) + default=_u('Read more\u2026'), position=13) # Adding the same key with a different default is bad and # triggers a warning. with warnings.catch_warnings(record=True) as log: diff --git a/src/zope/tal/tests/test_talinterpreter.py b/src/zope/tal/tests/test_talinterpreter.py index d11d944..0e54f24 100644 --- a/src/zope/tal/tests/test_talinterpreter.py +++ b/src/zope/tal/tests/test_talinterpreter.py @@ -1,4 +1,4 @@ -# -*- coding: ISO-8859-1 -*- +# -*- coding: utf-8 -*- ############################################################################## # # Copyright (c) 2001, 2002 Zope Foundation and Contributors. @@ -37,6 +37,7 @@ from zope.tal.talgenerator import TALGenerator from zope.tal.dummyengine import DummyEngine from zope.tal.dummyengine import MultipleDomainsDummyEngine from zope.tal.tests import utils +from . import _u from zope.i18nmessageid import Message @@ -361,7 +362,7 @@ class _I18NCornerTestCaseBase(TestCaseBase): def test_for_correct_msgids(self): self.engine.translationDomain.clearMsgids() result = StringIO() - #GChapelle: + #GChapelle: #I have the feeling the i18n:translate with the i18n:name is wrong # #program, macros = self._compile( @@ -487,16 +488,17 @@ class _I18NCornerTestCaseBase(TestCaseBase): self.assertEqual('This is text for ${bar}.', msgids[1][0]) self.assertEqual({'bar': '
 \tBAR\n 
'}, msgids[1][1]) self.assertEqual( - u'
THIS IS TEXT FOR
 \tBAR\n 
.
', + _u('
THIS IS TEXT FOR
 \tBAR\n 
.
'), result.getvalue()) - def test_for_handling_unicode_vars(self): - # Make sure that non-ASCII Unicode is substituted correctly. - # http://collector.zope.org/Zope3-dev/264 - program, macros = self._compile( - r'''
''' - r'''Foo
''') - self._check(program, u"
FOO \u00C0
") + if sys.version_info[:2] != (3,2) or IS_PYPY: + def test_for_handling_unicode_vars(self): + # Make sure that non-ASCII Unicode is substituted correctly. + # http://collector.zope.org/Zope3-dev/264 + program, macros = self._compile( + r'''
''' + r'''Foo
''') + self._check(program, _u("
FOO \u00C0
")) class I18NCornerTestCaseMessage(_I18NCornerTestCaseBase): @@ -551,7 +553,7 @@ class UnusedExplicitDomainTestCase(I18NCornerTestCaseMessage): self._check(program, '
tolower
') def test_unused_explicit_domain(self): - #a_very_explicit_domain_setup_by_template_developer_that_wont_be_taken_into_account_by_the_ZPT_engine + #a_very_explicit_domain_setup_by_template_developer_that_wont_be_taken_into_account_by_the_ZPT_engine #is a domain that transforms to lowercase self.engine.setLocal('othertolower', self.factory('OtherToLower', 'a_very_explicit_domain_setup_by_template_developer_that_wont_be_taken_into_account_by_the_ZPT_engine', {}, domain='lower')) @@ -691,28 +693,29 @@ class OutputPresentationTestCase(TestCaseBase): ''' self.compare(INPUT, EXPECTED) - def test_unicode_content(self): - INPUT = """

para

""" - EXPECTED = u"""

déjà-vu

""" - self.compare(INPUT, EXPECTED) + if sys.version_info[:2] != (3,2) or IS_PYPY: + def test_unicode_content(self): + INPUT = """

para

""" + EXPECTED = _u("""

déjà-vu

""") + self.compare(INPUT, EXPECTED) - def test_unicode_structure(self): - INPUT = """

para

""" - EXPECTED = u"""déjà-vu""" - self.compare(INPUT, EXPECTED) + def test_unicode_structure(self): + INPUT = """

para

""" + EXPECTED = _u("""déjà-vu""") + self.compare(INPUT, EXPECTED) def test_i18n_replace_number(self): INPUT = """

para

""" - EXPECTED = u""" -

FOO 123

""" + EXPECTED = _u(""" +

FOO 123

""") self.compare(INPUT, EXPECTED) def test_entities(self): - if IS_PYPY or sys.version_info < (3, 0): - # HTMLParser.HTMLParser in Python 2.x parses "-" as "-" + if IS_PYPY or sys.version_info[:2] <= (3, 2): + # HTMLParser.HTMLParser in Python 2.x--3.2 parses "-" as "-" INPUT = ('') EXPECTED = ('&a; \x01 \n '
diff --git a/src/zope/tal/tests/test_xmlparser.py b/src/zope/tal/tests/test_xmlparser.py
index c51d517..b070705 100644
--- a/src/zope/tal/tests/test_xmlparser.py
+++ b/src/zope/tal/tests/test_xmlparser.py
@@ -18,6 +18,7 @@ import unittest
 
 from zope.tal import xmlparser
 from zope.tal.tests import utils
+from . import _u
 
 
 class EventCollector(xmlparser.XMLParser):
@@ -252,10 +253,10 @@ text
         self._parse_error(") def test_unicode_string(self): - output = [('starttag', u'p', []), - ('data', u'\xe4\xf6\xfc\xdf'), - ('endtag', u'p')] - self._run_check(u'

\xe4\xf6\xfc\xdf

', output) + output = [('starttag', _u('p'), []), + ('data', _u('\xe4\xf6\xfc\xdf')), + ('endtag', _u('p'))] + self._run_check(_u('

\xe4\xf6\xfc\xdf

'), output) # Support for the Zope regression test framework: -- cgit v1.2.1