From 8c261eba30b6b82e1db8674f8f8e2381a457dc84 Mon Sep 17 00:00:00 2001 From: Garrett Robinson Date: Tue, 20 Jan 2015 09:22:54 -0800 Subject: [PATCH] Expand set of classes recognized by `_util._is_stream` Adds additional commonly used stream classes from the standard library to `_util._is_stream`. This means these classes can be used successfully wherever `_is_stream` is used to decide whether or not to encode data throughout the codebase, including in `_encrypt`. --- gnupg/_util.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/gnupg/_util.py b/gnupg/_util.py index fff8c0c..c193494 100644 --- a/gnupg/_util.py +++ b/gnupg/_util.py @@ -41,6 +41,17 @@ try: except ImportError: from cStringIO import StringIO +# Import the StringIO class from the StringIO module since it is a +# commonly used stream class. It is distinct from either of the +# StringIO's that may be loaded in the above try/except clause, so the +# name is prefixed with an underscore to distinguish it. +from StringIO import StringIO as _StringIO + +# Import the cStringIO module to test for the cStringIO stream types, +# InputType and OutputType. See +# http://stackoverflow.com/questions/14735295/to-check-an-instance-is-stringio +import cStringIO + from . import _logger @@ -350,7 +361,8 @@ def _is_stream(input): :rtype: bool :returns: True if :param:input is a stream, False if otherwise. """ - return isinstance(input, BytesIO) or isinstance(input, StringIO) + return isinstance(input, (BytesIO, StringIO, _StringIO, + cStringIO.InputType, cStringIO.OutputType)) def _is_list_or_tuple(instance): """Check that ``instance`` is a list or tuple.