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

do not catch ValueError exceptions inside method by mistake #108

Merged
merged 1 commit into from Feb 27, 2014

Conversation

2 participants
@baxeico
Contributor

baxeico commented Feb 26, 2014

I had an encoding issue with data passed to worksheet.write, that made the python codecs module to raise a UnicodeDecodeError exception.

UnicodeDecodeError is a ValueError descendant (http://docs.python.org/2/library/exceptions.html#exception-hierarchy), so the code took an unwanted path.

My approach here is to leave only the possibly failing int(args[0]) line in the try block, moving the method call outside of the try.

@jmcnamara

This comment has been minimized.

Show comment
Hide comment
@jmcnamara

jmcnamara Feb 26, 2014

Owner

Hi,

Thanks for that. It seems like a reasonable change.

Can you show a small example with the before and after output.

John

Owner

jmcnamara commented Feb 26, 2014

Hi,

Thanks for that. It seems like a reasonable change.

Can you show a small example with the before and after output.

John

@baxeico

This comment has been minimized.

Show comment
Hide comment
@baxeico

baxeico Feb 26, 2014

Contributor

This simple example:

from xlsxwriter.workbook import Workbook

workbook = Workbook('test.xlsx', {'constant_memory': True})
worksheet = workbook.add_worksheet('Test')
worksheet.write(0, 0, u'\xc4pple'.encode('iso-8859-1')) # wrong string encoding!
worksheet.write(1, 0, 'test')
workbook.close()

In the released version produces this:

Traceback (most recent call last):
  File "./test_xlsxwriter.py", line 8, in <module>
    worksheet.write(1, 0, 'test')
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 57, in cell_wrapper
    return method(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 413, in write
    return self.write_string(row, col, *args)
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 60, in cell_wrapper
    new_args = list(xl_cell_to_rowcol(args[0]))
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/utility.py", line 107, in xl_cell_to_rowcol
    match = range_parts.match(cell_str)
TypeError: expected string or buffer

In my patched version:

Traceback (most recent call last):
  File "./test_xlsxwriter.py", line 8, in <module>
    worksheet.write(1, 0, 'test')
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 63, in cell_wrapper
    return method(self, *args, **kwargs)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 415, in write
    return self.write_string(row, col, *args)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 63, in cell_wrapper
    return method(self, *args, **kwargs)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 468, in write_string
    self._write_single_row(row)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 4653, in _write_single_row
    self._write_cell(row_num, col_num, col_ref)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 4810, in _write_cell
    self._xml_inline_string(string, preserve, attributes)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/xmlwriter.py", line 167, in _xml_inline_string
    (attr, t_attr, string))
  File "/usr/lib/python2.7/codecs.py", line 691, in write
    return self.writer.write(data)
  File "/usr/lib/python2.7/codecs.py", line 351, in write
    data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 31: ordinal not in range(128)

That points to the real encoding issue.

Contributor

baxeico commented Feb 26, 2014

This simple example:

from xlsxwriter.workbook import Workbook

workbook = Workbook('test.xlsx', {'constant_memory': True})
worksheet = workbook.add_worksheet('Test')
worksheet.write(0, 0, u'\xc4pple'.encode('iso-8859-1')) # wrong string encoding!
worksheet.write(1, 0, 'test')
workbook.close()

In the released version produces this:

Traceback (most recent call last):
  File "./test_xlsxwriter.py", line 8, in <module>
    worksheet.write(1, 0, 'test')
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 57, in cell_wrapper
    return method(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 413, in write
    return self.write_string(row, col, *args)
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 60, in cell_wrapper
    new_args = list(xl_cell_to_rowcol(args[0]))
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/utility.py", line 107, in xl_cell_to_rowcol
    match = range_parts.match(cell_str)
TypeError: expected string or buffer

In my patched version:

Traceback (most recent call last):
  File "./test_xlsxwriter.py", line 8, in <module>
    worksheet.write(1, 0, 'test')
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 63, in cell_wrapper
    return method(self, *args, **kwargs)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 415, in write
    return self.write_string(row, col, *args)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 63, in cell_wrapper
    return method(self, *args, **kwargs)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 468, in write_string
    self._write_single_row(row)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 4653, in _write_single_row
    self._write_cell(row_num, col_num, col_ref)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/worksheet.py", line 4810, in _write_cell
    self._xml_inline_string(string, preserve, attributes)
  File "/home/augusto/develop/git/XlsxWriter/xlsxwriter/xmlwriter.py", line 167, in _xml_inline_string
    (attr, t_attr, string))
  File "/usr/lib/python2.7/codecs.py", line 691, in write
    return self.writer.write(data)
  File "/usr/lib/python2.7/codecs.py", line 351, in write
    data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 31: ordinal not in range(128)

That points to the real encoding issue.

@jmcnamara

This comment has been minimized.

Show comment
Hide comment
@jmcnamara

jmcnamara Feb 27, 2014

Owner

Thanks for that. It looks good.

Could you fix the convert_column_args() decorator in the same way and squash the changes into one commit (git commit --amend; git push origin --force should do it).

John.

Owner

jmcnamara commented Feb 27, 2014

Thanks for that. It looks good.

Could you fix the convert_column_args() decorator in the same way and squash the changes into one commit (git commit --amend; git push origin --force should do it).

John.

@baxeico

This comment has been minimized.

Show comment
Hide comment
@baxeico

baxeico Feb 27, 2014

Contributor

Ok, I've done it!

Contributor

baxeico commented Feb 27, 2014

Ok, I've done it!

jmcnamara added a commit that referenced this pull request Feb 27, 2014

Merge pull request #108 from baxeico/master
do not catch ValueError exceptions inside method by mistake

@jmcnamara jmcnamara merged commit 83d6b46 into jmcnamara:master Feb 27, 2014

1 check passed

default The Travis CI build passed
Details
@jmcnamara

This comment has been minimized.

Show comment
Hide comment
@jmcnamara

jmcnamara Feb 27, 2014

Owner

Great. Thanks for that. That is a good fix.

Merged.

John.

Owner

jmcnamara commented Feb 27, 2014

Great. Thanks for that. That is a good fix.

Merged.

John.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment