Interesting! If you are motivated, a further approach would be to expose the compressor and decompressor objects from the C extension, and write the file object in Python (as in Lib/gzip.py).
> One thing I was unsure of is how to handle exceptions that occur in
> BZ2File_dealloc(). Does the error status need to be cleared before it
> returns?
Yes, it should. Actually, it would be better to write out the exception using PyErr_WriteUnraisable().
> On a related note, the 'buffering' argument to __init__() is ignored,
> and I was wondering whether this should be documented explicitly?
Yes, it should probably be deprecated if it's not useful anymore.
By the way, the current patch produces reference leaks:
$ ./python -m test -R 3:2 test_bz2
[1/1] test_bz2
beginning 5 repetitions
12345
.....
test_bz2 leaked [44, 44] references, sum=88 |