python3.7.17 _bz2解压报“段错误(吐核)”

centos 用网上给的方法www.cnblogs.com/jifeng/p/11221691....
加载了_bz2,调用后,压缩没问题,解压时报段错误。

代码如下:
import bz2

def decompress_file(input_file, output_file):
with open(input_file, ‘rb’) as f_in:
with open(output_file, ‘wb’) as f_out:
decompressor = bz2.BZ2Decompressor()
for data in iter(lambda: f_in.read(4096), b’’):
print(3)
decompressed_data = decompressor.decompress(data)
print(5)
f_out.write(decompressed_data)
f_out.close()

i=0;
decompress_file(‘compressed.bz2’, ‘output.txt’)
结果
3
段错误(吐核)

不知道什么原因,有人可以解答下么?

讨论数量: 1
Jason990420

代码区塊

```python

你的代码

```

And where’s the Traceback information for the exception ?

Following code work fine on my WIN10, and bz2 is a built-in library on my Python 3.11.2

import bz2

def decompress_file(input_file, output_file):
    with open(input_file, 'rb') as f_in:
        with open(output_file, 'wb') as f_out:
            decompressor = bz2.BZ2Decompressor()
            for data in iter(lambda: f_in.read(4096), b''):
                decompressed_data = decompressor.decompress(data)
                f_out.write(decompressed_data)

data = b"""
Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue
nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,
sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus
pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.
Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo
felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum
dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum.""".strip()*4095

compressed_data = bz2.compress(data)

with open("compressed.bz2", "wb") as source:
    source.write(compressed_data)

decompress_file('compressed.bz2', 'output.txt')

with open('output.txt', "rb") as file:
    result = file.read()

print(data == result)
True

In Changelog of python 3.12.0

bpo-23224: Fix segfaults when creating lzma.LZMADecompressor and bz2.BZ2Decompressor objects without calling init(), and fix leakage of locks and internal buffers when calling the init() methods of lzma.LZMADecompressor, lzma.LZMACompressor, bz2.BZ2Compressor, and bz2.BZ2Decompressor objects multiple times.

bpo-33916: bz2 and lzma: When Decompressor.init() is called twice, free the old lock to not leak memory.

6个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!