claird / PyPDF4

A utility to read and write PDFs with Python
obsolete-https://pythonhosted.org/PyPDF2/
Other
328 stars 61 forks source link

Assertions vs. raising exceptions #6

Open acsor opened 5 years ago

acsor commented 5 years ago

While deploying unit tests for filters.py I have noticed that the use of assert is much more frequent than a reasonable exception. AFAIK, even in Python the assert keyword has a wide use in debugging, but not when launching production code.

Take for example filters.py:390 where I would substitute

assert b < (2 ** 32 - 1)

with something like

if b >= (2 ** 32) - 1:
    raise OverflowError("See ISO 32000, p. ...")

Shall I, and all the other developers, take the freedom of changing these occurrences when stumbled upon and when it is reasonable to do so?

sim0nx commented 5 years ago

I fully agree with you. Haven't used or seen such usage in Python outside debugging. Reading through https://mail.python.org/pipermail/python-list/2013-November/660568.html , especially the point that assert can be compiled away (through optimisations) probably is another good argument not to use asserts in this case, unless you don't care about them triggering.

A reasonable and descriptive exception seems way more appropriate to me.