我有一系列在特拉維斯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