From 62a368c31e5136ee1c33b4cf305fbb2a82778887 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 21 Dec 2015 10:15:07 +0200 Subject: Test/CI: Make doctests run on both Py2 and Py3 Fixes #293 --- babel/core.py | 4 ++-- babel/dates.py | 4 ++-- babel/localedata.py | 2 +- babel/messages/catalog.py | 10 +++++----- babel/messages/extract.py | 10 +++++----- babel/messages/frontend.py | 13 ++++++------- babel/messages/mofile.py | 10 +++++++--- babel/messages/pofile.py | 33 +++++++++++++++++---------------- babel/support.py | 4 ++-- babel/util.py | 4 ++-- conftest.py | 15 ++++----------- setup.cfg | 2 +- 12 files changed, 54 insertions(+), 57 deletions(-) diff --git a/babel/core.py b/babel/core.py index 0b314cd..84ee189 100644 --- a/babel/core.py +++ b/babel/core.py @@ -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'] - >>> print Locale('en', 'US').currency_formats['accounting'] + >>> Locale('en', 'US').currency_formats['accounting'] """ return self._data['currency_formats'] diff --git a/babel/dates.py b/babel/dates.py index f9498d8..5667703 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -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' diff --git a/babel/localedata.py b/babel/localedata.py index 79b4d86..437f49f 100644 --- a/babel/localedata.py +++ b/babel/localedata.py @@ -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 diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 12e8783..e289bef 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -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 ', ... language_team='de_DE ') >>> 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()) [] :param template: the reference catalog, usually read from a POT file diff --git a/babel/messages/extract.py b/babel/messages/extract.py index a0608d7..be2e630 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -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 diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index cd79ebf..56f1b76 100755 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -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)) diff --git a/babel/messages/mofile.py b/babel/messages/mofile.py index 1850328..2ab96d5 100644 --- a/babel/messages/mofile.py +++ b/babel/messages/mofile.py @@ -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): >>> catalog.add(('Fuzz', 'Fuzzes'), ('', '')) - >>> 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) diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 41fad0a..226ac1c 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -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!" @@ -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!" - >>> 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, " @@ -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, >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)]) - >>> 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" diff --git a/babel/support.py b/babel/support.py index 5ab97a5..3b4869c 100644 --- a/babel/support.py +++ b/babel/support.py @@ -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! diff --git a/babel/util.py b/babel/util.py index 1c72645..0f3fb99 100644 --- a/babel/util.py +++ b/babel/util.py @@ -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 diff --git a/conftest.py b/conftest.py index 15a589a..32bd136 100644 --- a/conftest.py +++ b/conftest.py @@ -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) diff --git a/setup.cfg b/setup.cfg index 8069749..c2d8f87 100644 --- 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 -- cgit v1.2.1