Skip to content

Commit f69087f

Browse files
committed
Switch to libz-rs-sys for zlib implementation
The `zlib` feature now enables the entire `zlib` module
1 parent 235adaf commit f69087f

File tree

4 files changed

+30
-63
lines changed

4 files changed

+30
-63
lines changed

Cargo.lock

+14-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stdlib/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ license.workspace = true
1414
default = ["compiler"]
1515
compiler = ["rustpython-vm/compiler"]
1616
threading = ["rustpython-common/threading", "rustpython-vm/threading"]
17-
zlib = ["libz-sys", "flate2/zlib"]
17+
zlib = ["dep:flate2", "dep:libz-sys"]
1818
bz2 = ["bzip2"]
1919
sqlite = ["dep:libsqlite3-sys"]
2020
ssl = ["openssl", "openssl-sys", "foreign-types-shared", "openssl-probe"]
@@ -48,7 +48,6 @@ memchr = { workspace = true }
4848
base64 = "0.13.0"
4949
csv-core = "0.1.11"
5050
dyn-clone = "1.0.10"
51-
libz-sys = { version = "1.1", default-features = false, optional = true }
5251
puruspe = "0.4.0"
5352
xml-rs = "0.8.14"
5453

@@ -81,7 +80,8 @@ ucd = "0.1.1"
8180
# compression
8281
adler32 = "1.2.0"
8382
crc32fast = "1.3.2"
84-
flate2 = "1.0.28"
83+
flate2 = { version = "1.1", optional = true, default-features = false, features = ["zlib-rs"] }
84+
libz-sys = { package = "libz-rs-sys", optional = true, version = "0.4", default-features = false }
8585
bzip2 = { version = "0.4", optional = true }
8686

8787
# uuid

stdlib/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub mod socket;
4242
#[cfg(all(unix, not(target_os = "redox")))]
4343
mod syslog;
4444
mod unicodedata;
45+
#[cfg(feature = "zlib")]
4546
mod zlib;
4647

4748
mod faulthandler;
@@ -128,10 +129,13 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
128129
"_statistics" => statistics::make_module,
129130
"_struct" => pystruct::make_module,
130131
"unicodedata" => unicodedata::make_module,
131-
"zlib" => zlib::make_module,
132132
"_statistics" => statistics::make_module,
133133
// crate::vm::sysmodule::sysconfigdata_name() => sysconfigdata::make_module,
134134
}
135+
#[cfg(feature = "zlib")]
136+
{
137+
"zlib" => zlib::make_module,
138+
}
135139
#[cfg(any(unix, target_os = "wasi"))]
136140
{
137141
"fcntl" => fcntl::make_module,

stdlib/src/zlib.rs

+8-49
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod zlib {
99
common::lock::PyMutex,
1010
convert::TryFromBorrowedObject,
1111
function::{ArgBytesLike, ArgPrimitiveIndex, ArgSize, OptionalArg},
12+
types::Constructor,
1213
PyObject, PyPayload, PyResult, VirtualMachine,
1314
};
1415
use adler32::RollingAdler32 as Adler32;
@@ -19,35 +20,12 @@ mod zlib {
1920
};
2021
use std::io::Write;
2122

22-
#[cfg(not(feature = "zlib"))]
23-
mod constants {
24-
pub const Z_NO_COMPRESSION: i32 = 0;
25-
pub const Z_BEST_COMPRESSION: i32 = 9;
26-
pub const Z_BEST_SPEED: i32 = 1;
27-
pub const Z_DEFAULT_COMPRESSION: i32 = -1;
28-
pub const Z_NO_FLUSH: i32 = 0;
29-
pub const Z_PARTIAL_FLUSH: i32 = 1;
30-
pub const Z_SYNC_FLUSH: i32 = 2;
31-
pub const Z_FULL_FLUSH: i32 = 3;
32-
// not sure what the value here means, but it's the only compression method zlibmodule
33-
// supports, so it doesn't really matter
34-
pub const Z_DEFLATED: i32 = 8;
35-
}
36-
#[cfg(feature = "zlib")]
37-
use libz_sys as constants;
38-
39-
#[pyattr]
40-
use constants::{
41-
Z_BEST_COMPRESSION, Z_BEST_SPEED, Z_DEFAULT_COMPRESSION, Z_DEFLATED as DEFLATED,
42-
Z_FULL_FLUSH, Z_NO_COMPRESSION, Z_NO_FLUSH, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH,
43-
};
44-
45-
#[cfg(feature = "zlib")]
4623
#[pyattr]
4724
use libz_sys::{
48-
Z_BLOCK, Z_DEFAULT_STRATEGY, Z_FILTERED, Z_FINISH, Z_FIXED, Z_HUFFMAN_ONLY, Z_RLE, Z_TREES,
25+
Z_BEST_COMPRESSION, Z_BEST_SPEED, Z_BLOCK, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY,
26+
Z_DEFLATED as DEFLATED, Z_FILTERED, Z_FINISH, Z_FIXED, Z_FULL_FLUSH, Z_HUFFMAN_ONLY,
27+
Z_NO_COMPRESSION, Z_NO_FLUSH, Z_PARTIAL_FLUSH, Z_RLE, Z_SYNC_FLUSH, Z_TREES,
4928
};
50-
use rustpython_vm::types::Constructor;
5129

5230
// copied from zlibmodule.c (commit 530f506ac91338)
5331
#[pyattr]
@@ -119,48 +97,35 @@ mod zlib {
11997
header: bool,
12098
// [De]Compress::new_with_window_bits is only enabled for zlib; miniz_oxide doesn't
12199
// support wbits (yet?)
122-
#[cfg(feature = "zlib")]
123100
wbits: u8,
124101
},
125-
#[cfg(feature = "zlib")]
126-
Gzip { wbits: u8 },
102+
Gzip {
103+
wbits: u8,
104+
},
127105
}
128106

