[#3204|Core] Fix unicode get_name unicode error

The recent change to torrent.get_name does not handle non-ascii paths
on Python 2.

- Add a decode_bytes to resolve the issue.
- Add tests.
- Refactor to reduce nesting.
This commit is contained in:
Calum Lind 2018-10-22 10:45:37 +01:00
parent 9ab2a50097
commit b834e33568
3 changed files with 18 additions and 15 deletions

View File

@ -933,23 +933,18 @@ class Torrent(object):
str: the name of the torrent.
"""
if not self.options['name']:
# Use the top-level folder as torrent name.
if self.has_metadata:
handle_name = (
self.torrent_info.file_at(0)
.path.replace('\\', '/', 1)
.split('/', 1)[0]
)
else:
handle_name = self.handle.name()
if self.options['name']:
return self.options['name']
if handle_name:
name = decode_bytes(handle_name)
if self.has_metadata:
# Use the top-level folder as torrent name.
filename = decode_bytes(self.torrent_info.file_at(0).path)
name = filename.replace('\\', '/', 1).split('/', 1)[0]
else:
name = decode_bytes(self.handle.name())
if not name:
name = self.torrent_id
else:
name = self.options['name']
return name

View File

@ -0,0 +1 @@
d13:creation datei1540200743e8:encoding5:UTF-84:infod6:lengthi0e4:name35:সুকুমার রায়.mkv12:piece lengthi32768e6:pieces0:7:privatei0eee

View File

@ -297,3 +297,10 @@ class TorrentTestCase(BaseTestCase):
result = self.torrent.get_eta()
self.assertEqual(result, 100)
self.assertIsInstance(result, int)
def test_get_name_unicode(self):
"""Test retrieving a unicode torrent name from libtorrent."""
atp = self.get_torrent_atp('unicode_file.torrent')
handle = self.session.add_torrent(atp)
self.torrent = Torrent(handle, {})
self.assertEqual(self.torrent.get_name(), 'সুকুমার রায়.mkv')