]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Test/CI: Make doctests run on both Py2 and Py3
authorAarni Koskela <akx@iki.fi>
Mon, 21 Dec 2015 08:15:07 +0000 (10:15 +0200)
committerAarni Koskela <akx@iki.fi>
Mon, 21 Dec 2015 08:52:24 +0000 (10:52 +0200)
Fixes #293

12 files changed:
babel/core.py
babel/dates.py
babel/localedata.py
babel/messages/catalog.py
babel/messages/extract.py
babel/messages/frontend.py
babel/messages/mofile.py
babel/messages/pofile.py
babel/support.py
babel/util.py
conftest.py
setup.cfg

index 0b314cd3a67bcf1fd29806d63f7d1ea417d0eab7..84ee1892012e4fcbf7479aeefef598337b75b4d2 100644 (file)
@@ -544,9 +544,9 @@ class Locale(object):
     def currency_formats(self):
         """Locale patterns for currency number formatting.
 
-        >>> print Locale('en', 'US').currency_formats['standard']
+        >>> Locale('en', 'US').currency_formats['standard']
         <NumberPattern u'\\xa4#,##0.00'>
-        >>> print Locale('en', 'US').currency_formats['accounting']
+        >>> Locale('en', 'US').currency_formats['accounting']
         <NumberPattern u'\\xa4#,##0.00'>
         """
         return self._data['currency_formats']
index f9498d80a6c654ff02b2b58dfed17f297705dcb1..56677030fb1935b9c0ac6a84a48f3b69a9d6f729 100644 (file)
@@ -539,7 +539,7 @@ def get_timezone_name(dt_or_tzinfo=None, width='long', uncommon=False,
 def format_date(date=None, format='medium', locale=LC_TIME):
     """Return a date formatted according to the given pattern.
 
-    >>> d = date(2007, 04, 01)
+    >>> d = date(2007, 4, 1)
     >>> format_date(d, locale='en_US')
     u'Apr 1, 2007'
     >>> format_date(d, format='full', locale='de_DE')
@@ -573,7 +573,7 @@ def format_datetime(datetime=None, format='medium', tzinfo=None,
                     locale=LC_TIME):
     r"""Return a date formatted according to the given pattern.
 
-    >>> dt = datetime(2007, 04, 01, 15, 30)
+    >>> dt = datetime(2007, 4, 1, 15, 30)
     >>> format_datetime(dt, locale='en_US')
     u'Apr 1, 2007, 3:30:00 PM'
 
index 79b4d8639866a592abc9bcd8b81fef7b675469ad..437f49fae39f5a7be3563efeb1c0ddc2da590047 100644 (file)
@@ -111,7 +111,7 @@ def merge(dict1, dict2):
 
     >>> d = {1: 'foo', 3: 'baz'}
     >>> merge(d, {1: 'Foo', 2: 'Bar'})
-    >>> items = d.items(); items.sort(); items
+    >>> sorted(d.items())
     [(1, 'Foo'), (2, 'Bar'), (3, 'baz')]
 
     :param dict1: the dictionary to merge into
index 12e8783480a8873d30b93ac1dec6153f6a3ee1ab..e289bef7606f95890403de723012ec18ed56e951 100644 (file)
@@ -331,7 +331,7 @@ class Catalog(object):
 
     >>> catalog = Catalog(project='Foobar', version='1.0',
     ...                   copyright_holder='Foo Company')
-    >>> print catalog.header_comment #doctest: +ELLIPSIS
+    >>> print(catalog.header_comment) #doctest: +ELLIPSIS
     # Translations template for Foobar.
     # Copyright (C) ... Foo Company
     # This file is distributed under the same license as the Foobar project.
@@ -349,7 +349,7 @@ class Catalog(object):
     ... # This file is distributed under the same license as the PROJECT
     ... # project.
     ... #'''
-    >>> print catalog.header_comment
+    >>> print(catalog.header_comment)
     # The POT for my really cool Foobar project.
     # Copyright (C) 1990-2003 Foo Company
     # This file is distributed under the same license as the Foobar
@@ -433,7 +433,7 @@ class Catalog(object):
     >>> catalog = Catalog(project='Foobar', version='1.0',
     ...                   creation_date=created)
     >>> for name, value in catalog.mime_headers:
-    ...     print '%s: %s' % (name, value)
+    ...     print('%s: %s' % (name, value))
     Project-Id-Version: Foobar 1.0
     Report-Msgid-Bugs-To: EMAIL@ADDRESS
     POT-Creation-Date: 1990-04-01 15:30+0000
@@ -453,7 +453,7 @@ class Catalog(object):
     ...                   last_translator='John Doe <jd@example.com>',
     ...                   language_team='de_DE <de@example.com>')
     >>> for name, value in catalog.mime_headers:
