@@ -1441,6 +1441,11 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1441
1441
}
1442
1442
Py_ssize_t bytes_written = 0 ;
1443
1443
while (1 ) {
1444
+ /* Allow escaping the GIL except when updating the buffer or when
1445
+ throwing errors. This makes a big difference for BGZF format gzip
1446
+ blocks. */
1447
+ PyThreadState * _save ;
1448
+ Py_UNBLOCK_THREADS
1444
1449
uint8_t * current_pos = self -> current_pos ;
1445
1450
uint8_t * buffer_end = self -> buffer_end ;
1446
1451
switch (self -> stream_phase ) {
@@ -1452,6 +1457,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1452
1457
// Reached EOF
1453
1458
self -> _size = self -> _pos ;
1454
1459
self -> current_pos = current_pos ;
1460
+ Py_BLOCK_THREADS ;
1455
1461
return bytes_written ;
1456
1462
}
1457
1463
if ((remaining ) < 10 ) {
@@ -1461,13 +1467,15 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1461
1467
uint8_t magic2 = current_pos [1 ];
1462
1468
1463
1469
if (!(magic1 == 0x1f && magic2 == 0x8b )) {
1464
- PyErr_Format (BadGzipFile ,
1470
+ Py_BLOCK_THREADS ;
1471
+ PyErr_Format (BadGzipFile ,
1465
1472
"Not a gzipped file (%R)" ,
1466
1473
PyBytes_FromStringAndSize ((char * )current_pos , 2 ));
1467
1474
return -1 ;
1468
1475
};
1469
1476
uint8_t method = current_pos [2 ];
1470
1477
if (method != 8 ) {
1478
+ Py_BLOCK_THREADS ;
1471
1479
PyErr_SetString (BadGzipFile , "Unknown compression method" );
1472
1480
return -1 ;
1473
1481
}
@@ -1511,6 +1519,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1511
1519
uint16_t crc = crc32_gzip_refl (
1512
1520
0 , current_pos , header_cursor - current_pos ) & 0xFFFF ;
1513
1521
if (header_crc != crc ) {
1522
+ Py_BLOCK_THREADS ;
1514
1523
PyErr_Format (
1515
1524
BadGzipFile ,
1516
1525
"Corrupted gzip header. Checksums do not "
@@ -1530,10 +1539,9 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1530
1539
self -> state .next_out = out_buffer ;
1531
1540
self -> state .avail_out = out_buffer_size ;
1532
1541
int ret ;
1533
- Py_BEGIN_ALLOW_THREADS
1534
1542
ret = isal_inflate (& self -> state );
1535
- Py_END_ALLOW_THREADS
1536
1543
if (ret != ISAL_DECOMP_OK ) {
1544
+ Py_BLOCK_THREADS ;
1537
1545
isal_inflate_error (ret );
1538
1546
return -1 ;
1539
1547
}
@@ -1548,6 +1556,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1548
1556
break ;
1549
1557
}
1550
1558
self -> current_pos = current_pos ;
1559
+ Py_BLOCK_THREADS ;
1551
1560
return bytes_written ;
1552
1561
}
1553
1562
current_pos -= bitbuffer_size (& self -> state );
@@ -1560,6 +1569,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1560
1569
uint32_t crc = * (uint32_t * )current_pos ;
1561
1570
current_pos += 4 ;
1562
1571
if (crc != self -> state .crc ) {
1572
+ Py_BLOCK_THREADS ;
1563
1573
PyErr_Format (
1564
1574
BadGzipFile ,
1565
1575
"CRC check failed %u != %u" ,
@@ -1570,6 +1580,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1570
1580
uint32_t length = * (uint32_t * )current_pos ;
1571
1581
current_pos += 4 ;
1572
1582
if (length != self -> state .total_out ) {
1583
+ Py_BLOCK_THREADS ;
1573
1584
PyErr_SetString (BadGzipFile , "Incorrect length of data produced" );
1574
1585
return -1 ;
1575
1586
}
@@ -1590,6 +1601,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1590
1601
default :
1591
1602
Py_UNREACHABLE ();
1592
1603
}
1604
+ Py_BLOCK_THREADS ;
1593
1605
// If buffer_end is reached, nothing was returned and all bytes are
1594
1606
// read we have an EOFError.
1595
1607
if (self -> all_bytes_read ) {
0 commit comments