Mercurial > cpython-lru_cache-weakref
annotate Lib/gzip.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | e044fa016c85 |
| children | 0b5ba5f610a9 d129982c063d |
| rev | line source |
|---|---|
|
11708
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
1 """Functions that read and write gzipped files. |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
2 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11692
diff
changeset
|
3 The user of the file doesn't have to worry about the compression, |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11692
diff
changeset
|
4 but random access is not allowed.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11692
diff
changeset
|
5 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11692
diff
changeset
|
6 # based on Andrew Kuchling's minigzip.py distributed with the zlib module |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11692
diff
changeset
|
7 |
|
56962
8cff949323c9
Merged revisions 75935 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents:
53909
diff
changeset
|
8 import struct, sys, time, os |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 import zlib |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43020
diff
changeset
|
10 import builtins |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
11 import io |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
64125
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
13 __all__ = ["GzipFile", "open", "compress", "decompress"] |
|
16160
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
14 |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 |
|
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 READ, WRITE = 1, 2 |
|
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
19 def U32(i): |
|
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
20 """Return i as an unsigned integer, assuming it fits in 32 bits. |
|
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
21 If it's >= 2GB when viewed as a 32-bit unsigned int, return a long. |
|
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
22 """ |
|
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
23 if i < 0: |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
24 i += 1 << 32 |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
25 return i |
|
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
26 |
|
26050
c54716aed26a
Another round on SF patch 618135: gzip.py and files > 2G
Tim Peters <tim.peters@gmail.com>
parents:
26019
diff
changeset
|
27 def LOWU32(i): |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45955
diff
changeset
|
28 """Return the low-order 32 bits, as a non-negative int""" |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
29 return i & 0xFFFFFFFF |
|
26050
c54716aed26a
Another round on SF patch 618135: gzip.py and files > 2G
Tim Peters <tim.peters@gmail.com>
parents:
26019
diff
changeset
|
30 |
|
10569
f58046339cbf
Two different changes.
Guido van Rossum <guido@python.org>
parents:
10508
diff
changeset
|
31 def write32u(output, value): |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
32 # The L format writes the bit pattern correctly whether signed |
|
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
33 # or unsigned. |
|
10569
f58046339cbf
Two different changes.
Guido van Rossum <guido@python.org>
parents:
10508
diff
changeset
|
34 output.write(struct.pack("<L", value)) |
|
f58046339cbf
Two different changes.
Guido van Rossum <guido@python.org>
parents:
10508
diff
changeset
|
35 |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 def read32(input): |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45955
diff
changeset
|
37 return struct.unpack("<I", input.read(4))[0] |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 |
|
10508
d79989a435d9
Oops, missed mode parameter to open().
Fred Drake <fdrake@acm.org>
parents:
10507
diff
changeset
|
39 def open(filename, mode="rb", compresslevel=9): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
40 """Shorthand for GzipFile(filename, mode, compresslevel). |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
41 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
42 The filename argument is required; mode defaults to 'rb' |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
43 and compresslevel defaults to 9. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
44 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
45 """ |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 return GzipFile(filename, mode, compresslevel) |
|
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 |
|
64906
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
48 class _PaddedFile: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
49 """Minimal read-only file object that prepends a string to the contents |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
50 of an actual file. Shouldn't be used outside of gzip.py, as it lacks |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
51 essential functionality.""" |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
52 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
53 def __init__(self, f, prepend=b''): |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
54 self._buffer = prepend |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
55 self._length = len(prepend) |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
56 self.file = f |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
57 self._read = 0 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
58 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
59 def read(self, size): |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
60 if self._read is None: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
61 return self.file.read(size) |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
62 if self._read + size <= self._length: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
63 read = self._read |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
64 self._read += size |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
65 return self._buffer[read:self._read] |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
66 else: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
67 read = self._read |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
68 self._read = None |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
69 return self._buffer[read:] + \ |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
70 self.file.read(size-self._length+read) |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
71 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
72 def prepend(self, prepend=b'', readprevious=False): |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
73 if self._read is None: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
74 self._buffer = prepend |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
75 elif readprevious and len(prepend) <= self._read: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
76 self._read -= len(prepend) |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
77 return |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
78 else: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
79 self._buffer = self._buffer[read:] + prepend |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
80 self._length = len(self._buffer) |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
81 self._read = 0 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
82 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
83 def unused(self): |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
84 if self._read is None: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
85 return b'' |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
86 return self._buffer[self._read:] |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
87 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
88 def seek(self, offset, whence=0): |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
89 # This is only ever called with offset=whence=0 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
90 if whence == 1 and self._read is not None: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
91 if 0 <= offset + self._read <= self._length: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
92 self._read += offset |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
93 return |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
94 else: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
95 offset += self._length - self._read |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
96 self._read = None |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
97 self._buffer = None |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
98 return self.file.seek(offset, whence) |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
99 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
100 def __getattr__(self, name): |
|
66270
245f6ca3b233
#10465: fix broken delegation in __getattr__ of _PaddedFile.
Georg Brandl <georg@python.org>
parents:
65151
diff
changeset
|
101 return getattr(self.file, name) |
|
64906
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
102 |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
103 |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
104 class GzipFile(io.BufferedIOBase): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
105 """The GzipFile class simulates most of the methods of a file object with |
|
24601
262ad16b4de0
Remove mention of deprecated xreadlines method.
Guido van Rossum <guido@python.org>
parents:
23565
diff
changeset
|
106 the exception of the readinto() and truncate() methods. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
107 |
|
74872
4b32309631da
Issue #13989: Document that GzipFile does not support text mode.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74493
diff
changeset
|
108 This class only supports opening files in binary mode. If you need to open a |
|
4b32309631da
Issue #13989: Document that GzipFile does not support text mode.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74493
diff
changeset
|
109 compressed file in text mode, wrap your GzipFile with an io.TextIOWrapper. |
|
4b32309631da
Issue #13989: Document that GzipFile does not support text mode.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74493
diff
changeset
|
110 |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
111 """ |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 |
|
5872
dd0e9b646616
Add the option to pass an open file object to GzipFile. This obviates
Guido van Rossum <guido@python.org>
parents:
5386
diff
changeset
|
113 myfileobj = None |
|
35050
e79793a8eeea
[Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents:
34818
diff
changeset
|
114 max_read_chunk = 10 * 1024 * 1024 # 10Mb |
|
5872
dd0e9b646616
Add the option to pass an open file object to GzipFile. This obviates
Guido van Rossum <guido@python.org>
parents:
5386
diff
changeset
|
115 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
116 def __init__(self, filename=None, mode=None, |
|
50604
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
117 compresslevel=9, fileobj=None, mtime=None): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
118 """Constructor for the GzipFile class. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
119 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
120 At least one of fileobj and filename must be given a |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
121 non-trivial value. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
122 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
123 The new class instance is based on fileobj, which can be a regular |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
124 file, a StringIO object, or any other object which simulates a file. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
125 It defaults to None, in which case filename is opened to provide |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
126 a file object. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
127 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
128 When fileobj is not None, the filename argument is only used to be |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
129 included in the gzip file header, which may includes the original |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
130 filename of the uncompressed file. It defaults to the filename of |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
131 fileobj, if discernible; otherwise, it defaults to the empty string, |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
132 and in this case the original filename is not included in the header. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
133 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
134 The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', or 'wb', |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
135 depending on whether the file will be read or written. The default |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
136 is the mode of fileobj if discernible; otherwise, the default is 'rb'. |
|
74872
4b32309631da
Issue #13989: Document that GzipFile does not support text mode.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74493
diff
changeset
|
137 A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and |
|
4b32309631da
Issue #13989: Document that GzipFile does not support text mode.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74493
diff
changeset
|
138 'wb', and 'a' and 'ab'. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
139 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
140 The compresslevel argument is an integer from 1 to 9 controlling the |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
141 level of compression; 1 is fastest and produces the least compression, |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
142 and 9 is slowest and produces the most compression. The default is 9. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
143 |
|
50604
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
144 The mtime argument is an optional numeric timestamp to be written |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
145 to the stream when compressing. All gzip compressed streams |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
146 are required to contain a timestamp. If omitted or None, the |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
147 current time is used. This module ignores the timestamp when |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
148 decompressing; however, some programs, such as gunzip, make use |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
149 of it. The format of the timestamp is the same as that of the |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
150 return value of time.time() and of the st_mtime member of the |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
151 object returned by os.stat(). |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
152 |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
153 """ |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23518
diff
changeset
|
154 |
|
74872
4b32309631da
Issue #13989: Document that GzipFile does not support text mode.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74493
diff
changeset
|
155 if mode and ('t' in mode or 'U' in mode): |
|
4b32309631da
Issue #13989: Document that GzipFile does not support text mode.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74493
diff
changeset
|
156 raise IOError("Mode " + mode + " not supported") |
|
23518
c03e8899d4b1
force gzip module to open files using 'b'inary mode.
Skip Montanaro <skip@pobox.com>
parents:
23075
diff
changeset
|
157 if mode and 'b' not in mode: |
|
c03e8899d4b1
force gzip module to open files using 'b'inary mode.
Skip Montanaro <skip@pobox.com>
parents:
23075
diff
changeset
|
158 mode += 'b' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
159 if fileobj is None: |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43020
diff
changeset
|
160 fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') |
|
5872
dd0e9b646616
Add the option to pass an open file object to GzipFile. This obviates
Guido van Rossum <guido@python.org>
parents:
5386
diff
changeset
|
161 if filename is None: |
|
77520
e044fa016c85
Fix GzipFile's handling of filenames given as bytes objects.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74872
diff
changeset
|
162 filename = getattr(fileobj, 'name', '') |
|
e044fa016c85
Fix GzipFile's handling of filenames given as bytes objects.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74872
diff
changeset
|
163 if not isinstance(filename, (str, bytes)): |
|
74493
7d405058e458
Issue #13781: Fix GzipFile to work with os.fdopen()'d file objects.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
68257
diff
changeset
|
164 filename = '' |
|
5872
dd0e9b646616
Add the option to pass an open file object to GzipFile. This obviates
Guido van Rossum <guido@python.org>
parents:
5386
diff
changeset
|
165 if mode is None: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
166 if hasattr(fileobj, 'mode'): mode = fileobj.mode |
|
10507
753f3f95c50f
Made the default mode 'rb' instead of 'r', for better cross-platform
Fred Drake <fdrake@acm.org>
parents:
10470
diff
changeset
|
167 else: mode = 'rb' |
|
5872
dd0e9b646616
Add the option to pass an open file object to GzipFile. This obviates
Guido van Rossum <guido@python.org>
parents:
5386
diff
changeset
|
168 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
169 if mode[0:1] == 'r': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
170 self.mode = READ |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
171 # Set flag indicating start of a new member |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22417
diff
changeset
|
172 self._new_member = True |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
173 # Buffer data read from gzip file. extrastart is offset in |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
174 # stream where buffer starts. extrasize is number of |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
175 # bytes remaining in buffer from current stream position. |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
176 self.extrabuf = b"" |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
177 self.extrasize = 0 |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
178 self.extrastart = 0 |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40775
diff
changeset
|
179 self.name = filename |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
180 # Starts small, scales exponentially |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
181 self.min_readsize = 100 |
|
64906
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
182 fileobj = _PaddedFile(fileobj) |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
184 elif mode[0:1] == 'w' or mode[0:1] == 'a': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
185 self.mode = WRITE |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
186 self._init_write(filename) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
187 self.compress = zlib.compressobj(compresslevel, |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
188 zlib.DEFLATED, |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
189 -zlib.MAX_WBITS, |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
190 zlib.DEF_MEM_LEVEL, |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
191 0) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
192 else: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42573
diff
changeset
|
193 raise IOError("Mode " + mode + " not supported") |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
195 self.fileobj = fileobj |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
196 self.offset = 0 |
|
50604
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
197 self.mtime = mtime |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
199 if self.mode == WRITE: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
200 self._write_gzip_header() |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40775
diff
changeset
|
202 @property |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40775
diff
changeset
|
203 def filename(self): |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40775
diff
changeset
|
204 import warnings |
|
53909
238502460795
#4351: more appropriate DeprecationWarning stacklevels
Philip Jenvey <pjenvey@underboss.org>
parents:
50700
diff
changeset
|
205 warnings.warn("use the name attribute", DeprecationWarning, 2) |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40775
diff
changeset
|
206 if self.mode == WRITE and self.name[-3:] != ".gz": |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40775
diff
changeset
|
207 return self.name + ".gz" |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40775
diff
changeset
|
208 return self.name |
|
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40775
diff
changeset
|
209 |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 def __repr__(self): |
|
65012
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
211 fileobj = self.fileobj |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
212 if isinstance(fileobj, _PaddedFile): |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
213 fileobj = fileobj.file |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
214 s = repr(fileobj) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
215 return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>' |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 |
|
65153
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
217 def _check_closed(self): |
|
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
218 """Raises a ValueError if the underlying file object has been closed. |
|
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
219 |
|
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
220 """ |
|
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
221 if self.closed: |
|
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
222 raise ValueError('I/O operation on closed file.') |
|
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
223 |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 def _init_write(self, filename): |
|
40893
32c4897b5d46
Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40775
diff
changeset
|
225 self.name = filename |
|
57736
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
53909
diff
changeset
|
226 self.crc = zlib.crc32(b"") & 0xffffffff |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
227 self.size = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
228 self.writebuf = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
229 self.bufsize = 0 |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 |
|
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 def _write_gzip_header(self): |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
232 self.fileobj.write(b'\037\213') # magic header |
|
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
233 self.fileobj.write(b'\010') # compression method |
|
42573
fb069eafaf89
Retouch my last change after a comment on style from Guido.
Lars Gustäbel <lars@gustaebel.de>
parents:
42522
diff
changeset
|
234 try: |
|
fb069eafaf89
Retouch my last change after a comment on style from Guido.
Lars Gustäbel <lars@gustaebel.de>
parents:
42522
diff
changeset
|
235 # RFC 1952 requires the FNAME field to be Latin-1. Do not |
|
fb069eafaf89
Retouch my last change after a comment on style from Guido.
Lars Gustäbel <lars@gustaebel.de>
parents:
42522
diff
changeset
|
236 # include filenames that cannot be represented that way. |
|
56962
8cff949323c9
Merged revisions 75935 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents:
53909
diff
changeset
|
237 fname = os.path.basename(self.name) |
|
77520
e044fa016c85
Fix GzipFile's handling of filenames given as bytes objects.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74872
diff
changeset
|
238 if not isinstance(fname, bytes): |
|
e044fa016c85
Fix GzipFile's handling of filenames given as bytes objects.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
74872
diff
changeset
|
239 fname = fname.encode('latin-1') |
|
42573
fb069eafaf89
Retouch my last change after a comment on style from Guido.
Lars Gustäbel <lars@gustaebel.de>
parents:
42522
diff
changeset
|
240 if fname.endswith(b'.gz'): |
|
fb069eafaf89
Retouch my last change after a comment on style from Guido.
Lars Gustäbel <lars@gustaebel.de>
parents:
42522
diff
changeset
|
241 fname = fname[:-3] |
|
fb069eafaf89
Retouch my last change after a comment on style from Guido.
Lars Gustäbel <lars@gustaebel.de>
parents:
42522
diff
changeset
|
242 except UnicodeEncodeError: |
|
fb069eafaf89
Retouch my last change after a comment on style from Guido.
Lars Gustäbel <lars@gustaebel.de>
parents:
42522
diff
changeset
|
243 fname = b'' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
244 flags = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
245 if fname: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
246 flags = FNAME |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
247 self.fileobj.write(chr(flags).encode('latin-1')) |
|
50604
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
248 mtime = self.mtime |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
249 if mtime is None: |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
250 mtime = time.time() |
|
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
251 write32u(self.fileobj, int(mtime)) |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
252 self.fileobj.write(b'\002') |
|
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
253 self.fileobj.write(b'\377') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
254 if fname: |
|
42522
f37016d42729
RFC 1952 requires the FNAME field to be Latin-1. Do not include
Lars Gustäbel <lars@gustaebel.de>
parents:
41965
diff
changeset
|
255 self.fileobj.write(fname + b'\000') |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 |
|
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 def _init_read(self): |
|
57736
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
53909
diff
changeset
|
258 self.crc = zlib.crc32(b"") & 0xffffffff |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
259 self.size = 0 |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 |
|
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 def _read_gzip_header(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
262 magic = self.fileobj.read(2) |
|
64906
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
263 if magic == b'': |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
264 raise EOFError("Reached EOF") |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
265 |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
266 if magic != b'\037\213': |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42573
diff
changeset
|
267 raise IOError('Not a gzipped file') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
268 method = ord( self.fileobj.read(1) ) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
269 if method != 8: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42573
diff
changeset
|
270 raise IOError('Unknown compression method') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
271 flag = ord( self.fileobj.read(1) ) |
|
50604
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
272 self.mtime = read32(self.fileobj) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
273 # extraflag = self.fileobj.read(1) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
274 # os = self.fileobj.read(1) |
|
50604
6a6f138d4162
Merged revisions 68319 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50596
diff
changeset
|
275 self.fileobj.read(2) |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
277 if flag & FEXTRA: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
278 # Read & discard the extra field, if present |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
279 xlen = ord(self.fileobj.read(1)) |
|
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
280 xlen = xlen + 256*ord(self.fileobj.read(1)) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
281 self.fileobj.read(xlen) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
282 if flag & FNAME: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
283 # Read and discard a null-terminated string containing the filename |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22417
diff
changeset
|
284 while True: |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
285 s = self.fileobj.read(1) |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
286 if not s or s==b'\000': |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
287 break |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
288 if flag & FCOMMENT: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
289 # Read and discard a null-terminated string containing a comment |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22417
diff
changeset
|
290 while True: |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
291 s = self.fileobj.read(1) |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
292 if not s or s==b'\000': |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
293 break |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
294 if flag & FHCRC: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
295 self.fileobj.read(2) # Read & discard the 16-bit header CRC |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 |
|
64906
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
297 unused = self.fileobj.unused() |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
298 if unused: |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
299 uncompress = self.decompress.decompress(unused) |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
300 self._add_read_data(uncompress) |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 |
|
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 def write(self,data): |
|
65153
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
303 self._check_closed() |
|
22216
64c731d180e9
Patch #443899: Check modes on files before performing operations.
Martin v. Löwis <martin@v.loewis.de>
parents:
20418
diff
changeset
|
304 if self.mode != WRITE: |
|
64c731d180e9
Patch #443899: Check modes on files before performing operations.
Martin v. Löwis <martin@v.loewis.de>
parents:
20418
diff
changeset
|
305 import errno |
|
64c731d180e9
Patch #443899: Check modes on files before performing operations.
Martin v. Löwis <martin@v.loewis.de>
parents:
20418
diff
changeset
|
306 raise IOError(errno.EBADF, "write() on read-only GzipFile object") |
|
23075
b7e87732f6ab
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
22867
diff
changeset
|
307 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
308 if self.fileobj is None: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42573
diff
changeset
|
309 raise ValueError("write() on closed GzipFile object") |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
310 |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
311 # Convert data type if called by io.BufferedWriter. |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
312 if isinstance(data, memoryview): |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
313 data = data.tobytes() |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
314 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
315 if len(data) > 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
316 self.size = self.size + len(data) |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45955
diff
changeset
|
317 self.crc = zlib.crc32(data, self.crc) & 0xffffffff |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
318 self.fileobj.write( self.compress.compress(data) ) |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
319 self.offset += len(data) |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
321 return len(data) |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
322 |
|
11692
0dd520f4dce8
Make read() and readlines() conform more to the file object interface:
Guido van Rossum <guido@python.org>
parents:
11255
diff
changeset
|
323 def read(self, size=-1): |
|
65153
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
324 self._check_closed() |
|
22216
64c731d180e9
Patch #443899: Check modes on files before performing operations.
Martin v. Löwis <martin@v.loewis.de>
parents:
20418
diff
changeset
|
325 if self.mode != READ: |
|
64c731d180e9
Patch #443899: Check modes on files before performing operations.
Martin v. Löwis <martin@v.loewis.de>
parents:
20418
diff
changeset
|
326 import errno |
|
31096
d35f8799cf12
Fix error in exception message.
Brett Cannon <bcannon@gmail.com>
parents:
27705
diff
changeset
|
327 raise IOError(errno.EBADF, "read() on write-only GzipFile object") |
|
23075
b7e87732f6ab
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
22867
diff
changeset
|
328 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
329 if self.extrasize <= 0 and self.fileobj is None: |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
330 return b'' |
|
7169
373d78118113
(This fix is really by Jeremy)
Guido van Rossum <guido@python.org>
parents:
7006
diff
changeset
|
331 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
332 readsize = 1024 |
|
11692
0dd520f4dce8
Make read() and readlines() conform more to the file object interface:
Guido van Rossum <guido@python.org>
parents:
11255
diff
changeset
|
333 if size < 0: # get the whole thing |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
334 try: |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22417
diff
changeset
|
335 while True: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
336 self._read(readsize) |
|
35050
e79793a8eeea
[Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents:
34818
diff
changeset
|
337 readsize = min(self.max_read_chunk, readsize * 2) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
338 except EOFError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
339 size = self.extrasize |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
340 else: # just get some more of it |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
341 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
342 while size > self.extrasize: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
343 self._read(readsize) |
|
35050
e79793a8eeea
[Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents:
34818
diff
changeset
|
344 readsize = min(self.max_read_chunk, readsize * 2) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
345 except EOFError: |
|
8804
5d5a5bc8de5c
Patch by Ron Klatchko: fix invariant in _unread(). Also fixed
Guido van Rossum <guido@python.org>
parents:
8324
diff
changeset
|
346 if size > self.extrasize: |
|
5d5a5bc8de5c
Patch by Ron Klatchko: fix invariant in _unread(). Also fixed
Guido van Rossum <guido@python.org>
parents:
8324
diff
changeset
|
347 size = self.extrasize |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
348 |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
349 offset = self.offset - self.extrastart |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
350 chunk = self.extrabuf[offset: offset + size] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
351 self.extrasize = self.extrasize - size |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
353 self.offset += size |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
354 return chunk |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 |
|
65012
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
356 def peek(self, n): |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
357 if self.mode != READ: |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
358 import errno |
|
65089
5f20f7f15edf
GzipFile.peek improvements, suggested by Nir Aides.
Antoine Pitrou <solipsis@pitrou.net>
parents:
65012
diff
changeset
|
359 raise IOError(errno.EBADF, "peek() on write-only GzipFile object") |
|
65012
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
360 |
|
65089
5f20f7f15edf
GzipFile.peek improvements, suggested by Nir Aides.
Antoine Pitrou <solipsis@pitrou.net>
parents:
65012
diff
changeset
|
361 # Do not return ridiculously small buffers, for one common idiom |
|
5f20f7f15edf
GzipFile.peek improvements, suggested by Nir Aides.
Antoine Pitrou <solipsis@pitrou.net>
parents:
65012
diff
changeset
|
362 # is to call peek(1) and expect more bytes in return. |
|
65012
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
363 if n < 100: |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
364 n = 100 |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
365 if self.extrasize == 0: |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
366 if self.fileobj is None: |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
367 return b'' |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
368 try: |
|
65089
5f20f7f15edf
GzipFile.peek improvements, suggested by Nir Aides.
Antoine Pitrou <solipsis@pitrou.net>
parents:
65012
diff
changeset
|
369 # 1024 is the same buffering heuristic used in read() |
|
5f20f7f15edf
GzipFile.peek improvements, suggested by Nir Aides.
Antoine Pitrou <solipsis@pitrou.net>
parents:
65012
diff
changeset
|
370 self._read(max(n, 1024)) |
|
65012
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
371 except EOFError: |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
372 pass |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
373 offset = self.offset - self.extrastart |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
374 remaining = self.extrasize |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
375 assert remaining == len(self.extrabuf) - offset |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
376 return self.extrabuf[offset:offset + n] |
|
f4d047491cb4
Issue #9962: GzipFile now has the peek() method.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64906
diff
changeset
|
377 |
|
7169
373d78118113
(This fix is really by Jeremy)
Guido van Rossum <guido@python.org>
parents:
7006
diff
changeset
|
378 def _unread(self, buf): |
|
8804
5d5a5bc8de5c
Patch by Ron Klatchko: fix invariant in _unread(). Also fixed
Guido van Rossum <guido@python.org>
parents:
8324
diff
changeset
|
379 self.extrasize = len(buf) + self.extrasize |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
380 self.offset -= len(buf) |
|
7169
373d78118113
(This fix is really by Jeremy)
Guido van Rossum <guido@python.org>
parents:
7006
diff
changeset
|
381 |
|
373d78118113
(This fix is really by Jeremy)
Guido van Rossum <guido@python.org>
parents:
7006
diff
changeset
|
382 def _read(self, size=1024): |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
383 if self.fileobj is None: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42573
diff
changeset
|
384 raise EOFError("Reached EOF") |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
385 |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
386 if self._new_member: |
|
13743
c76e74abb65b
SF patch #100740: Add optional size arguments to .readline() and
Andrew M. Kuchling <amk@amk.ca>
parents:
12619
diff
changeset
|
387 # If the _new_member flag is set, we have to |
|
c76e74abb65b
SF patch #100740: Add optional size arguments to .readline() and
Andrew M. Kuchling <amk@amk.ca>
parents:
12619
diff
changeset
|
388 # jump to the next member, if there is one. |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
389 self._init_read() |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
390 self._read_gzip_header() |
|
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
391 self.decompress = zlib.decompressobj(-zlib.MAX_WBITS) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22417
diff
changeset
|
392 self._new_member = False |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
393 |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
394 # Read a chunk of data from the file |
|
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
395 buf = self.fileobj.read(size) |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
396 |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
397 # If the EOF has been reached, flush the decompression object |
|
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
398 # and mark this object as finished. |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
399 |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
400 if buf == b"": |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
401 uncompress = self.decompress.flush() |
|
64906
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
402 # Prepend the already read bytes to the fileobj to they can be |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
403 # seen by _read_eof() |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
404 self.fileobj.prepend(self.decompress.unused_data, True) |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
405 self._read_eof() |
|
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
406 self._add_read_data( uncompress ) |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42573
diff
changeset
|
407 raise EOFError('Reached EOF') |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
408 |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
409 uncompress = self.decompress.decompress(buf) |
|
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
410 self._add_read_data( uncompress ) |
|
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
411 |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
412 if self.decompress.unused_data != b"": |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
413 # Ending case: we've come to the end of a member in the file, |
|
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
414 # so seek back to the start of the unused data, finish up |
|
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
415 # this member, and read a new gzip header. |
|
64906
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
416 # Prepend the already read bytes to the fileobj to they can be |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
417 # seen by _read_eof() and _read_gzip_header() |
|
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
418 self.fileobj.prepend(self.decompress.unused_data, True) |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
419 # Check the CRC and file size, and set the flag so we read |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
420 # a new member on the next call |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
421 self._read_eof() |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22417
diff
changeset
|
422 self._new_member = True |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
423 |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
424 def _add_read_data(self, data): |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45955
diff
changeset
|
425 self.crc = zlib.crc32(data, self.crc) & 0xffffffff |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
426 offset = self.offset - self.extrastart |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
427 self.extrabuf = self.extrabuf[offset:] + data |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
428 self.extrasize = self.extrasize + len(data) |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
429 self.extrastart = self.offset |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
430 self.size = self.size + len(data) |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 |
|
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 def _read_eof(self): |
|
64906
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
433 # We've read to the end of the file |
|
10470
022b178fd0ed
Based on a suggestion from bruce@hams.com, make a trivial change to
Andrew M. Kuchling <amk@amk.ca>
parents:
10450
diff
changeset
|
434 # We check the that the computed CRC and size of the |
|
26050
c54716aed26a
Another round on SF patch 618135: gzip.py and files > 2G
Tim Peters <tim.peters@gmail.com>
parents:
26019
diff
changeset
|
435 # uncompressed data matches the stored values. Note that the size |
|
c54716aed26a
Another round on SF patch 618135: gzip.py and files > 2G
Tim Peters <tim.peters@gmail.com>
parents:
26019
diff
changeset
|
436 # stored is the true file size mod 2**32. |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
437 crc32 = read32(self.fileobj) |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45955
diff
changeset
|
438 isize = read32(self.fileobj) # may exceed 2GB |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45955
diff
changeset
|
439 if crc32 != self.crc: |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45955
diff
changeset
|
440 raise IOError("CRC check failed %s != %s" % (hex(crc32), |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45955
diff
changeset
|
441 hex(self.crc))) |
|
46042
4a6b093b319b
Merged revisions 61820-61823 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46034
diff
changeset
|
442 elif isize != (self.size & 0xffffffff): |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42573
diff
changeset
|
443 raise IOError("Incorrect length of data produced") |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
444 |
|
58301
ab905f74bf11
Merged revisions 77472-77473 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58138
diff
changeset
|
445 # Gzip files can be padded with zeroes and still have archives. |
|
ab905f74bf11
Merged revisions 77472-77473 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58138
diff
changeset
|
446 # Consume all zero bytes and set the file position to the first |
|
ab905f74bf11
Merged revisions 77472-77473 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58138
diff
changeset
|
447 # non-zero byte. See http://www.gzip.org/#faq8 |
|
ab905f74bf11
Merged revisions 77472-77473 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58138
diff
changeset
|
448 c = b"\x00" |
|
ab905f74bf11
Merged revisions 77472-77473 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58138
diff
changeset
|
449 while c == b"\x00": |
|
ab905f74bf11
Merged revisions 77472-77473 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58138
diff
changeset
|
450 c = self.fileobj.read(1) |
|
ab905f74bf11
Merged revisions 77472-77473 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58138
diff
changeset
|
451 if c: |
|
64906
f4a1c796bc98
Issue #1675951: Allow GzipFile to work with unseekable file objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64125
diff
changeset
|
452 self.fileobj.prepend(c, True) |
|
58301
ab905f74bf11
Merged revisions 77472-77473 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58138
diff
changeset
|
453 |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
454 @property |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
455 def closed(self): |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
456 return self.fileobj is None |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
457 |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 def close(self): |
|
47278
f762a9ab69dd
Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46042
diff
changeset
|
459 if self.fileobj is None: |
|
f762a9ab69dd
Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46042
diff
changeset
|
460 return |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
461 if self.mode == WRITE: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
462 self.fileobj.write(self.compress.flush()) |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45955
diff
changeset
|
463 write32u(self.fileobj, self.crc) |
|
26050
c54716aed26a
Another round on SF patch 618135: gzip.py and files > 2G
Tim Peters <tim.peters@gmail.com>
parents:
26019
diff
changeset
|
464 # self.size may exceed 2GB, or even 4GB |
|
46042
4a6b093b319b
Merged revisions 61820-61823 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46034
diff
changeset
|
465 write32u(self.fileobj, self.size & 0xffffffff) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
466 self.fileobj = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
467 elif self.mode == READ: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
468 self.fileobj = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
469 if self.myfileobj: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
470 self.myfileobj.close() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
471 self.myfileobj = None |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
472 |
|
34650
11fcdbb279de
Patch #1110248: SYNC_FLUSH the zlib buffer for GZipFile.flush.
Martin v. Löwis <martin@v.loewis.de>
parents:
32835
diff
changeset
|
473 def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH): |
|
65153
7c1031d3823c
Merged revisions 85291 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61223
diff
changeset
|
474 self._check_closed() |
|
34650
11fcdbb279de
Patch #1110248: SYNC_FLUSH the zlib buffer for GZipFile.flush.
Martin v. Löwis <martin@v.loewis.de>
parents:
32835
diff
changeset
|
475 if self.mode == WRITE: |
|
34818
4eaab38566c9
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34650
diff
changeset
|
476 # Ensure the compressor's buffer is flushed |
|
4eaab38566c9
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34650
diff
changeset
|
477 self.fileobj.write(self.compress.flush(zlib_mode)) |
|
61223
9ba6fcec4f14
Merged revisions 80764 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
57736
diff
changeset
|
478 self.fileobj.flush() |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 |
|
32834
718305112f9f
Added a new fileno() method. ZODB's repozo.py wants this so it can
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
480 def fileno(self): |
|
718305112f9f
Added a new fileno() method. ZODB's repozo.py wants this so it can
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
481 """Invoke the underlying file object's fileno() method. |
|
718305112f9f
Added a new fileno() method. ZODB's repozo.py wants this so it can
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
482 |
|
718305112f9f
Added a new fileno() method. ZODB's repozo.py wants this so it can
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
483 This will raise AttributeError if the underlying file object |
|
718305112f9f
Added a new fileno() method. ZODB's repozo.py wants this so it can
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
484 doesn't support fileno(). |
|
718305112f9f
Added a new fileno() method. ZODB's repozo.py wants this so it can
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
485 """ |
|
718305112f9f
Added a new fileno() method. ZODB's repozo.py wants this so it can
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
486 return self.fileobj.fileno() |
|
718305112f9f
Added a new fileno() method. ZODB's repozo.py wants this so it can
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
487 |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
488 def rewind(self): |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
489 '''Return the uncompressed stream file position indicator to the |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19088
diff
changeset
|
490 beginning of the file''' |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
491 if self.mode != READ: |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
492 raise IOError("Can't rewind in write mode") |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
493 self.fileobj.seek(0) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22417
diff
changeset
|
494 self._new_member = True |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
495 self.extrabuf = b"" |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
496 self.extrasize = 0 |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
497 self.extrastart = 0 |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
498 self.offset = 0 |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
499 |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
500 def readable(self): |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
501 return self.mode == READ |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
502 |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
503 def writable(self): |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
504 return self.mode == WRITE |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
505 |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
506 def seekable(self): |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
507 return True |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
508 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
509 def seek(self, offset, whence=0): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
510 if whence: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
511 if whence == 1: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
512 offset = self.offset + offset |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
513 else: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
514 raise ValueError('Seek from end not supported') |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
515 if self.mode == WRITE: |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
516 if offset < self.offset: |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
517 raise IOError('Negative seek in write mode') |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
518 count = offset - self.offset |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
519 chunk = bytes(1024) |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
520 for i in range(count // 1024): |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
521 self.write(chunk) |
|
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
522 self.write(bytes(count % 1024)) |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
523 elif self.mode == READ: |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
524 if offset < self.offset: |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
525 # for negative seek, rewind and do positive seek |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
526 self.rewind() |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
527 count = offset - self.offset |
|
26019
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
528 for i in range(count // 1024): |
|
6fa5c95408eb
Related to SF patch 618135: gzip.py and files > 2G.
Tim Peters <tim.peters@gmail.com>
parents:
24601
diff
changeset
|
529 self.read(1024) |
|
19088
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
530 self.read(count % 1024) |
|
1960ceec1a00
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17083
diff
changeset
|
531 |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
532 return self.offset |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
533 |
|
13743
c76e74abb65b
SF patch #100740: Add optional size arguments to .readline() and
Andrew M. Kuchling <amk@amk.ca>
parents:
12619
diff
changeset
|
534 def readline(self, size=-1): |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
535 if size < 0: |
|
58138
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
536 # Shortcut common case - newline found in buffer. |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
537 offset = self.offset - self.extrastart |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
538 i = self.extrabuf.find(b'\n', offset) + 1 |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
539 if i > 0: |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
540 self.extrasize -= i - offset |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
541 self.offset += i - offset |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
542 return self.extrabuf[offset: i] |
|
c5802ab9e34e
Merged revisions 77288 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57734
diff
changeset
|
543 |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
44098
diff
changeset
|
544 size = sys.maxsize |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
545 readsize = self.min_readsize |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
546 else: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
547 readsize = size |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
548 bufs = [] |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
549 while size != 0: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
550 c = self.read(readsize) |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
551 i = c.find(b'\n') |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
552 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
553 # We set i=size to break out of the loop under two |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
554 # conditions: 1) there's no newline, and the chunk is |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
555 # larger than size, or 2) there is a newline, but the |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
556 # resulting line would be longer than 'size'. |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
557 if (size <= i) or (i == -1 and len(c) > size): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
558 i = size - 1 |
|
13743
c76e74abb65b
SF patch #100740: Add optional size arguments to .readline() and
Andrew M. Kuchling <amk@amk.ca>
parents:
12619
diff
changeset
|
559 |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
560 if i >= 0 or c == b'': |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
561 bufs.append(c[:i + 1]) # Add portion of last chunk |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
562 self._unread(c[i + 1:]) # Push back rest of chunk |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
563 break |
|
5386
43c5e1f9154e
Adding Jeremy Hylton's gzip module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
564 |
|
13743
c76e74abb65b
SF patch #100740: Add optional size arguments to .readline() and
Andrew M. Kuchling <amk@amk.ca>
parents:
12619
diff
changeset
|
565 # Append chunk to list, decrease 'size', |
|
c76e74abb65b
SF patch #100740: Add optional size arguments to .readline() and
Andrew M. Kuchling <amk@amk.ca>
parents:
12619
diff
changeset
|
566 bufs.append(c) |
|
c76e74abb65b
SF patch #100740: Add optional size arguments to .readline() and
Andrew M. Kuchling <amk@amk.ca>
parents:
12619
diff
changeset
|
567 size = size - len(c) |
|
c76e74abb65b
SF patch #100740: Add optional size arguments to .readline() and
Andrew M. Kuchling <amk@amk.ca>
parents:
12619
diff
changeset
|
568 readsize = min(size, readsize * 2) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
569 if readsize > self.min_readsize: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35050
diff
changeset
|
570 self.min_readsize = min(readsize, self.min_readsize * 2, 512) |
|
41965
4596727f6c39
Fix gzip.py: Use bytes where 8bit strings have been used formerly.
Walter Dörwald <walter@livinglogic.de>
parents:
41501
diff
changeset
|
571 return b''.join(bufs) # Return resulting line |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13743
diff
changeset
|
572 |
|
7006
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
573 |
|
64125
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
574 def compress(data, compresslevel=9): |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
575 """Compress data in one shot and return the compressed string. |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
576 Optional argument is the compression level, in range of 1-9. |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
577 """ |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
578 buf = io.BytesIO() |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
579 with GzipFile(fileobj=buf, mode='wb', compresslevel=compresslevel) as f: |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
580 f.write(data) |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
581 return buf.getvalue() |
|
22417
df35656e7d99
Make GzipFile an iterator. Closes bug #532621.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22216
diff
changeset
|
582 |
|
64125
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
583 def decompress(data): |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
584 """Decompress a gzip compressed string in one shot. |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
585 Return the decompressed string. |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
586 """ |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
587 with GzipFile(fileobj=io.BytesIO(data)) as f: |
|
3fa0a9553402
Issue #3488: Provide convenient shorthand functions `gzip.compress`
Antoine Pitrou <solipsis@pitrou.net>
parents:
61222
diff
changeset
|
588 return f.read() |
|
50700
15a056e0f89f
Merged revisions 68484-68485 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50604
diff
changeset
|
589 |
|
7006
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
590 |
|
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
591 def _test(): |
|
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
592 # Act like gzip; with -d, act like gunzip. |
|
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
593 # The input file is not deleted, however, nor are any other gzip |
|
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
594 # options or features supported. |
|
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
595 args = sys.argv[1:] |
|
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
596 decompress = args and args[0] == "-d" |
|
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
597 if decompress: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
598 args = args[1:] |
|
7006
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
599 if not args: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
600 args = ["-"] |
|
7006
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
601 for arg in args: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
602 if decompress: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
603 if arg == "-": |
|
50596
474745b7a12a
Buglet: restore functioning of gzip as an executable when (de)compressing from stdin to stdout
Antoine Pitrou <solipsis@pitrou.net>
parents:
47278
diff
changeset
|
604 f = GzipFile(filename="", mode="rb", fileobj=sys.stdin.buffer) |
|
474745b7a12a
Buglet: restore functioning of gzip as an executable when (de)compressing from stdin to stdout
Antoine Pitrou <solipsis@pitrou.net>
parents:
47278
diff
changeset
|
605 g = sys.stdout.buffer |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
606 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
607 if arg[-3:] != ".gz": |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
608 print("filename doesn't end in .gz:", repr(arg)) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
609 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
610 f = open(arg, "rb") |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43020
diff
changeset
|
611 g = builtins.open(arg[:-3], "wb") |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
612 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
613 if arg == "-": |
|
50596
474745b7a12a
Buglet: restore functioning of gzip as an executable when (de)compressing from stdin to stdout
Antoine Pitrou <solipsis@pitrou.net>
parents:
47278
diff
changeset
|
614 f = sys.stdin.buffer |
|
474745b7a12a
Buglet: restore functioning of gzip as an executable when (de)compressing from stdin to stdout
Antoine Pitrou <solipsis@pitrou.net>
parents:
47278
diff
changeset
|
615 g = GzipFile(filename="", mode="wb", fileobj=sys.stdout.buffer) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
616 else: |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43020
diff
changeset
|
617 f = builtins.open(arg, "rb") |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
618 g = open(arg + ".gz", "wb") |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22417
diff
changeset
|
619 while True: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
620 chunk = f.read(1024) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
621 if not chunk: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
622 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
623 g.write(chunk) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
624 if g is not sys.stdout: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
625 g.close() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
626 if f is not sys.stdin: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7169
diff
changeset
|
627 f.close() |
|
7006
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
628 |
|
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
629 if __name__ == '__main__': |
|
9773d3941c96
When there's no filename, don't make one up.
Guido van Rossum <guido@python.org>
parents:
5872
diff
changeset
|
630 _test() |
