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

attempting to load directory throws UnboundLocalError #70

Closed
nog642 opened this issue Jul 17, 2019 · 2 comments
Closed

attempting to load directory throws UnboundLocalError #70

nog642 opened this issue Jul 17, 2019 · 2 comments

Comments

@nog642
Copy link

nog642 commented Jul 17, 2019

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
Copy link
Member

chfw commented Jul 17, 2019

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

chfw added a commit that referenced this issue Jul 17, 2019
@nog642
Copy link
Author

nog642 commented Aug 20, 2019

Yup, seems solved.

@nog642 nog642 closed this as completed Aug 20, 2019
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

No branches or pull requests

2 participants