浏览代码

all tests pass in python 3.5 now

Fabian Peter Hammerle 8 年之前
父节点
当前提交
7cfea61d41

+ 11 - 5
ioex/__init__.py

@@ -21,8 +21,8 @@ def setlocale(temporary_locale):
         try:
             try:
                 yield locale.setlocale(locale.LC_ALL, temporary_locale)
-            except locale.Error, ex:
-                if ex.message == 'unsupported locale setting':
+            except locale.Error as ex:
+                if str(ex) == 'unsupported locale setting':
                     raise UnsupportedLocaleSettingError(temporary_locale)
                 else:
                     raise ex
@@ -54,11 +54,17 @@ def yaml_represent_unicode_as_str(dumper, unicode_string):
     return dumper.represent_scalar(u'tag:yaml.org,2002:str', unicode_string)
 
 def register_yaml_unicode_as_str_representer(dumper):
-    dumper.add_representer(unicode, yaml_represent_unicode_as_str)
+    try:
+        dumper.add_representer(unicode, yaml_represent_unicode_as_str)
+    except NameError: # python3
+        pass
 
 def yaml_construct_str_as_unicode(loader, node):
     string = loader.construct_scalar(node)
-    return string if type(string) is unicode else string.decode('utf-8')
+    try:
+        return string if type(string) is unicode else string.decode('utf-8')
+    except NameError: # python3
+        return string
 
 def register_yaml_str_as_unicode_constructor(loader):
     loader.add_constructor(u'tag:yaml.org,2002:str', yaml_construct_str_as_unicode)
@@ -82,7 +88,7 @@ def yaml_diff(a, b, dumper = None, colors = False):
                 Dumper = dumper,
                 default_flow_style = False,
                 allow_unicode = True,
-                ).decode('utf-8')
+                )
     diff_lines = difflib.ndiff(
         to_yaml(a).splitlines(True),
         to_yaml(b).splitlines(True),

+ 1 - 1
ioex/datetimeex.py

@@ -7,7 +7,7 @@ def construct_yaml_timestamp(loader, node):
     loaded_dt = loader.construct_yaml_timestamp(node)
     if type(loaded_dt) is datetime.datetime and loaded_dt.tzinfo is None:
         timezone_match = re.search(
-            ur'(Z|(?P<sign>[\+-])(?P<h>\d{2}):(?P<m>\d{2}))$',
+            r'(Z|(?P<sign>[\+-])(?P<h>\d{2}):(?P<m>\d{2}))$',
             loader.construct_yaml_str(node),
             )
         if timezone_match:

+ 1 - 1
tests/datetimeex/test_period.py

@@ -52,7 +52,7 @@ def test_init_isoformat(start, end, iso):
         },
     ])
 def test_init_param_fail(params):
