From b834e33568facc7c1402c98965b83f690f4d045c Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Mon, 22 Oct 2018 10:45:37 +0100 Subject: [PATCH] [#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. --- deluge/core/torrent.py | 25 ++++++++++--------------- deluge/tests/data/unicode_file.torrent | 1 + deluge/tests/test_torrent.py | 7 +++++++ 3 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 deluge/tests/data/unicode_file.torrent diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py index c9cd16ae3..ca854b33d 100644 --- a/deluge/core/torrent.py +++ b/deluge/core/torrent.py @@ -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) - else: - name = self.torrent_id + 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 = self.options['name'] + name = decode_bytes(self.handle.name()) + + if not name: + name = self.torrent_id return name diff --git a/deluge/tests/data/unicode_file.torrent b/deluge/tests/data/unicode_file.torrent new file mode 100644 index 000000000..41db239da --- /dev/null +++ b/deluge/tests/data/unicode_file.torrent @@ -0,0 +1 @@ +d13:creation datei1540200743e8:encoding5:UTF-84:infod6:lengthi0e4:name35:সুকুমার রায়.mkv12:piece lengthi32768e6:pieces0:7:privatei0eee diff --git a/deluge/tests/test_torrent.py b/deluge/tests/test_torrent.py index 4d028fda9..747aa33ea 100644 --- a/deluge/tests/test_torrent.py +++ b/deluge/tests/test_torrent.py @@ -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')