Skip to content

attempting to load directory throws UnboundLocalError #70

Closed
@nog642

Description

@nog642

After installing pyexcel-xlsxr, running the following file gives an unhelpful error message:

from pyexcel_xlsxr import get_data
get_data('/')
UnboundLocalError: local variable 'reader' referenced before assignment

Looking at the source code for io.py, it's clear the error stems from here:

    try:
        reader = READERS.get_a_plugin(file_type, library)
    except NoSupportingPluginFound:
        if file_name:
            if not os.path.exists(file_name):
                raise IOError("%s does not exist" % file_name)
        else:
            raise

The except block is entered, but the first if is true while the second if is false (because the path does exist, it is just a directory rather than a file), so no new exception is raised. The code continues on until reader is referenced again, which fails because it was never defined.

Expected behavior:

That os.path.exists check should probably be os.path.isfile instead, and the error message should be "%s is not a file" rather than "%s does not exist". That if should also then have an else that raises an exception, so you are never stuck in a position where reader is undefined and you are left with an UnboundLocalError. Something like this:

    try:
        reader = READERS.get_a_plugin(file_type, library)
    except NoSupportingPluginFound:
        if not os.path.isfile(file_name):
            raise IOError("%s is not a file" % file_name)
        else:
            raise

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions