我有一系列在特拉维斯CI上运行的单元测试,在PY3.2上只有只有,它会肚子痛。我怎样才能解决这个问题,而不使用six.u()?在Python 3.2上缺少U字符串?
def test_parse_utf8(self):
s = String("foo", 12, encoding="utf8")
self.assertEqual(s.parse(b"hello joh\xd4\x83n"), u"hello joh\u0503n")
======================================================================
ERROR: Failure: SyntaxError (invalid syntax (test_strings.py, line 37))
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/virtualenv/python3.2.5/lib/python3.2/site-packages/nose/failure.py", line 39, in runTest
raise self.exc_val.with_traceback(self.tb)
File "/home/travis/virtualenv/python3.2.5/lib/python3.2/site-packages/nose/loader.py", line 414, in loadTestsFromName
addr.filename, addr.module)
File "/home/travis/virtualenv/python3.2.5/lib/python3.2/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/home/travis/virtualenv/python3.2.5/lib/python3.2/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/home/travis/build/construct/construct/tests/test_strings.py", line 37
self.assertEqual(s.build(u"hello joh\u0503n"), b"hello joh\xd4\x83n")
^
SyntaxError: invalid syntax
试图得到这个工作:
PY3 = sys.version_info[0] == 3
def u(s): return s if PY3 else s.decode("utf-8")
self.assertEqual(s.parse(b"hello joh\xd4\x83n"), u("hello joh\u0503n"))
报价从https://pythonhosted.org/six/
在Python 2中,U()不知道是什么编码的字面意思是。 每个字节直接转换为同一个 值的unicode码点。因此,使用u()与ASCII数据的字符串 只是安全的。
但是,使用unicode的重点不仅限于ASCII。
是的,3.2只是没有这种语法。你是否需要使用相同的代码库来支持Python 2和Python 3.2,而不使用'2to3'? – user2357112
@ArekBulski:2to3永远不应该让你使用'six'。我不认为2to3中的任何代码都知道“six”。当我使用'u'文字对代码运行2to3时,它会剥掉'u'。 – user2357112