129107
impl InitOptions {
130108
fn new(wbits: i8, vm: &VirtualMachine) -> PyResult<InitOptions> {
131109
let header = wbits > 0;
132110
let wbits = wbits.unsigned_abs();
133111
match wbits {
134-
9..=15 => Ok(InitOptions::Standard {
135-
header,
136-
#[cfg(feature = "zlib")]
137-
wbits,
138-
}),
139-
#[cfg(feature = "zlib")]
112+
9..=15 => Ok(InitOptions::Standard { header, wbits }),
140113
25..=31 => Ok(InitOptions::Gzip { wbits: wbits - 16 }),
141114
_ => Err(vm.new_value_error("Invalid initialization option".to_owned())),
142115
}
143116
}
144117

145118
fn decompress(self) -> Decompress {
146119
match self {
147-
#[cfg(not(feature = "zlib"))]
148-
Self::Standard { header } => Decompress::new(header),
149-
#[cfg(feature = "zlib")]
150120
Self::Standard { header, wbits } => Decompress::new_with_window_bits(header, wbits),
151-
#[cfg(feature = "zlib")]
152121
Self::Gzip { wbits } => Decompress::new_gzip(wbits),
153122
}
154123
}
155124
fn compress(self, level: Compression) -> Compress {
156125
match self {
157-
#[cfg(not(feature = "zlib"))]
158-
Self::Standard { header } => Compress::new(level, header),
159-
#[cfg(feature = "zlib")]
160126
Self::Standard { header, wbits } => {
161127
Compress::new_with_window_bits(level, header, wbits)
162128
}
163-
#[cfg(feature = "zlib")]
164129
Self::Gzip { wbits } => Compress::new_gzip(level, wbits),
165130
}
166131
}
@@ -264,7 +229,6 @@ mod zlib {
264229
struct DecompressobjArgs {
265230
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
266231
wbits: ArgPrimitiveIndex<i8>,
267-
#[cfg(feature = "zlib")]
268232
#[pyarg(any, optional)]
269233
_zdict: OptionalArg<ArgBytesLike>,
270234
}
@@ -273,7 +237,6 @@ mod zlib {
273237
fn decompressobj(args: DecompressobjArgs, vm: &VirtualMachine) -> PyResult<PyDecompress> {
274238
#[allow(unused_mut)]
275239
let mut decompress = InitOptions::new(args.wbits.value, vm)?.decompress();
276-
#[cfg(feature = "zlib")]
277240
if let OptionalArg::Present(_dict) = args._zdict {
278241
// FIXME: always fails
279242
// dict.with_ref(|d| decompress.set_dictionary(d));
@@ -426,10 +389,8 @@ mod zlib {
426389
wbits: ArgPrimitiveIndex<i8>,
427390
#[pyarg(any, name = "_memLevel", default = "DEF_MEM_LEVEL")]
428391
_mem_level: u8,
429-
#[cfg(feature = "zlib")]
430392
#[pyarg(any, default = "Z_DEFAULT_STRATEGY")]
431393
_strategy: i32,
432-
#[cfg(feature = "zlib")]
433394
#[pyarg(any, optional)]
434395
zdict: Option<ArgBytesLike>,
435396
}
@@ -439,15 +400,13 @@ mod zlib {
439400
let CompressobjArgs {
440401
level,
441402
wbits,
442-
#[cfg(feature = "zlib")]
443403
zdict,
444404
..
445405
} = args;
446406
let level =
447407
level.ok_or_else(|| vm.new_value_error("invalid initialization option".to_owned()))?;
448408
#[allow(unused_mut)]
449409
let mut compress = InitOptions::new(wbits.value, vm)?.compress(level);
450-
#[cfg(feature = "zlib")]
451410
if let Some(zdict) = zdict {
452411
zdict.with_ref(|zdict| compress.set_dictionary(zdict).unwrap());
453412
}

0 commit comments

Comments
 (0)