pyexcel / pyexcel-io

One interface to read and write the data in various excel formats, import the data into and export the data from databases
http://io.pyexcel.org
Other
58 stars 20 forks source link

attempting to load directory throws UnboundLocalError #70

Closed nog642 closed 5 years ago

nog642 commented 5 years ago

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
chfw commented 5 years ago

yep, you have made your point there. I will schedule a change for it.

nog642 commented 5 years ago

Yup, seems solved.