From 40bd145637d2361eb324f7e8b85a605dec69b798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sat, 6 Apr 2024 03:43:12 +0200 Subject: [PATCH] remove 'contextlib' imports --- gallery_dl/cookies.py | 6 ++++-- test/test_job.py | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/gallery_dl/cookies.py b/gallery_dl/cookies.py index 092a9415..71a45f00 100644 --- a/gallery_dl/cookies.py +++ b/gallery_dl/cookies.py @@ -10,7 +10,6 @@ # https://github.com/yt-dlp/yt-dlp/blob/master/yt_dlp/cookies.py import binascii -import contextlib import ctypes import logging import os @@ -682,7 +681,8 @@ def _get_gnome_keyring_password(browser_keyring_name): # lists all keys and presumably searches for its key in the list. # It appears that we must do the same. # https://github.com/jaraco/keyring/issues/556 - with contextlib.closing(secretstorage.dbus_init()) as con: + con = secretstorage.dbus_init() + try: col = secretstorage.get_default_collection(con) label = browser_keyring_name + " Safe Storage" for item in col.get_all_items(): @@ -691,6 +691,8 @@ def _get_gnome_keyring_password(browser_keyring_name): else: _log_error("Failed to read from GNOME keyring") return b"" + finally: + con.close() def _get_linux_keyring_password(browser_keyring_name, keyring): diff --git a/test/test_job.py b/test/test_job.py index 141b1b2e..3e6f85be 100644 --- a/test/test_job.py +++ b/test/test_job.py @@ -13,7 +13,6 @@ import unittest from unittest.mock import patch import io -import contextlib sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from gallery_dl import job, config, text # noqa E402 @@ -32,8 +31,13 @@ class TestJob(unittest.TestCase): jobinstance = extr_or_job with io.StringIO() as buffer: - with contextlib.redirect_stdout(buffer): + stdout = sys.stdout + sys.stdout = buffer + try: jobinstance.run() + finally: + sys.stdout = stdout + return buffer.getvalue()