From f8ccdc5028f2d8d74233abfdb01cb695b59463bb Mon Sep 17 00:00:00 2001 From: Garrett Robinson Date: Sat, 17 Jan 2015 16:09:39 -0800 Subject: [PATCH] Fix `GPG.encrypt` for file-like objects `GPG.encrypt_file` was refactored into `GPG.encrypt` in 295d98f, which broke the functionality of `GPG.encrypt_file` for encrypting file-like stream objects such as StringIO, BytesIO, etc. The main difference between `GPG.encrypt_file` and `GPG.encrypt` is that `GPG.encrypt` first converts its `data` argument into a binary stream via `_make_binary_stream`. This is unnecessary when the argument is already a stream, as was often the case in calls to `GPG.encrypt_file`. Additionally, `_make_binary_stream` typically fails when it attempts to encode a stream object, which means it is no longer possible to achieve the functionality of `GPG.encrypt_file` with `GPG.encrypt` after the refactor. This commit only converts `data` to a binary stream if it is not already a stream, re-using `_util._is_stream` to make that determination. --- gnupg/gnupg.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gnupg/gnupg.py b/gnupg/gnupg.py index 7168017..757f3e2 100644 --- a/gnupg/gnupg.py +++ b/gnupg/gnupg.py @@ -952,7 +952,10 @@ generate keys. Please see .. seealso:: :meth:`._encrypt` """ - stream = _make_binary_stream(data, self._encoding) + if _is_stream(data): + stream = data + else: + stream = _make_binary_stream(data, self._encoding) result = self._encrypt(stream, recipients, **kwargs) stream.close() return result