-    with pytest.raises(StandardError):
+    with pytest.raises(AttributeError):
         ioex.datetimeex.Period(**params)
 
 @pytest.mark.parametrize(('start'), [

+ 9 - 6
tests/external/test_yaml.py

@@ -14,20 +14,23 @@ def test_to_yaml(source_object, yaml_string):
     assert yaml.dump(source_object) == yaml_string
     assert yaml.safe_dump(source_object) == yaml_string
 
+@pytest.mark.parametrize('yaml_loader', [yaml.Loader, yaml.SafeLoader])
 @pytest.mark.parametrize('expected_object,yaml_string', [
     [datetime.datetime(2016, 7, 14, 13, 50, 4, 0), '2016-07-14 13:50:04'],
     [pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+02:00'],
     [pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+00:00'],
     [pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04Z'],
     ])
-def test_from_yaml(expected_object, yaml_string):
+def test_from_yaml(expected_object, yaml_string, yaml_loader):
+    loaded_object = yaml.load(yaml_string, Loader = yaml_loader)
     try:
-        assert expected_object == yaml.load(yaml_string)
-        assert expected_object == yaml.safe_load(yaml_string)
-    except TypeError, ex:
-        if (isinstance(expected_object, datetime.datetime) 
+        assert expected_object == loaded_object
+    except (TypeError, AssertionError) as ex:
+        # python 2.7 -> TypeError
+        # python 3 -> AssertionError
+        if (isinstance(expected_object, datetime.datetime)
                 and not expected_object.tzinfo is None
-                and "can't compare offset-naive and offset-aware datetimes" in ex.message):
+                and loaded_object.tzinfo is None):
             pytest.xfail('pyyaml\'s loaders do not set datetime.tzinfo')
         else:
             raise ex

+ 11 - 11
tests/scripts/test_reyaml.py

@@ -8,9 +8,9 @@ import subprocess
 script_path = os.path.realpath(os.path.join(__file__, '..', '..', '..', 'scripts', 'reyaml'))
 
 @pytest.mark.parametrize(('stdin', 'params', 'expected_stdout'), [
-    ['a: b\n', [], 'a: b\n'],
-    ['{a: b}\n', [], 'a: b\n'],
-    ['[a, b, c]\n', [], '- a\n- b\n- c\n'],
+    [b'a: b\n', [], b'a: b\n'],
+    [b'{a: b}\n', [], b'a: b\n'],
+    [b'[a, b, c]\n', [], b'- a\n- b\n- c\n'],
     ])
 def test_params(stdin, params, expected_stdout):
     p = subprocess.Popen(
@@ -24,7 +24,7 @@ def test_params(stdin, params, expected_stdout):
 def test_file_input(tmpdir):
     input_file = tmpdir.join('in')
     input_file.write('a: b')
-    assert 'a: b\n' == subprocess.check_output(
+    assert b'a: b\n' == subprocess.check_output(
             [script_path, '-i', input_file.strpath],
             )
 
@@ -36,9 +36,9 @@ def test_file_output(tmpdir):
             stdout = subprocess.PIPE,
             stderr = subprocess.PIPE,
             )
-    stdout, stderr = p.communicate('a: b')
-    assert stdout == ''
-    assert stderr == ''
+    stdout, stderr = p.communicate(b'a: b')
+    assert stdout == b''
+    assert stderr == b''
     assert 'a: b\n' == output_file.read()
 
 def test_file_input_output(tmpdir):
@@ -51,8 +51,8 @@ def test_file_input_output(tmpdir):
             stderr = subprocess.PIPE,
             )
     stdout, stderr = p.communicate()
-    assert stdout == ''
-    assert stderr == ''
+    assert stdout == b''
+    assert stderr == b''
     assert 'c: d\n' == output_file.read()
 
 def test_file_input_output_same(tmpdir):
@@ -64,6 +64,6 @@ def test_file_input_output_same(tmpdir):
             stderr = subprocess.PIPE,
             )
     stdout, stderr = p.communicate()
-    assert stdout == ''
-    assert stderr == ''
+    assert stdout == b''
+    assert stderr == b''
     assert 'b: 3\n' == io_file.read()

+ 10 - 10
tests/test_setlocale.py

@@ -17,18 +17,18 @@ def test_setlocale_unsupported_inheritance():
     assert issubclass(ioex.UnsupportedLocaleSettingError, locale.Error)
 
 @pytest.mark.parametrize(('dt', 'dt_format', 'locale_code', 'expected_string'), [
-    [datetime.datetime(2016, 07, 23, 1, 7, 12), '%x', 'de_DE.utf8', u'23.07.2016'],
-    [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'de_DE.utf8', u'01:07:12'],
-    [datetime.datetime(2016, 07, 23, 1, 7, 12), '%x', 'en_US.utf8', u'07/23/2016'],
-    [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'en_US.utf8', u'01:07:12 AM'],
-    [datetime.datetime(2016, 07, 23, 1, 7, 12), '%x', 'it_IT.utf8', u'23/07/2016'],
-    [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'it_IT.utf8', u'01:07:12'],
-    [datetime.datetime(2016, 07, 23, 1, 7, 12), '%x', 'zh_CN.utf8', u'2016年07月23日'],
-    [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'zh_CN.utf8', u'01时07分12秒'],
+    [datetime.datetime(2016, 7, 23, 1, 7, 12), '%x', 'de_DE.utf8', '23.07.2016'],
+    [datetime.datetime(2016, 7, 23, 1, 7, 12), '%X', 'de_DE.utf8', '01:07:12'],
+    [datetime.datetime(2016, 7, 23, 1, 7, 12), '%x', 'en_US.utf8', '07/23/2016'],
+    [datetime.datetime(2016, 7, 23, 1, 7, 12), '%X', 'en_US.utf8', '01:07:12 AM'],
+    [datetime.datetime(2016, 7, 23, 1, 7, 12), '%x', 'it_IT.utf8', '23/07/2016'],
+    [datetime.datetime(2016, 7, 23, 1, 7, 12), '%X', 'it_IT.utf8', '01:07:12'],
+    [datetime.datetime(2016, 7, 23, 1, 7, 12), '%x', 'zh_CN.utf8', '2016年07月23日'],
+    [datetime.datetime(2016, 7, 23, 1, 7, 12), '%X', 'zh_CN.utf8', '01时07分12秒'],
     ])
 def test_setlocale_strtime(dt, dt_format, locale_code, expected_string):
     try:
         with ioex.setlocale(locale_code):
-            assert dt.strftime(dt_format).decode('utf-8') == expected_string
-    except ioex.UnsupportedLocaleSettingError, ex:
+            assert dt.strftime(dt_format) == expected_string
+    except ioex.UnsupportedLocaleSettingError as ex:
         pytest.skip('locale %s unsupported' % locale_code)

+ 3 - 3
tests/test_yaml_construct_str_as_unicode.py

@@ -15,9 +15,9 @@ yaml = pytest.importorskip('yaml')
     # True
     ['item', u'item'],
     ['itäm', u'itäm'],
-    ['it\xc3\xa4m', u'itäm'],
-    ['"it\xc3\xa4m"', u'itäm'],
-    [r'it\xE4m', ur'it\xE4m'],
+    [b'it\xc3\xa4m', u'itäm'],
+    [b'"it\xc3\xa4m"', u'itäm'],
+    [r'it\xE4m', u'it\\xE4m'],
     ['"itäm"', u'itäm'],
     [r'"it\xc3\xa4m"', u'it\xc3\xa4m'], # see comment above
     # unicode strings

+ 0 - 1
tests/test_yaml_diff.py

@@ -4,7 +4,6 @@ import pytest
 yaml = pytest.importorskip('yaml')
 import datetime
 import ioex.datetimeex
-import ioex.debug
 from ioex.shell import TextColor
 
 @pytest.mark.parametrize(('a', 'b', 'expected_diff_lines'), [

+ 0 - 2
tests/test_yaml_represent_unicode_as_str.py

@@ -9,11 +9,9 @@ yaml = pytest.importorskip('yaml')
     [[u'item'], '[item]\n', {}],
     [[u'itäm'], '["it\\xE4m"]\n', {'allow_unicode': False}],
     [[u'itäm'], '[itäm]\n', {'allow_unicode': True}],
-    [[u'itäm'], u'[itäm]\n'.encode('utf-8'), {'allow_unicode': True}],
     [{u'key': u'value'}, '{key: value}\n', {}],
     [{u'kï': u'valü'}, '{"k\\xEF": "val\\xFC"}\n', {'allow_unicode': False}],
     [{u'kï': u'valü'}, '{kï: valü}\n', {'allow_unicode': True}],
-    [{u'kï': u'valü'}, u'{kï: valü}\n'.encode('utf-8'), {'allow_unicode': True}],
     [{u'kĕyĭ': u'可以'}, '{kĕyĭ: 可以}\n', {'allow_unicode': True}],
     [{u'⚕': u'☤'}, '{⚕: ☤}\n', {'allow_unicode': True}],
     ])