Skip to content

The check for file_contents didn't discriminate the argument missing and the file being empty - #55

Closed
fawkesley wants to merge 1 commit into
python-excel:masterfrom
fawkesley:master
Closed

fawkesley wants to merge 1 commit into
python-excel:masterfrom
fawkesley:master

Conversation

@fawkesley

Copy link
Copy Markdown

With a default argument ie file_contents = None

Checking if file_contents has been provided using:
if file_contents:

... returns False for an empty file. This leads to very tedious debugging if your file happens to be empty!

@sjmachin

sjmachin commented Jun 6, 2013

Copy link
Copy Markdown
Member

Please be more specific. Show the actual args you passed for both filename and file_contents, and show the outcomes you experience before and after your change.

@fawkesley

Copy link
Copy Markdown
Author

@sjmachin Apologies, completely missed this in github noise! Just encountered again :)

I use xlrd via messytables so it was awkard to diagnose what happened. It happens because I cocked up my file handle by reading it twice, leading to file_contents containing an empty string. This gets mistaken for "not provided" because the test doesn't check None vs "". Here's a demo:

with open('myfile.xls', 'rb') as f:
    f.read()
    # next read() will return empty str

    open_workbook(filename=None, file_contents=f.read())

Result:

Traceback (most recent call last):
  File "/home/paul/v/scraperwiki/local/lib/python2.7/site-packages/nose/suite.py", line 208, in run
    self.setUp()
  File "/home/paul/v/scraperwiki/local/lib/python2.7/site-packages/nose/suite.py", line 291, in setUp
    self.setupContext(ancestor)
  File "/home/paul/v/scraperwiki/local/lib/python2.7/site-packages/nose/suite.py", line 314, in setupContext
    try_run(context, names)
  File "/home/paul/v/scraperwiki/local/lib/python2.7/site-packages/nose/util.py", line 469, in try_run
    return func()
  File "/home/paul/repo/informa-brazil-conab/tool/tests.py", line 61, in setUpClass
    cls.rows = list(process_grains.process(f))
  File "/home/paul/repo/informa-brazil-conab/tool/process_grains.py", line 69, in process
    table = get_table(xls_fileobj, indicator)
  File "/home/paul/repo/informa-brazil-conab/tool/process_grains.py", line 85, in get_table
    table_name=sheet_name)
  File "/home/paul/repo/xypath/xypath/xypath.py", line 594, in from_file_object
    table_set = messytables.any.any_tableset(fobj, extension=extension)
  File "/home/paul/v/scraperwiki/src/messytables/messytables/any.py", line 131, in any_tableset
    return attempt(fileobj)
  File "/home/paul/v/scraperwiki/src/messytables/messytables/excel.py", line 44, in __init__
    encoding_override=encoding)
  File "/home/paul/v/scraperwiki/local/lib/python2.7/site-packages/xlrd/__init__.py", line 396, in open_workbook
    f = open(filename, "rb")
TypeError: coercing to Unicode: need string or buffer, NoneType found

This logic isn't strong enough IMHO because 1) it doesn't discriminate between file_contents being None (not given) or "" and 2) it doesn't check that filename is actually provided before trying to use it as one.

if file_contents:                                                           
    # use the file_contents                                        
else:
    # use the filename

I would favour this:

if file_contents is not None:
    if not len(file_contents):
        raise ValueError("file_contents is empty")
    # use file_contents
elif filename is not None:
    # use filename
else:
    raise RuntimeError("Require one of file_contents or filename")

Happy to regenerate the pull request if required?

@cjw296

cjw296 commented Jan 18, 2014

Copy link
Copy Markdown
Member

I think the current change in d8d3d01 is fine, just needs a unit test, howevr, if we want to be more informative:

"if not len(file_contents):" is horrible, that should just be "if not filecontents".

I like the 'else' clause :-)

But yes, a pull request would be great, please include tests in the unit test suite. A ShouldRaise would be the way to go...

@cjw296

cjw296 commented Apr 7, 2014

Copy link
Copy Markdown
Member

No followup.

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 this pull request may close these issues.

3 participants