Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit df380bc

Browse filesBrowse files
authored
lzma FORMAT_ALONE implementation (#5777)
* implement init_alone * error if format is raw and there is a memlimit
1 parent 9bd7f18 commit df380bc
Copy full SHA for df380bc

File tree

1 file changed

+27
-2
lines changed
Filter options

1 file changed

+27
-2
lines changed

‎stdlib/src/lzma.rs

Copy file name to clipboardExpand all lines: stdlib/src/lzma.rs
+27-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod _lzma {
3939
use rustpython_vm::types::Constructor;
4040
use rustpython_vm::{PyObjectRef, PyPayload, PyResult, VirtualMachine};
4141
use std::fmt;
42-
use xz2::stream::{Action, Check, Error, Filters, Status, Stream};
42+
use xz2::stream::{Action, Check, Error, Filters, LzmaOptions, Status, Stream};
4343

4444
#[cfg(windows)]
4545
type EnumVal = i32;
@@ -149,6 +149,11 @@ mod _lzma {
149149
type Args = LZMADecompressorConstructorArgs;
150150

151151
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
152+
if args.format == FORMAT_RAW && args.memlimit.is_some() {
153+
return Err(
154+
vm.new_value_error("Cannot specify memory limit with FORMAT_RAW".to_string())
155+
);
156+
}
152157
let memlimit = args.memlimit.unwrap_or(u64::MAX);
153158
let filters = args.filters.unwrap_or(0);
154159
let stream_result = match args.format {
@@ -322,6 +327,25 @@ mod _lzma {
322327
.map_err(|_| new_lzma_error("Failed to initialize encoder", vm))?)
323328
}
324329
}
330+
331+
fn init_alone(
332+
preset: u32,
333+
filter_specs: Option<Vec<PyObjectRef>>,
334+
vm: &VirtualMachine,
335+
) -> PyResult<Stream> {
336+
if let Some(_filter_specs) = filter_specs {
337+
Err(new_lzma_error(
338+
"TODO: RUSTPYTHON: LZMA: Alone filter specs",
339+
vm,
340+
))
341+
} else {
342+
let options = LzmaOptions::new_preset(preset)
343+
.map_err(|_| new_lzma_error("Failed to initialize encoder", vm))?;
344+
let stream = Stream::new_lzma_encoder(&options)
345+
.map_err(|_| new_lzma_error("Failed to initialize encoder", vm))?;
346+
Ok(stream)
347+
}
348+
}
325349
}
326350

327351
#[derive(FromArgs)]
@@ -365,7 +389,8 @@ mod _lzma {
365389
}
366390
let stream = match args.format {
367391
FORMAT_XZ => Self::init_xz(args.check, preset, args.filters, vm)?,
368-
// TODO: ALONE AND RAW
392+
FORMAT_ALONE => Self::init_alone(preset, args.filters, vm)?,
393+
// TODO: RAW
369394
_ => return Err(new_lzma_error("Invalid format", vm)),
370395
};
371396
Ok(Self {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.