Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Lib/test/test_lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class CompressorDecompressorTestCase(unittest.TestCase):

# Test error cases.

@unittest.expectedFailure # TODO: RUSTPYTHON; lzma.LZMAError: Invalid format
def test_simple_bad_args(self):
self.assertRaises(TypeError, LZMACompressor, [])
self.assertRaises(TypeError, LZMACompressor, format=3.45)
Expand Down Expand Up @@ -631,7 +630,6 @@ def test_init_bad_mode(self):
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), "rw")

@unittest.expectedFailure # TODO: RUSTPYTHON; TypeError: Invalid check value
def test_init_bad_check(self):
with self.assertRaises(TypeError):
LZMAFile(BytesIO(), "w", check=b"asd")
Expand Down Expand Up @@ -1521,7 +1519,6 @@ def test_is_check_supported(self):
# This value should not be a valid check ID.
self.assertFalse(lzma.is_check_supported(lzma.CHECK_UNKNOWN))

@unittest.expectedFailure # TODO: RUSTPYTHON; TypeError: expected at most 0 arguments, got 1
def test__encode_filter_properties(self):
with self.assertRaises(TypeError):
lzma._encode_filter_properties(b"not a dict")
Expand Down
19 changes: 11 additions & 8 deletions crates/common/src/compression/lzma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use super::{CHUNKSIZE, Chunker};

pub const BUFSIZ: usize = 8192;
const DEF_BUF_SIZE: usize = 16 * 1024;
const USE_AFTER_FINISH_ERR: &str = "Error -2: inconsistent stream state";
const LZMA_FILTERS_MAX: usize = 4;

pub const CHECK_NONE: i32 = xz_sys::LZMA_CHECK_NONE as _;
Expand Down Expand Up @@ -267,9 +266,7 @@ pub fn encode_filter_properties(spec: &FilterSpec) -> Result<Vec<u8>, Error> {
FILTER_DELTA => {
let dist = spec.dist.unwrap_or(1);
if !(1..=256).contains(&dist) {
return Err(Error::Value(
"Invalid filter specifier for delta filter".to_owned(),
));
return Err(Error::Lzma("Internal error".to_owned()));
}
Ok(vec![(dist - 1) as u8])
}
Expand Down Expand Up @@ -547,8 +544,14 @@ impl Compressor {
}
let stream = match format {
FORMAT_XZ => {
let check = int_to_check(check)
.ok_or_else(|| Error::Value("Invalid check value".to_owned()))?;
let check = int_to_check(check).ok_or_else(|| {
let message = if (0..=CHECK_ID_MAX).contains(&check) {
"Unsupported integrity check"
} else {
"Internal error"
};
Error::Lzma(message.to_owned())
})?;
if let Some(specs) = filters {
Stream::new_stream_encoder(&build_filters(&specs)?, check)?
} else {
Expand Down Expand Up @@ -589,7 +592,7 @@ impl Compressor {
let stream = self
.stream
.as_mut()
.ok_or_else(|| Error::Lzma(USE_AFTER_FINISH_ERR.to_owned()))?;
.ok_or_else(|| Error::Value("Compressor has been flushed".to_owned()))?;
let mut output = Vec::new();
for mut chunk in data.chunks(CHUNKSIZE) {
while !chunk.is_empty() {
Expand All @@ -608,7 +611,7 @@ impl Compressor {
let stream = self
.stream
.as_mut()
.ok_or_else(|| Error::Lzma(USE_AFTER_FINISH_ERR.to_owned()))?;
.ok_or_else(|| Error::Value("Repeated call to flush()".to_owned()))?;
let mut output = Vec::new();
let status = loop {
if output.len() == output.capacity() {
Expand Down
Loading