2011-07-27 22 views
3

我有,只能接受在Python 2.x的Unicode文本測試爲的UnicodeDecodeError在Python 3

def testNonUnicodeInput(self): 
     """ Test falure on non-unicode input. """ 
     input = "foo".encode('utf-16') 
     self.assertRaises(UnicodeDecodeError, myfunction, input) 

但是功能下面的測試,在Python 3.x的運行時測試失敗我得到:

AssertionError: UnicodeDecodeError not raised by myfunction 

我試圖找出如何建立一個測試,將繼續在Python 2.x到工作,也將通過2to3的Python的3.x中被運行後工作

我也許應該注意到,我做了以下在我的功能給力的Unicode:

def myfunction(input): 
    """ myfunction only accepts unicode input. """ 
    ... 
    try: 
     source = unicode(source) 
    except UnicodeDecodeError, e: 
     # Customise error message while maintaining original trackback 
     e.reason += '. -- Note: Myfunction only accepts unicode input!' 
     raise 
    ... 

當然,這(與測試一起)被正在對Python 3的運行之前通過2to3的運行。X。我想我真正想要在Python 3上的是不接受字節串​​,我雖然我通過首先編碼字符串。我沒有使用'utf-8'作爲編碼,因爲我知道這是默認設置。

任何人有任何關於一致性的想法嗎?

回答

3

你不應該對Python 3字符串做任何事情;他們都是Unicode。只需測試isinstance(s,str)。或者,如果問題是相反的,你會想使用bytes.decode()。


好吧,想以此引起UnicodeDecodeError在Python 3和Python 2都:

的Python 3:

>>> "foo".encode('utf-16').decode('utf-8') 
Traceback (most recent call last): 
    File "<pyshell#61>", line 1, in <module> 
"foo".encode('utf-16').decode('utf-8') 
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte 

的Python 2:

>>> "foo".encode('utf-16').decode('utf-8') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Python26\lib\encodings\utf_8.py", line 16, in decode 
    return codecs.utf_8_decode(input, errors, True) 
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte 

不知道2to3會自動將字符串文字轉換爲b"foo"語法,但是。如果是這樣,您只需手動取出b,或將其設置爲以某種方式忽略。

+0

這就是我的想法,但測試失敗。或者我應該跳過Python 3中的這個測試嗎? – Waylan

0

那麼,我決定現在就跳過Python 3下的測試。

if sys.version_info < (3, 0): 
    input = "foo".encode('utf-16') 
    self.assertRaises(UnicodeDecodeError, myfunction, input 

但是,如果有人能提出一個測試,將通過Python的2 & 3下,我很開放的建議。

+0

如果sys.version_info <(3,0)else b「foo」',爲什麼不只是'input =「foo」.encode('utf-16')?至少Python 2.6似乎在字符串文字之前接受'b'就好了。等等,我只是想到了一個解決方案。更新我的答案,所以檢查出來。 – JAB