]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Python 3: skip test_json_with_utf8 on Py3
authorCyril Roelandt <cyril@redhat.com>
Mon, 12 Oct 2015 14:07:54 +0000 (16:07 +0200)
committerCedric Brandily <zzelle@gmail.com>
Thu, 15 Oct 2015 08:44:53 +0000 (08:44 +0000)
The last skipped unit test should indeed be skipped: it does not make
sense to run it with Python 3.

$ python 2
>>> import json; json.dumps({'a': u'\xe9'.encode('utf-8')})
'{"a": "\\u00e9"}'

$ python3
>>> import json; json.dumps({'a': u'\xe9'.encode('utf-8')})
Traceback (most recent call last):
  ...
  File "/usr/lib64/python3.4/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'\xc3\xa9' is not JSON serializable

In Python 2, the JSON encoder function to encode a string begins with
a magic test to convert UTF-8 encoded string to Unicode:

    if isinstance(s, str) and HAS_UTF8.search(s) is not None:
        s = s.decode('utf-8')

https://hg.python.org/cpython/file/4188cd5dc0c5/Lib/json/encoder.py#l42

This trick is gone in Python 3:

https://hg.python.org/cpython/file/288953a787ce/Lib/json/encoder.py#l49

Change-Id: Iddaaea4ebedf04c87f1ff9f9098163a15ffa78f7
Blueprint: neutron-python3
Closes-Bug: #1491824

neutron/tests/unit/test_wsgi.py

index c6d4ee961d83e01acad7f230dc9f5b0541cb63a5..190299967777a93a46770c2575763990740c8d99 100644 (file)
@@ -494,8 +494,9 @@ class JSONDictSerializerTest(base.BaseTestCase):
 
         self.assertEqual(expected_json, result)
 
-    # TODO(cbrandily): support this test in py3K
-    @testtools.skipIf(six.PY3, "bug/1491824")
+    # The tested behaviour is only meant to be witnessed in Python 2, so it is
+    # OK to skip this test with Python 3.
+    @testtools.skipIf(six.PY3, "This test does not make sense in Python 3")
     def test_json_with_utf8(self):
         input_dict = dict(servers=dict(a=(2, '\xe7\xbd\x91\xe7\xbb\x9c')))
         expected_json = b'{"servers":{"a":[2,"\\u7f51\\u7edc"]}}'