Skip to content

Commit

Permalink
make py3 fix on bytes written to stringio
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed May 19, 2015
1 parent f43a218 commit 6611614
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pyexcel_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
from .csvzipbook import CSVZipWriter, CSVZipBook
from .sqlbook import SQLBookReader, SQLBookWriter
from .djangobook import DjangoBookReader, DjangoBookWriter
from ._compact import is_string, BytesIO, StringIO, isstream, OrderedDict
from ._compact import is_string, BytesIO, StringIO, isstream, OrderedDict, PY2
from .constants import (
MESSAGE_LOADING_FORMATTER,
MESSAGE_ERROR_02,
MESSAGE_ERROR_03,
MESSAGE_CANNOT_WRITE_STREAM_FORMATTER,
MESSAGE_CANNOT_READ_STREAM_FORMATTER,
Expand Down Expand Up @@ -116,6 +117,8 @@ def load_data(filename,
book = None
from_memory = False
content = None
if filename is None:
raise IOError(MESSAGE_ERROR_02)
if filename in READERS:
book_class = READERS[filename]
book = book_class(**keywords)
Expand All @@ -137,7 +140,14 @@ def load_data(filename,
raise IOError(MESSAGE_ERROR_03)
else:
io = get_io(file_type)
io.write(filename)
if not PY2:
if isinstance(io, StringIO) and isinstance(filename, bytes):
content = filename.decode('utf-8')
else:
content = filename
io.write(content)
else:
io.write(filename)
io.seek(0)
content = io
book = book_class(None, file_content=content,
Expand Down Expand Up @@ -168,6 +178,8 @@ def get_writer(filename, file_type=None, **keywords):
extension = None
writer = None
to_memory = False
if filename is None:
raise IOError(MESSAGE_ERROR_02)
if filename in WRITERS:
writer_class = WRITERS[filename]
writer = writer_class(filename, **keywords)
Expand Down
1 change: 1 addition & 0 deletions pyexcel_io/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DEFAULT_SEPARATOR = '__'

MESSAGE_INVALID_PARAMETERS = "Invalid parameters"
MESSAGE_ERROR_02 = "No content, file name. Nothing is given"
MESSAGE_ERROR_03 = "cannot handle unknown content"
MESSAGE_CANNOT_WRITE_STREAM_FORMATTER = "Cannot write content of file type %s to stream"
MESSAGE_CANNOT_READ_STREAM_FORMATTER = "Cannot read content of file type %s from stream"
Expand Down

0 comments on commit 6611614

Please sign in to comment.