diff --git a/deluge/common.py b/deluge/common.py index b68c1c776..1116572c1 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -659,20 +659,22 @@ def decode_string(s, encoding="utf8"): pass return u'' -def utf8_encoded(s): +def utf8_encoded(s, encoding="utf8"): """ Returns a utf8 encoded string of s :param s: (unicode) string to (re-)encode :type s: basestring + :keyword encoding: the encoding to use in the decoding + :type encoding: string :returns: a utf8 encoded string of s :rtype: str """ if isinstance(s, str): - s = decode_string(s).encode("utf8") + s = decode_string(s, encoding).encode("utf8") elif isinstance(s, unicode): - s = s.encode("utf8", "ignore") + s = s.encode("utf8") return s class VersionSplit(object): diff --git a/deluge/tests/test_ui_common.py b/deluge/tests/test_ui_common.py new file mode 100644 index 000000000..14d58e7b4 --- /dev/null +++ b/deluge/tests/test_ui_common.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +from twisted.trial import unittest +import os +from deluge.ui.common import TorrentInfo + +class UICommonTestCase(unittest.TestCase): + def setUp(self): + pass + + def tearDown(self): + pass + + def test_utf8_encoded_paths(self): + filename = os.path.join(os.path.dirname(__file__), "test.torrent") + ti = TorrentInfo(filename) + self.assertTrue(ti.files_tree.has_key("azcvsupdater_2.6.2.jar")) + + def test_utf8_encoded_paths2(self): + filename = os.path.join(os.path.dirname(__file__), "unicode_filenames.torrent") + ti = TorrentInfo(filename) + + files = ti.files_tree["unicode_filenames"] + self.assertTrue(files.has_key("\xe3\x83\x86\xe3\x82\xaf\xe3\x82\xb9\xe3\x83\xbb\xe3\x83" + "\x86\xe3\x82\xaf\xe3\x82\xb5\xe3\x83\xb3.mkv")) + self.assertTrue(files.has_key("\xd0\x9c\xd0\xb8\xd1\x85\xd0\xb0\xd0\xb8\xd0\xbb \xd0\x93" + "\xd0\xbe\xd1\x80\xd0\xb1\xd0\xb0\xd1\x87\xd1\x91\xd0\xb2.mkv")) + self.assertTrue(files.has_key("Alisher ibn G'iyosiddin Navoiy.mkv")) + self.assertTrue(files.has_key("Ascii title.mkv")) + self.assertTrue(files.has_key("\xe0\xa6\xb8\xe0\xa7\x81\xe0\xa6\x95\xe0\xa7\x81\xe0\xa6" + "\xae\xe0\xa6\xbe\xe0\xa6\xb0 \xe0\xa6\xb0\xe0\xa6\xbe\xe0\xa7\x9f.mkv")) diff --git a/deluge/tests/unicode_filenames.torrent b/deluge/tests/unicode_filenames.torrent new file mode 100644 index 000000000..e34f055df Binary files /dev/null and b/deluge/tests/unicode_filenames.torrent differ