Issue #637 closed
arthurlapraye
created an issue

I recently had a bug using openpyxl, and when I tracked down the problem, I found this piece of code (lines 178-187 in file writer/write_only.py )

            except ValueError:
                if isinstance(value, Cell):
                    cell = value
                    if cell.comment is not None:
                        comment = cell.comment
                        comment._parent = CommentParentCell(cell)
                        self._comments.append(comment)
                else:
                    raise ValueError

An exception is caught on line 178, a fix is tried in the next line and then if it fails, a new exception is raised. The problem with this is that an exception typically contains valuable debugging information. In this case the original ValueError caught on line 178 will contain information about what value caused it. The new ValueError raised on line 187 is devoid of information and therefore masks what originally caused the exception.

I hereby propose a fix :

except ValueError as E:
                if isinstance(value, Cell):
                    cell = value
                    if cell.comment is not None:
                        comment = cell.comment
                        comment._parent = CommentParentCell(cell)
                        self._comments.append(comment)
                else:
                    raise E

Comments (8)

  1. arthurlapraye reporter

    Sorry, I wasn't quite sure where I should post this.

    Anyway, here's the way I found it :

    import openpyxl as opx
    from PyQt4.QtCore import QString
    
    wb=opx.Workbook(write_only=True,guess_types=True)
    ws=wb.create_sheet()
    ws.append([QString("whatever")]) #This raises a bare ValueError
    

    Trying this :

    wb=opx.Workbook(guess_types=True)
    ws=wb.create_sheet()
    ws.append([QString("whatever")])
    

    or this :

    opx.writer.write_only.WriteOnlyCell(ws,QString("whatever"))
    

    yields a more informative traceback.

  2. Log in to comment