Skip to content

Commit

Permalink
Fix #237: ensure local variable is initialized even when an exception…
Browse files Browse the repository at this point in the history
… occurs.
  • Loading branch information
vsajip committed Apr 11, 2024
1 parent 3744512 commit 0039b9d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
8 changes: 5 additions & 3 deletions gnupg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
15 changes: 8 additions & 7 deletions test_gnupg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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':
Expand All @@ -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']),
}


Expand Down

0 comments on commit 0039b9d

Please sign in to comment.