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.
fix/89-fix-encrypting-streams
Garrett Robinson 2015-01-17 16:09:39 -08:00
parent b1dab1570d
commit f8ccdc5028
1 changed files with 4 additions and 1 deletions

View File

@ -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