Repository navigation
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
Description
| BPO | 15955 |
|---|---|
| Nosy | @pitrou, @vstinner, @tiran, @merwok, @vadmium, @serhiy-storchaka |
| Files |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = <Date 2015-02-26.12:12:10.383>
created_at = <Date 2012-09-17.12:22:42.212>
labels = ['type-feature', 'library']
title = 'bz2, lzma: add option to limit output size'
updated_at = <Date 2015-02-26.12:12:10.381>
user = 'https://github.com/tiran'bugs.python.org fields:
activity = <Date 2015-02-26.12:12:10.381>
actor = 'pitrou'
assignee = 'none'
closed = True
closed_date = <Date 2015-02-26.12:12:10.383>
closer = 'pitrou'
components = ['Library (Lib)']
creation = <Date 2012-09-17.12:22:42.212>
creator = 'christian.heimes'
dependencies = []
files = ['34609', '34789', '34792', '37644', '37658', '37677', '37734', '38194', '38206', '38211', '38217', '38218', '38226']
hgrepos = []
issue_num = 15955
keywords = ['patch']
message_count = 56.0
messages = ['170598', '171059', '171248', '171257', '171304', '174860', '174912', '175028', '176811', '176883', '176896', '177213', '177228', '180271', '187534', '208522', '208763', '209151', '209228', '209999', '214773', '214774', '215211', '215657', '215661', '215959', '220580', '220675', '225219', '226685', '232953', '233661', '233799', '233865', '233948', '234115', '234160', '234172', '234173', '235084', '235123', '235124', '236248', '236351', '236356', '236416', '236450', '236463', '236468', '236473', '236474', '236514', '236647', '236664', '236665', '236666']
nosy_count = 10.0
nosy_names = ['pitrou', 'vstinner', 'christian.heimes', 'nadeem.vawda', 'eric.araujo', 'Arfrever', 'nikratio', 'python-dev', 'martin.panter', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue15955'
versions = ['Python 3.5']Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
Activity
tiran commentedon Sep 17, 2012
The gzip, bz2 and lzma file reader have no method to get the actual size and compression ratio of a compressed file. In my opinion it's useful to know how much disk space a file will need before it's decompressed.
nadeemvawda commentedon Sep 23, 2012
As far as I can tell, there is no way to find this out reliably without decompressing the entire file. With gzip, the file trailer contains the uncompressed size modulo 2^32, but this seems less than useful. It appears that the other two formats do not store the total uncompressed data size in any form.
For bz2 and lzma, one can get the uncompressed size by doing f.seek(0, 2) followed by f.tell(). However this approach is ugly and potentially very slow, so I would be reluctant to add a method based on it to the (BZ2|LZMA)File classes.
tiran commentedon Sep 25, 2012
I've checked the implementation of the gzip command. It uses some tricks to get the result size of a compressed file. The bzip2 command doesn't have the ability. lzmainfo can print the actual size of the file but it says "Unknown" for my test file.
Also see bpo-16043 why this would be a useful feature.
pitrou commentedon Sep 25, 2012
You don't need to know the final size. You just need to be able to pass an output size limit. This would be an easy feature to add to functions like zlib.decompress(), since they already handle sizing of the output buffer.
[-]gzip, bz2, lzma: add method to get decompressed size[/-][+]gzip, bz2, lzma: add option to limit output size[/+]serhiy-storchaka commentedon Sep 25, 2012
Agree with Antoine. This would be a desirable feature.
nadeemvawda commentedon Nov 5, 2012
I agree that being able to limit output size is useful and desirable, but
I'm not keen on copying the max_length/unconsumed_tail approach used by
zlib's decompressor class. It feels awkward to use, and it complicates
the implementation of the existing decompress() method, which is already
unwieldy enough.
As an alternative, I propose a thin wrapper around the underlying C API:
This would store decompressed data in a caller-provided bytearray, and
return a pair of integers indicating the end points of the consumed and
produced data in the respective buffers.
The implementation should be extremely simple - it does not need to do
any memory allocation or reference management.
I think it could also be useful for optimizing the implementation of
BZ2File and LZMAFile. I plan to write a prototype and run some benchmarks
some time in the next few weeks.
(Aside: if implemented for zlib, this could also be a nicer (I think)
solution for the problem raised in bpo-5804.)
serhiy-storchaka commentedon Nov 5, 2012
unconsumed_tail should be private hidden attribute, which automatically prepends any consumed data. This should not be very complicated. But benchmarks needed to show what kind of approach is more efficient.
nadeemvawda commentedon Nov 6, 2012
I suspect that it will be slower than the decompress_into() approach, but
as you say, we need to do benchmarks to see for sure.
nadeemvawda commentedon Dec 2, 2012
I've tried reimplementing LZMAFile in terms of the decompress_into()
method, and it has ended up not being any faster than the existing
implementation. (It is _slightly_ faster for readinto() with a large
buffer size, but all other cases it was either of equal performance or
significantly slower.)
In addition, decompress_into() is more complicated to work with than I
had expected, so I withdraw my objection to the approach based on
max_length/unconsumed_tail.
I don't think this is a good idea. In order to have predictable memory
usage, the caller will need to ensure that the current input is fully
decompressed before passing in the next block of compressed data. This
can be done more simply with the interface used by zlib. Compare:
with:
A related, but orthogonal proposal: We might want to make unconsumed_tail
a memoryview (provided the input data is know to be immutable), to avoid
creating an unnecessary copy of the data.
serhiy-storchaka commentedon Dec 4, 2012
This is not usable with bzip2. Bzip2 uses large block size and unconsumed_tail
can be non empty but decompress() will return b''. With zlib you possible can
see the same effect on some input when read by one byte.
It looks interesting. However the data should be copied anyway if the input
data is not a bytes object.
serhiy-storchaka commentedon Dec 4, 2012
Actually it should be:
Note that you should use d.unconsumed_tail + compressed as input, and therefore do an unnecessary copy of the data.
Without explicit unconsumed_tail you can write input data in the internal mutable buffer, it will be more effective for large buffer (handreds of KB) and small input chunks (several KB).
nadeemvawda commentedon Dec 9, 2012
I don't see how this is a problem. If (for some strange reason) the
application-specific processing code can't handle empty blocks properly, you can
just stick "if not output: continue" before it.
Why is this necessary? If unconsumed_tail is b'', then there's no need to
prepend it (and the concatenation would be a no-op anyway). If unconsumed_tail
does contain data, then we don't need to read additional compressed data from
the file until we've finished decompressing the data we already have.
Are you proposing that the decompressor object maintain its own buffer, and
copy the input data into it before passing it to the decompression library?
Doesn't that just duplicate work that the library is already doing for us?
32 remaining items
vadmium commentedon Feb 21, 2015
The new bzip parameter still needs documenting in the library reference.
However I reviewed the doc string, C code, and tests, and I can�’t find anything wrong.
nikratio commentedon Feb 22, 2015
Updated patch attached.
nikratio commentedon Feb 23, 2015
Attached rev3 with fixed indentation and explicit cast to avoid compiler warning on 32-bit systems.
vadmium commentedon Feb 23, 2015
Rev3 still seems to have the same weird indentation as rev2. Are you using some sort of diff --ignore-all-space option by accident?
nikratio commentedon Feb 23, 2015
On Feb 23 2015, Martin Panter <report@bugs.python.org> wrote:
I used "diff -w" for revision 2, but revision 3 should be good. Can you
give me a line number where you see problems in rev3?
Best,
-Nikolaus
--
GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F
nikratio commentedon Feb 24, 2015
One more try before I give up.
nikratio commentedon Feb 24, 2015
grmblfhargs#@$@#
nikratio commentedon Feb 24, 2015
Revision 7 of the bz2 patch is attached.
Apologies for the flood of revisions to everyone and many thanks to Martin for noticing the problems every time, it boggles my mind that it took me 5 tries to add a simple cast.
Antoine, I think this is ready for commit now.
nikratio commentedon Feb 26, 2015
Martin, I'll try to review your GzipFile patch. But maybe it would make sense to open a separate issue for this?
I think the LZMAFile patch has not yet been reviewed or committed, and we probably want a patch for BZ2File too. The review page is already pretty cluttered right now, so I think it would make sense to use this issue for the low-level compressor/decompressor API and handle the *File API separately.
vadmium commentedon Feb 26, 2015
Yes I think that is a good idea:
python-dev commentedon Feb 26, 2015
New changeset 10dab1b596d4 by Antoine Pitrou in branch 'default':
Issue bpo-15955: Add an option to limit the output size in bz2.decompress().
https://hg.python.org/cpython/rev/10dab1b596d4
pitrou commentedon Feb 26, 2015
Thank you, Nikolaus, and thank you Martin for the reviews.
[-]gzip, bz2, lzma: add option to limit output size[/-][+]bz2, lzma: add option to limit output size[/+]