This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: bz2 would use a dedicated exception for data error
Type: enhancement Stage:
Components: Extension Modules Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: mgorny, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2020-09-05 10:26 by mgorny, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg376427 - (view) Author: Michał Górny (mgorny) * Date: 2020-09-05 10:26
Currently, the bz2 extension functions raise a generic OSError() on BZ_DATA_ERROR and BZ_DATA_ERROR_MAGIC.  When working on files, this makes it unnecessarily hard to catch compressor errors (i.e. invalid bz2 input file) without catching system I/O errors.  Could you please consider adding a new exception type for bz2 errors, akin to gzip.BadGzipFile?
msg376744 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-09-11 22:20
An OSError for a file the OS can read seems a bit off.  What is the accompanying message?  Would something like ValueError("bz2 compressor failed with BZ_DATA_ERROR") be better? 

Michał, are there precedents or models for this in the other file compression modules?
msg376769 - (view) Author: Michał Górny (mgorny) * Date: 2020-09-12 03:19
> An OSError for a file the OS can read seems a bit off.  What is the accompanying message?

For example:

  OSError: Invalid data stream

(I've just put random string into a file, and opened it as bzip2)

> Would something like ValueError("bz2 compressor failed with BZ_DATA_ERROR") be better?

It would be a partial improvement.  However, it would also break backwards compatibility with code already catching OSError.  It might be better to go with custom exception type derived from OSError to keep existing code working.

> Michał, are there precedents or models for this in the other file compression modules?

Python 3.8 has introduced BadGzipFile in gzip module, to replace their OSError.

lzma has used LZMAError from day one.
History
Date User Action Args
2022-04-11 14:59:35adminsetgithub: 85891
2020-09-12 03:19:17mgornysetmessages: + msg376769
2020-09-11 22:20:13terry.reedysetnosy: + terry.reedy, serhiy.storchaka

messages: + msg376744
versions: + Python 3.10
2020-09-05 10:26:14mgornycreate