Skip to content

Commit

Permalink
fix #19, use cStringIO by default but allow StringIO if the developer…
Browse files Browse the repository at this point in the history
… isn't aware of cStringIO
  • Loading branch information
chfw committed Jun 2, 2016
1 parent 6411955 commit ce7a62f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 7 additions & 3 deletions pyexcel_io/_compact.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ def is_generator(struct):


if PY2:
from StringIO import StringIO
from StringIO import StringIO as BytesIO
from cStringIO import StringIO
from cStringIO import StringIO as BytesIO
from StringIO import StringIO as SlowStringIO
text_type = unicode

class Iterator(object):
def next(self):
return type(self).__next__(self)

def isstream(instance):
return isinstance(instance, StringIO)
import cStringIO
return (isinstance(instance, cStringIO.InputType) or
isinstance(instance, cStringIO.OutputType) or
isinstance(instance, SlowStringIO))

else:
from io import StringIO, BytesIO
Expand Down
5 changes: 4 additions & 1 deletion pyexcel_io/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
:copyright: (c) 2014-2016 by Onni Software Ltd.
:license: New BSD License, see LICENSE for more details
"""
from ._compact import StringIO, BytesIO
from ._compact import StringIO, BytesIO, PY2
from .utils import resolve_missing_readers, resolve_missing_writers


Expand Down Expand Up @@ -114,6 +114,9 @@ def get_io(file_type):

@staticmethod
def validate_io(file_type, stream):
if PY2:
# only python 3 cares about stringio types
return True
if file_type in RWManager.text_stream_types:
return isinstance(stream, StringIO)
elif file_type in RWManager.binary_stream_types:
Expand Down
8 changes: 6 additions & 2 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@ def test_is_string():
assert is_string(type('a')) == True

def test_validate_io():
assert RWManager.validate_io("csd", StringIO()) == False

if PY2:
# because this function in python 2 is hardcode
# why? because validation is not needed in python 3
assert RWManager.validate_io("csd", StringIO()) == True
else:
assert RWManager.validate_io("csd", StringIO()) == False

@raises(TypeError)
def test_generator_is_obtained():
Expand Down

0 comments on commit ce7a62f

Please sign in to comment.