Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

book.py::_convert_content_to_stream incorrectly handling unsupported filetypes #96

Closed
NickRL21 opened this issue Oct 12, 2020 · 2 comments · Fixed by #97
Closed

book.py::_convert_content_to_stream incorrectly handling unsupported filetypes #96

NickRL21 opened this issue Oct 12, 2020 · 2 comments · Fixed by #97

Comments

@NickRL21
Copy link

NickRL21 commented Oct 12, 2020

After updating pyexcel-io I discovered this bug when intentionally passing an unsupported filetype in one of my unittests.
When passing the unsupported filetype into get_book the expected exception in the past would have been one of (FileTypeNotSupported, NoSupportingPluginFound, SupportingPluginAvailableButNotInstalled) however in book.py::_convert_content_to_stream (line 231) the call to manager.get_io (line 232) results in None causing the write method to be called on a NoneType (line 244) resulting in a generic attribute error.

def _convert_content_to_stream(file_content, file_type):
    stream = manager.get_io(file_type)
    target_content_type = manager.get_io_type(file_type)
    needs_encode = target_content_type == "bytes" and not isinstance(
        file_content, bytes
    )
    needs_decode = target_content_type == "string" and isinstance(
        file_content, bytes
    )
    if needs_encode:
        file_content = file_content.encode("utf-8")
    elif needs_decode:
        file_content = file_content.decode("utf-8")
    stream.write(file_content)
    stream.seek(0)
    return stream
.venv/lib/python3.6/site-packages/pyexcel/core.py:47: in get_book
    book_stream = sources.get_book_stream(**keywords)
.venv/lib/python3.6/site-packages/pyexcel/internal/core.py:39: in get_book_stream
    sheets = a_source.get_data()
.venv/lib/python3.6/site-packages/pyexcel/plugins/sources/memory_input.py:41: in get_data
    self.__file_content, **self._keywords
.venv/lib/python3.6/site-packages/pyexcel/plugins/parsers/excel.py:28: in parse_file_content
    file_content, file_type=self._file_type, **keywords
.venv/lib/python3.6/site-packages/pyexcel/plugins/parsers/excel.py:40: in _parse_any
    sheets = get_data(anything, file_type=file_type, **keywords)
.venv/lib/python3.6/site-packages/pyexcel_io/io.py:87: in get_data
    afile, file_type=file_type, streaming=False, **keywords
.venv/lib/python3.6/site-packages/pyexcel_io/io.py:105: in _get_data
    return load_data(**keywords)
.venv/lib/python3.6/site-packages/pyexcel_io/io.py:193: in load_data
    reader.open_content(file_content, **keywords)
.venv/lib/python3.6/site-packages/pyexcel_io/reader.py:70: in open_content
    file_content, self.file_type
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
file_content = 'foo-bar-data', file_type = 'ods'
    def _convert_content_to_stream(file_content, file_type):
        stream = manager.get_io(file_type)
        target_content_type = manager.get_io_type(file_type)
        needs_encode = target_content_type == "bytes" and not isinstance(
            file_content, bytes
        )
        needs_decode = target_content_type == "string" and isinstance(
            file_content, bytes
        )
        if needs_encode:
            file_content = file_content.encode("utf-8")
        elif needs_decode:
            file_content = file_content.decode("utf-8")
>       stream.write(file_content)
E       AttributeError: 'NoneType' object has no attribute 'write'
.venv/lib/python3.6/site-packages/pyexcel_io/book.py:244: AttributeError

pyexcel version info

pyexcel                                       0.6.5
pyexcel-io                                    0.6.2
pyexcel-xls                                   0.5.8
pyexcel-xlsx                                  0.5.7
@NickRL21 NickRL21 changed the title book.py incorrectly handling unsupported filetypes book.py::_convert_content_to_stream incorrectly handling unsupported filetypes Oct 12, 2020
chfw added a commit that referenced this issue Oct 12, 2020
@chfw chfw mentioned this issue Oct 12, 2020
7 tasks
@chfw chfw closed this as completed in #97 Oct 12, 2020
chfw added a commit that referenced this issue Oct 12, 2020
* 🐛 regression: unknown file type shall trigger NoSupportingPluginFound. fix #96

* This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst

* 💚 update coding style. #96

* 🔬 more test coverage

Co-authored-by: chfw <chfw@users.noreply.github.com>
@chfw
Copy link
Member

chfw commented Oct 12, 2020

thanks for your report. Please verify 0.6.3 and see if the behaviour has been corrected.

@NickRL21
Copy link
Author

Working as expected in 0.6.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants