Refactor _make_binary_stream() error handling and variable names.
parent
da59707945
commit
4f1b1f6a8d
|
@ -531,25 +531,26 @@ def _is_gpg2(version):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _make_binary_stream(s, encoding=None):
|
def _make_binary_stream(thing, encoding=None, armor=True):
|
||||||
"""Encode **s**, then make it stream/file-like.
|
"""Encode **thing**, then make it stream/file-like.
|
||||||
|
|
||||||
:param s: The thing to turn into a encoded stream.
|
:param thing: The thing to turn into a encoded stream.
|
||||||
:rtype: ``io.BytesIO`` or ``io.StringIO``.
|
:rtype: ``io.BytesIO`` or ``io.StringIO``.
|
||||||
:returns: The encoded **thing**, wrapped in an ``io.BytesIO`` (if
|
:returns: The encoded **thing**, wrapped in an ``io.BytesIO`` (if
|
||||||
available), otherwise wrapped in a ``io.StringIO``.
|
available), otherwise wrapped in a ``io.StringIO``.
|
||||||
"""
|
"""
|
||||||
|
if _py3k:
|
||||||
|
if isinstance(thing, str):
|
||||||
|
thing = thing.encode(encoding)
|
||||||
|
else:
|
||||||
|
if type(thing) is not str:
|
||||||
|
thing = thing.encode(encoding)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if _py3k:
|
rv = BytesIO(thing)
|
||||||
if isinstance(s, str):
|
except NameError:
|
||||||
s = s.encode(encoding)
|
rv = StringIO(thing)
|
||||||
else:
|
|
||||||
if type(s) is not str:
|
|
||||||
s = s.encode(encoding)
|
|
||||||
from io import BytesIO
|
|
||||||
rv = BytesIO(s)
|
|
||||||
except ImportError:
|
|
||||||
rv = StringIO(s)
|
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
def _make_passphrase(length=None, save=False, file=None):
|
def _make_passphrase(length=None, save=False, file=None):
|
||||||
|
|
Loading…
Reference in New Issue