Skip to content

Commit 0039b9d

Browse files
committed
Fix #237: ensure local variable is initialized even when an exception occurs.
1 parent 3744512 commit 0039b9d

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ Change log
7777

7878
Released: Not yet
7979

80-
* Fix #117: Add WKD support for auto-locating keys. Thanks to Myzel394 for the patch.
80+
* Fix #117: Add WKD (Web Key Directory) support for auto-locating keys. Thanks to Myzel394
81+
for the patch.
82+
83+
* Fix #237: Ensure local variable is initialized even when an exception occurs.
84+
8185

8286
0.5.2
8387
-----

gnupg.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
and so does not work on Windows). Renamed to gnupg.py to avoid confusion with
2828
the previous versions.
2929
30-
Modifications Copyright (C) 2008-2023 Vinay Sajip. All rights reserved.
30+
Modifications Copyright (C) 2008-2024 Vinay Sajip. All rights reserved.
3131
3232
For the full documentation, see https://docs.red-dove.com/python-gnupg/ or
3333
https://gnupg.readthedocs.io/
3434
"""
3535

3636
import codecs
37-
from datetime import date, datetime
37+
from datetime import datetime
3838
from email.utils import parseaddr
3939
from io import StringIO
4040
import logging
@@ -1336,13 +1336,15 @@ def _handle_io(self, args, fileobj_or_path, result, passphrase=None, binary=Fals
13361336
stdin = codecs.getwriter(self.encoding)(p.stdin)
13371337
else:
13381338
stdin = p.stdin
1339+
writer = None # See issue #237
13391340
if passphrase:
13401341
_write_passphrase(stdin, passphrase, self.encoding)
13411342
writer = _threaded_copy_data(fileobj, stdin, self.buffer_size)
13421343
self._collect_output(p, result, writer, stdin)
13431344
return result
13441345
finally:
1345-
writer.join(0.01)
1346+
if writer:
1347+
writer.join(0.01)
13461348
if fileobj is not fileobj_or_path:
13471349
fileobj.close()
13481350

test_gnupg.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
A test harness for gnupg.py.
44
5-
Copyright (C) 2008-2023 Vinay Sajip. All rights reserved.
5+
Copyright (C) 2008-2024 Vinay Sajip. All rights reserved.
66
"""
77
import argparse
88
import json
@@ -1554,16 +1554,17 @@ def test_multiple_signatures_one_invalid(self):
15541554

15551555
@skipIf('CI' not in os.environ, "Don't test locally")
15561556
def test_auto_key_locating(self):
1557-
gpg = self.gpg
1558-
15591557
# Let's hope ProtonMail doesn't change their key anytime soon
1560-
expected_fingerprint = "90E619A84E85330A692F6D81A655882018DBFA9D"
1561-
expected_type = "rsa2048"
1558+
expected_fingerprint = '90E619A84E85330A692F6D81A655882018DBFA9D'
1559+
# expected_type = 'rsa2048'
15621560

1563-
actual = self.gpg.auto_locate_key("[email protected]")
1561+
actual = self.gpg.auto_locate_key('[email protected]')
15641562

15651563
self.assertEqual(actual.fingerprint, expected_fingerprint)
15661564

1565+
def test_passphrase_encoding(self):
1566+
self.assertRaises(UnicodeEncodeError, self.gpg.decrypt, 'foo', passphrase=u'I’ll')
1567+
15671568

15681569
TEST_GROUPS = {
15691570
'sign':
@@ -1586,7 +1587,7 @@ def test_auto_key_locating(self):
15861587
'basic':
15871588
set(['test_environment', 'test_list_keys_initial', 'test_nogpg', 'test_make_args', 'test_quote_with_shell']),
15881589
'test':
1589-
set(['test_auto_key_locating']),
1590+
set(['test_passphrase_encoding']),
15901591
}
15911592

15921593

0 commit comments

Comments
 (0)