# Buggy code
def on_received_downloads(self, downloads):
    if not downloads:
        return  # This might happen when closing Tribler

    total_download = 0
    total_upload = 0
    self.received_downloads.emit(downloads)
    self.downloads = downloads

    download_infohashes = set()
    for download in downloads["downloads"]:
        if download["infohash"] in self.download_widgets:
            item = self.download_widgets[download["infohash"]]
        else:
            item = DownloadWidgetItem(self.window().downloads_list)
            self.download_widgets[download["infohash"]] = item

        item.update_with_download(download)

        # Update video player with download info
        video_infohash = self.window().video_player_page.active_infohash
        if video_infohash != "" and download["infohash"] == video_infohash:
            self.window().video_player_page.update_with_download_info(download)

        total_download += download["speed_down"]
        total_upload += download["speed_up"]

        download_infohashes.add(download["infohash"])

        if (
            self.window().download_details_widget.current_download is not None
            and self.window().download_details_widget.current_download["infohash"]
            == download["infohash"]
        ):
            self.window().download_details_widget.current_download = download
            self.window().download_details_widget.update_pages()

    # Check whether there are download that should be removed
    toremove = set()
    for infohash, item in self.download_widgets.iteritems():
        if infohash not in download_infohashes:
            index = self.window().downloads_list.indexOfTopLevelItem(item)
            toremove.add((infohash, index))

    for infohash, index in toremove:
        self.window().downloads_list.takeTopLevelItem(index)
        del self.download_widgets[infohash]

    if QSystemTrayIcon.isSystemTrayAvailable():
        self.window().tray_icon.setToolTip(
            "Down: %s, Up: %s"
            % (format_speed(total_download), format_speed(total_upload))
        )
    self.update_download_visibility()
    self.schedule_downloads_timer()

    # Update the top download management button if we have a row selected
    if len(self.window().downloads_list.selectedItems()) > 0:
        self.on_download_item_clicked()


# Traceback (AssertionError)
Tuples differ: (2, ('127.0.0.1', [5, 4, 3, 2,... != (2, ('127.0.0.1', [5, 4, 3, 2,...

First differing element 2:
u'None'
''

- (2, ('127.0.0.1', [5, 4, 3, 2, 1]), u'None')
?                                     - ----

+ (2, ('127.0.0.1', [5, 4, 3, 2, 1]), '')
Stacktrace

Traceback (most recent call last):
File "c:\python27\lib\unittest\case.py", line 329, in run
testMethod()
File "C:\workspace\jenkins\workspace\GH_Tribler_PR_tests_win64\tribler\Tribler\Test\test_as_server.py", line 58, in check
result = fun(*argv, **kwargs)
File "C:\workspace\jenkins\workspace\GH_Tribler_PR_tests_win64\tribler\Tribler\Test\Core\Upgrade\test_config_upgrade_70_71.py", line 33, in test_read_test_libtribler_conf
self.assertEqual(result_config.get_anon_proxy_settings(), (2, ("127.0.0.1", [5, 4, 3, 2, 1]), ''))
File "c:\python27\lib\unittest\case.py", line 513, in assertEqual
assertion_func(first, second, msg=msg)
File "c:\python27\lib\unittest\case.py", line 753, in assertTupleEqual
self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)
File "c:\python27\lib\unittest\case.py", line 724, in assertSequenceEqual
self.fail(msg)
File "c:\python27\lib\unittest\case.py", line 410, in fail
raise self.failureException(msg)
AssertionError: Tuples differ: (2, ('127.0.0.1', [5, 4, 3, 2,... != (2, ('127.0.0.1', [5, 4, 3, 2,...

First differing element 2:
u'None'
''

- (2, ('127.0.0.1', [5, 4, 3, 2, 1]), u'None')
?                                     - ----

+ (2, ('127.0.0.1', [5, 4, 3, 2, 1]), '')

# Fixed code
def on_received_downloads(self, downloads):
    if not downloads:
        return  # This might happen when closing Tribler

    total_download = 0
    total_upload = 0
    self.received_downloads.emit(downloads)
    self.downloads = downloads

    download_infohashes = set()
    for download in downloads["downloads"]:
        if download["infohash"] in self.download_widgets:
            item = self.download_widgets[download["infohash"]]
        else:
            item = DownloadWidgetItem(self.window().downloads_list)
            self.download_widgets[download["infohash"]] = item

        item.update_with_download(download)

        # Update video player with download info
        video_infohash = self.window().video_player_page.active_infohash
        if video_infohash != "" and download["infohash"] == video_infohash:
            self.window().video_player_page.update_with_download_info(download)

        total_download += download["speed_down"]
        total_upload += download["speed_up"]

        download_infohashes.add(download["infohash"])

        if (
            self.window().download_details_widget.current_download is not None
            and self.window().download_details_widget.current_download["infohash"]
            == download["infohash"]
        ):
            self.window().download_details_widget.current_download = download
            self.window().download_details_widget.update_pages()

    # Check whether there are download that should be removed
    toremove = set()
    for infohash, item in self.download_widgets.iteritems():
        if infohash not in download_infohashes:
            index = self.window().downloads_list.indexOfTopLevelItem(item)
            toremove.add((infohash, index))

    for infohash, index in toremove:
        self.window().downloads_list.takeTopLevelItem(index)
        del self.download_widgets[infohash]

    if self.window().tray_icon:
        self.window().tray_icon.setToolTip(
            "Down: %s, Up: %s"
            % (format_speed(total_download), format_speed(total_upload))
        )
    self.update_download_visibility()
    self.schedule_downloads_timer()

    # Update the top download management button if we have a row selected
    if len(self.window().downloads_list.selectedItems()) > 0:
        self.on_download_item_clicked()