@@ -145,34 +145,24 @@ compress(BZ2Compressor *c, char *data, size_t len, int action)
145145 result = PyBytes_FromStringAndSize (NULL , SMALLCHUNK );
146146 if (result == NULL )
147147 return NULL ;
148+
148149 c -> bzs .next_in = data ;
149- /* On a 64-bit system, len might not fit in avail_in (an unsigned int).
150- Do compression in chunks of no more than UINT_MAX bytes each. */
151- c -> bzs .avail_in = MIN (len , UINT_MAX );
152- len -= c -> bzs .avail_in ;
150+ c -> bzs .avail_in = 0 ;
153151 c -> bzs .next_out = PyBytes_AS_STRING (result );
154152 c -> bzs .avail_out = PyBytes_GET_SIZE (result );
155153 for (;;) {
156154 char * this_out ;
157155 int bzerror ;
158156
159- Py_BEGIN_ALLOW_THREADS
160- this_out = c -> bzs .next_out ;
161- bzerror = BZ2_bzCompress (& c -> bzs , action );
162- data_size += c -> bzs .next_out - this_out ;
163- Py_END_ALLOW_THREADS
164- if (catch_bz2_error (bzerror ))
165- goto error ;
166-
157+ /* On a 64-bit system, len might not fit in avail_in (an unsigned int).
158+ Do compression in chunks of no more than UINT_MAX bytes each. */
167159 if (c -> bzs .avail_in == 0 && len > 0 ) {
168160 c -> bzs .avail_in = MIN (len , UINT_MAX );
169161 len -= c -> bzs .avail_in ;
170162 }
171163
172- /* In regular compression mode, stop when input data is exhausted.
173- In flushing mode, stop when all buffered data has been flushed. */
174- if ((action == BZ_RUN && c -> bzs .avail_in == 0 ) ||
175- (action == BZ_FINISH && bzerror == BZ_STREAM_END ))
164+ /* In regular compression mode, stop when input data is exhausted. */
165+ if (action == BZ_RUN && c -> bzs .avail_in == 0 )
176166 break ;
177167
178168 if (c -> bzs .avail_out == 0 ) {
@@ -185,6 +175,18 @@ compress(BZ2Compressor *c, char *data, size_t len, int action)
185175 }
186176 c -> bzs .avail_out = MIN (buffer_left , UINT_MAX );
187177 }
178+
179+ Py_BEGIN_ALLOW_THREADS
180+ this_out = c -> bzs .next_out ;
181+ bzerror = BZ2_bzCompress (& c -> bzs , action );
182+ data_size += c -> bzs .next_out - this_out ;
183+ Py_END_ALLOW_THREADS
184+ if (catch_bz2_error (bzerror ))
185+ goto error ;
186+
187+ /* In flushing mode, stop when all buffered data has been flushed. */
188+ if (action == BZ_FINISH && bzerror == BZ_STREAM_END )
189+ break ;
188190 }
189191 if (data_size != PyBytes_GET_SIZE (result ))
190192 if (_PyBytes_Resize (& result , data_size ) < 0 )
0 commit comments