-    ...     print '%s: %s' % (name, value)
+    ...     print('%s: %s' % (name, value))
     Project-Id-Version: Foobar 1.0
     Report-Msgid-Bugs-To: EMAIL@ADDRESS
     POT-Creation-Date: 1990-04-01 15:30+0000
@@ -720,7 +720,7 @@ class Catalog(object):
 
         >>> 'head' in catalog
         False
-        >>> catalog.obsolete.values()
+        >>> list(catalog.obsolete.values())
         [<Message 'head' (flags: [])>]
 
         :param template: the reference catalog, usually read from a POT file
index a0608d797d464c5aaafd3d7dc4810a683ad3d8b6..be2e6303ca4dcc92304eb6d860ff8de3ecd3ab99 100644 (file)
@@ -202,14 +202,14 @@ def extract(method, fileobj, keywords=DEFAULT_KEYWORDS, comment_tags=(),
     The implementation dispatches the actual extraction to plugins, based on the
     value of the ``method`` parameter.
 
-    >>> source = '''# foo module
+    >>> source = b'''# foo module
     ... def run(argv):
-    ...    print _('Hello, world!')
+    ...    print(_('Hello, world!'))
     ... '''
 
-    >>> from StringIO import StringIO
-    >>> for message in extract('python', StringIO(source)):
-    ...     print message
+    >>> from babel._compat import BytesIO
+    >>> for message in extract('python', BytesIO(source)):
+    ...     print(message)
     (3, u'Hello, world!', [], None)
 
     :param method: an extraction method (a callable), or
index cd79ebf201b187ee762f31d2182751a2f6a146cb..56f1b76778596c6664d9afb155bd97f57642d153 100755 (executable)
@@ -8,7 +8,7 @@
     :copyright: (c) 2013 by the Babel Team.
     :license: BSD, see LICENSE for more details.
 """
-
+from __future__ import print_function
 try:
     from ConfigParser import RawConfigParser
 except ImportError:
@@ -35,7 +35,7 @@ from babel.messages.extract import extract_from_dir, DEFAULT_KEYWORDS, \
 from babel.messages.mofile import write_mo
 from babel.messages.pofile import read_po, write_po
 from babel.util import odict, LOCALTZ
-from babel._compat import string_types, BytesIO, PY2
+from babel._compat import string_types, StringIO, PY2
 
 
 class compile_catalog(Command):
@@ -332,7 +332,7 @@ class extract_messages(Command):
             message_extractors = self.distribution.message_extractors
             for dirname, mapping in message_extractors.items():
                 if isinstance(mapping, string_types):
-                    method_map, options_map = parse_mapping(BytesIO(mapping))
+                    method_map, options_map = parse_mapping(StringIO(mapping))
                 else:
                     method_map, options_map = [], {}
                     for pattern, method, options in mapping:
@@ -1154,7 +1154,7 @@ def main():
 def parse_mapping(fileobj, filename=None):
     """Parse an extraction method mapping from a file-like object.
 
-    >>> buf = BytesIO(b'''
+    >>> buf = StringIO('''
     ... [extractors]
     ... custom = mypackage.module:myfunc
     ...
@@ -1227,10 +1227,9 @@ def parse_mapping(fileobj, filename=None):
 def parse_keywords(strings=[]):
     """Parse keywords specifications from the given list of strings.
 
-    >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3', 'pgettext:1c,2']).items()
-    >>> kw.sort()
+    >>> kw = sorted(parse_keywords(['_', 'dgettext:2', 'dngettext:2,3', 'pgettext:1c,2']).items())
     >>> for keyword, indices in kw:
-    ...     print (keyword, indices)
+    ...     print((keyword, indices))
     ('_', None)
     ('dgettext', (2,))
     ('dngettext', (2, 3))
index 18503287b1301ba44006b39763ac564271758d08..2ab96d523291d581faad13d4803578a0f05c3f5f 100644 (file)
@@ -108,9 +108,10 @@ def write_mo(fileobj, catalog, use_fuzzy=False):
     """Write a catalog to the specified file-like object using the GNU MO file
     format.
 
+    >>> import sys
     >>> from babel.messages import Catalog
     >>> from gettext import GNUTranslations
-    >>> from StringIO import StringIO
+    >>> from babel._compat import BytesIO
 
     >>> catalog = Catalog(locale='en_US')
     >>> catalog.add('foo', 'Voh')
@@ -123,11 +124,14 @@ def write_mo(fileobj, catalog, use_fuzzy=False):
     <Message ...>
     >>> catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
     <Message ...>
-    >>> buf = StringIO()
+    >>> buf = BytesIO()
 
     >>> write_mo(buf, catalog)
-    >>> buf.seek(0)
+    >>> x = buf.seek(0)
     >>> translations = GNUTranslations(fp=buf)
+    >>> if sys.version_info[0] >= 3:
+    ...     translations.ugettext = translations.gettext
+    ...     translations.ungettext = translations.ngettext
     >>> translations.ugettext('foo')
     u'Voh'
     >>> translations.ungettext('bar', 'baz', 1)
index 41fad0a8af6f4b8f4ac8a022015a99a1c550e30b..226ac1ce9536932aa119e2c2c8e1ac608c741d59 100644 (file)
@@ -10,6 +10,7 @@
     :license: BSD, see LICENSE for more details.
 """
 
+from __future__ import print_function
 import os
 import re
 
@@ -21,7 +22,7 @@ from babel._compat import text_type
 def unescape(string):
     r"""Reverse `escape` the given string.
 
-    >>> print unescape('"Say:\\n  \\"hello, world!\\"\\n"')
+    >>> print(unescape('"Say:\\n  \\"hello, world!\\"\\n"'))
     Say:
       "hello, world!"
     <BLANKLINE>
@@ -44,18 +45,18 @@ def unescape(string):
 def denormalize(string):
     r"""Reverse the normalization done by the `normalize` function.
 
-    >>> print denormalize(r'''""
+    >>> print(denormalize(r'''""
     ... "Say:\n"
-    ... "  \"hello, world!\"\n"''')
+    ... "  \"hello, world!\"\n"'''))
     Say:
       "hello, world!"
     <BLANKLINE>
 
-    >>> print denormalize(r'''""
+    >>> print(denormalize(r'''""
     ... "Say:\n"
     ... "  \"Lorem ipsum dolor sit "
     ... "amet, consectetur adipisicing"
-    ... " elit, \"\n"''')
+    ... " elit, \"\n"'''))
     Say:
       "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
     <BLANKLINE>
@@ -77,7 +78,7 @@ def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False, charset=No
     file-like object and return a `Catalog`.
 
     >>> from datetime import datetime
-    >>> from StringIO import StringIO
+    >>> from babel._compat import StringIO
     >>> buf = StringIO('''
     ... #: main.py:1
     ... #, fuzzy, python-format
@@ -93,13 +94,13 @@ def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False, charset=No
     ... msgstr[1] "baaz"
     ... ''')
     >>> catalog = read_po(buf)
-    >>> catalog.revision_date = datetime(2007, 04, 01)
+    >>> catalog.revision_date = datetime(2007, 4, 1)
 
     >>> for message in catalog:
     ...     if message.id:
-    ...         print (message.id, message.string)
-    ...         print ' ', (message.locations, sorted(list(message.flags)))
-    ...         print ' ', (message.user_comments, message.auto_comments)
+    ...         print((message.id, message.string))
+    ...         print(' ', (message.locations, sorted(list(message.flags))))
+    ...         print(' ', (message.user_comments, message.auto_comments))
     (u'foo %(name)s', u'quux %(name)s')
       ([(u'main.py', 1)], [u'fuzzy', u'python-format'])
       ([], [])
@@ -278,16 +279,16 @@ def escape(string):
 def normalize(string, prefix='', width=76):
     r"""Convert a string into a format that is appropriate for .po files.
 
-    >>> print normalize('''Say:
+    >>> print(normalize('''Say:
     ...   "hello, world!"
-    ... ''', width=None)
+    ... ''', width=None))
     ""
     "Say:\n"
     "  \"hello, world!\"\n"
 
-    >>> print normalize('''Say:
+    >>> print(normalize('''Say:
     ...   "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
-    ... ''', width=32)
+    ... ''', width=32))
     ""
     "Say:\n"
     "  \"Lorem ipsum dolor sit "
@@ -348,10 +349,10 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False,
     <Message...>
     >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)])
     <Message...>
-    >>> from io import BytesIO
+    >>> from babel._compat import BytesIO
     >>> buf = BytesIO()
     >>> write_po(buf, catalog, omit_header=True)
-    >>> print buf.getvalue()
+    >>> print(buf.getvalue().decode("utf8"))
     #: main.py:1
     #, fuzzy, python-format
     msgid "foo %(name)s"
index 5ab97a5b23fdcc6a23943aab5dddd8abe00d382d..3b4869cdb90949419904f7197f24f2ac73f8edba 100644 (file)
@@ -137,7 +137,7 @@ class LazyProxy(object):
     >>> def greeting(name='world'):
     ...     return 'Hello, %s!' % name
     >>> lazy_greeting = LazyProxy(greeting, name='Joe')
-    >>> print lazy_greeting
+    >>> print(lazy_greeting)
     Hello, Joe!
     >>> u'  ' + lazy_greeting
     u'  Hello, Joe!'
@@ -160,7 +160,7 @@ class LazyProxy(object):
     ... ]
     >>> greetings.sort()
     >>> for greeting in greetings:
-    ...     print greeting
+    ...     print(greeting)
     Hello, Joe!
     Hello, universe!
     Hello, world!
index 1c7264594d8feea3423e81f743b74528efe90e79..0f3fb994730635954ab84ba752c34dc0417d60e0 100644 (file)
@@ -26,9 +26,9 @@ def distinct(iterable):
     Unlike when using sets for a similar effect, the original ordering of the
     items in the collection is preserved by this function.
 
-    >>> print list(distinct([1, 2, 1, 3, 4, 4]))
+    >>> print(list(distinct([1, 2, 1, 3, 4, 4])))
     [1, 2, 3, 4]
-    >>> print list(distinct('foobar'))
+    >>> print(list(distinct('foobar')))
     ['f', 'o', 'b', 'a', 'r']
 
     :param iterable: the iterable collection providing the data
index 15a589a6c17fe9067f0e493716018914e70bb02e..32bd1362aabf4f3d0ccd85074ae1f92cf1c20370 100644 (file)
@@ -1,18 +1,11 @@
-import sys
 from _pytest.doctest import DoctestModule
 from py.path import local
 
-
-PY2 = sys.version_info[0] < 3
-
-
 collect_ignore = ['tests/messages/data', 'setup.py']
+babel_path = local(__file__).dirpath().join('babel')
 
 
 def pytest_collect_file(path, parent):
-    babel_path = local(__file__).dirpath().join('babel')
-    config = parent.config
-    if PY2:
-        if babel_path.common(path) == babel_path:
-            if path.ext == ".py":
-                return DoctestModule(path, parent)
+    if babel_path.common(path) == babel_path:
+        if path.ext == ".py":
+            return DoctestModule(path, parent)
index 8069749f56c24d88f05763a06f2f8c342b234db2..c2d8f87e9e45c65c9bf9a310875484df18057198 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -3,7 +3,7 @@ release = sdist bdist_wheel
 
 [pytest]
 norecursedirs = venv* .* _* scripts {args}
-doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE ALLOW_UNICODE
+doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE ALLOW_UNICODE IGNORE_EXCEPTION_DETAIL
 
 [bdist_wheel]
 universal = 1