Skip to content

Commit 2efc566

Browse files
authored
Merge pull request #4693 from youknowone/zlib-level
Refactor zlib using new `Level` type
2 parents 134d9f1 + 089c8b7 commit 2efc566

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"bindgen",
3131
"cstring",
3232
"chrono",
33+
"flate2",
3334
"insta",
3435
"peekable",
3536
"lalrpop",

stdlib/src/zlib.rs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ mod zlib {
55
use crate::vm::{
66
builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyIntRef, PyTypeRef},
77
common::lock::PyMutex,
8+
convert::TryFromBorrowedObject,
89
function::{ArgBytesLike, ArgPrimitiveIndex, ArgSize, OptionalArg},
9-
PyPayload, PyResult, VirtualMachine,
10+
PyObject, PyPayload, PyResult, VirtualMachine,
1011
};
1112
use adler32::RollingAdler32 as Adler32;
1213
use crossbeam_utils::atomic::AtomicCell;
@@ -78,23 +79,12 @@ mod zlib {
7879
crate::binascii::crc32(data, begin_state)
7980
}
8081

81-
// TODO: rewrite with TryFromBorrowedObject
82-
fn compression_from_int(level: i32) -> Option<Compression> {
83-
match level {
84-
Z_DEFAULT_COMPRESSION => Some(Compression::default()),
85-
valid_level @ Z_NO_COMPRESSION..=Z_BEST_COMPRESSION => {
86-
Some(Compression::new(valid_level as u32))
87-
}
88-
_ => None,
89-
}
90-
}
91-
9282
#[derive(FromArgs)]
9383
struct PyFuncCompressArgs {
9484
#[pyarg(positional)]
9585
data: ArgBytesLike,
96-
#[pyarg(any, default = "Z_DEFAULT_COMPRESSION")]
97-
level: i32,
86+
#[pyarg(any, default = "Level::new(Z_DEFAULT_COMPRESSION)")]
87+
level: Level,
9888
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
9989
wbits: ArgPrimitiveIndex<i8>,
10090
}
@@ -107,9 +97,7 @@ mod zlib {
10797
level,
10898
ref wbits,
10999
} = args;
110-
111-
let level = compression_from_int(level)
112-
.ok_or_else(|| new_zlib_error("Bad compression level", vm))?;
100+
let level = level.ok_or_else(|| new_zlib_error("Bad compression level", vm))?;
113101

114102
let encoded_bytes = if args.wbits.value == MAX_WBITS {
115103
let mut encoder = ZlibEncoder::new(Vec::new(), level);
@@ -431,8 +419,8 @@ mod zlib {
431419
#[derive(FromArgs)]
432420
#[allow(dead_code)] // FIXME: use args
433421
struct CompressobjArgs {
434-
#[pyarg(any, default = "Z_DEFAULT_COMPRESSION")]
435-
level: i32,
422+
#[pyarg(any, default = "Level::new(Z_DEFAULT_COMPRESSION)")]
423+
level: Level,
436424
// only DEFLATED is valid right now, it's w/e
437425
#[pyarg(any, default = "DEFLATED")]
438426
_method: i32,
@@ -457,8 +445,8 @@ mod zlib {
457445
zdict,
458446
..
459447
} = args;
460-
let level = compression_from_int(level)
461-
.ok_or_else(|| vm.new_value_error("invalid initialization option".to_owned()))?;
448+
let level =
449+
level.ok_or_else(|| vm.new_value_error("invalid initialization option".to_owned()))?;
462450
#[allow(unused_mut)]
463451
let mut compress = InitOptions::new(wbits.value, vm)?.compress(level);
464452
#[cfg(feature = "zlib")]
@@ -574,4 +562,32 @@ mod zlib {
574562
fn new_zlib_error(message: &str, vm: &VirtualMachine) -> PyBaseExceptionRef {
575563
vm.new_exception_msg(vm.class("zlib", "error"), message.to_owned())
576564
}
565+
566+
struct Level(Option<flate2::Compression>);
567+
568+
impl Level {
569+
fn new(level: i32) -> Self {
570+
let compression = match level {
571+
Z_DEFAULT_COMPRESSION => Compression::default(),
572+
valid_level @ Z_NO_COMPRESSION..=Z_BEST_COMPRESSION => {
573+
Compression::new(valid_level as u32)
574+
}
575+
_ => return Self(None),
576+
};
577+
Self(Some(compression))
578+
}
579+
fn ok_or_else(
580+
self,
581+
f: impl FnOnce() -> PyBaseExceptionRef,
582+
) -> PyResult<flate2::Compression> {
583+
self.0.ok_or_else(f)
584+
}
585+
}
586+
587+
impl TryFromBorrowedObject for Level {
588+
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Self> {
589+
let int: i32 = obj.try_index(vm)?.try_to_primitive(vm)?;
590+
Ok(Self::new(int))
591+
}
592+
}
577593
}

0 commit comments

Comments
 (0)