Skip to content

lzma FORMAT_ALONE implementation #5777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2025
Merged
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
29 changes: 27 additions & 2 deletions stdlib/src/lzma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod _lzma {
use rustpython_vm::types::Constructor;
use rustpython_vm::{PyObjectRef, PyPayload, PyResult, VirtualMachine};
use std::fmt;
use xz2::stream::{Action, Check, Error, Filters, Status, Stream};
use xz2::stream::{Action, Check, Error, Filters, LzmaOptions, Status, Stream};

#[cfg(windows)]
type EnumVal = i32;
Expand Down Expand Up @@ -149,6 +149,11 @@ mod _lzma {
type Args = LZMADecompressorConstructorArgs;

fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
if args.format == FORMAT_RAW && args.memlimit.is_some() {
return Err(
vm.new_value_error("Cannot specify memory limit with FORMAT_RAW".to_string())
);
}
let memlimit = args.memlimit.unwrap_or(u64::MAX);
let filters = args.filters.unwrap_or(0);
let stream_result = match args.format {
Expand Down Expand Up @@ -322,6 +327,25 @@ mod _lzma {
.map_err(|_| new_lzma_error("Failed to initialize encoder", vm))?)
}
}

fn init_alone(
preset: u32,
filter_specs: Option<Vec<PyObjectRef>>,
vm: &VirtualMachine,
) -> PyResult<Stream> {
if let Some(_filter_specs) = filter_specs {
Err(new_lzma_error(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike other actual errors, probably NotImplementedError fits better. Not very important though.

"TODO: RUSTPYTHON: LZMA: Alone filter specs",
vm,
))
} else {
let options = LzmaOptions::new_preset(preset)
.map_err(|_| new_lzma_error("Failed to initialize encoder", vm))?;
let stream = Stream::new_lzma_encoder(&options)
.map_err(|_| new_lzma_error("Failed to initialize encoder", vm))?;
Ok(stream)
}
}
}

#[derive(FromArgs)]
Expand Down Expand Up @@ -365,7 +389,8 @@ mod _lzma {
}
let stream = match args.format {
FORMAT_XZ => Self::init_xz(args.check, preset, args.filters, vm)?,
// TODO: ALONE AND RAW
FORMAT_ALONE => Self::init_alone(preset, args.filters, vm)?,
// TODO: RAW
_ => return Err(new_lzma_error("Invalid format", vm)),
};
Ok(Self {
Expand Down
Loading