From 0039b9d47ccfb1910ddfbdde78639cd76a5ec846 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Thu, 11 Apr 2024 10:12:20 +0100 Subject: [PATCH] Fix #237: ensure local variable is initialized even when an exception occurs. --- README.rst | 6 +++++- gnupg.py | 8 +++++--- test_gnupg.py | 15 ++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index 1411a65..50a5bc7 100644 --- a/README.rst +++ b/README.rst @@ -77,7 +77,11 @@ Change log Released: Not yet -* Fix #117: Add WKD support for auto-locating keys. Thanks to Myzel394 for the patch. +* Fix #117: Add WKD (Web Key Directory) support for auto-locating keys. Thanks to Myzel394 + for the patch. + +* Fix #237: Ensure local variable is initialized even when an exception occurs. + 0.5.2 ----- diff --git a/gnupg.py b/gnupg.py index 637838e..7bf3fb8 100644 --- a/gnupg.py +++ b/gnupg.py @@ -27,14 +27,14 @@ and so does not work on Windows). Renamed to gnupg.py to avoid confusion with the previous versions. -Modifications Copyright (C) 2008-2023 Vinay Sajip. All rights reserved. +Modifications Copyright (C) 2008-2024 Vinay Sajip. All rights reserved. For the full documentation, see https://docs.red-dove.com/python-gnupg/ or https://gnupg.readthedocs.io/ """ import codecs -from datetime import date, datetime +from datetime import datetime from email.utils import parseaddr from io import StringIO import logging @@ -1336,13 +1336,15 @@ def _handle_io(self, args, fileobj_or_path, result, passphrase=None, binary=Fals stdin = codecs.getwriter(self.encoding)(p.stdin) else: stdin = p.stdin + writer = None # See issue #237 if passphrase: _write_passphrase(stdin, passphrase, self.encoding) writer = _threaded_copy_data(fileobj, stdin, self.buffer_size) self._collect_output(p, result, writer, stdin) return result finally: - writer.join(0.01) + if writer: + writer.join(0.01) if fileobj is not fileobj_or_path: fileobj.close() diff --git a/test_gnupg.py b/test_gnupg.py index da24a6d..d1614e3 100644 --- a/test_gnupg.py +++ b/test_gnupg.py @@ -2,7 +2,7 @@ """ A test harness for gnupg.py. -Copyright (C) 2008-2023 Vinay Sajip. All rights reserved. +Copyright (C) 2008-2024 Vinay Sajip. All rights reserved. """ import argparse import json @@ -1554,16 +1554,17 @@ def test_multiple_signatures_one_invalid(self): @skipIf('CI' not in os.environ, "Don't test locally") def test_auto_key_locating(self): - gpg = self.gpg - # Let's hope ProtonMail doesn't change their key anytime soon - expected_fingerprint = "90E619A84E85330A692F6D81A655882018DBFA9D" - expected_type = "rsa2048" + expected_fingerprint = '90E619A84E85330A692F6D81A655882018DBFA9D' + # expected_type = 'rsa2048' - actual = self.gpg.auto_locate_key("no-reply@protonmail.com") + actual = self.gpg.auto_locate_key('no-reply@protonmail.com') self.assertEqual(actual.fingerprint, expected_fingerprint) + def test_passphrase_encoding(self): + self.assertRaises(UnicodeEncodeError, self.gpg.decrypt, 'foo', passphrase=u'I’ll') + TEST_GROUPS = { 'sign': @@ -1586,7 +1587,7 @@ def test_auto_key_locating(self): 'basic': set(['test_environment', 'test_list_keys_initial', 'test_nogpg', 'test_make_args', 'test_quote_with_shell']), 'test': - set(['test_auto_key_locating']), + set(['test_passphrase_encoding']), }