@@ -2,10 +2,10 @@ pub(crate) use zlib::make_module;
22
33#[ pymodule]
44mod zlib {
5- use crate :: common:: lock:: PyMutex ;
65 use crate :: vm:: {
76 builtins:: { PyBaseExceptionRef , PyBytes , PyBytesRef , PyIntRef , PyTypeRef } ,
8- function:: { ArgBytesLike , OptionalArg , OptionalOption } ,
7+ common:: lock:: PyMutex ,
8+ function:: { ArgBytesLike , ArgPrimitiveIndex , ArgSize , OptionalArg , OptionalOption } ,
99 PyPayload , PyResult , VirtualMachine ,
1010 } ;
1111 use adler32:: RollingAdler32 as Adler32 ;
@@ -233,9 +233,9 @@ mod zlib {
233233 #[ pyarg( positional) ]
234234 data : ArgBytesLike ,
235235 #[ pyarg( any, optional) ]
236- wbits : OptionalArg < i8 > ,
236+ wbits : OptionalArg < ArgPrimitiveIndex < i8 > > ,
237237 #[ pyarg( any, optional) ]
238- bufsize : OptionalArg < usize > ,
238+ bufsize : OptionalArg < ArgPrimitiveIndex < usize > > ,
239239 }
240240
241241 /// Returns a bytes object containing the uncompressed data.
@@ -245,9 +245,9 @@ mod zlib {
245245 let wbits = arg. wbits ;
246246 let bufsize = arg. bufsize ;
247247 data. with_ref ( |data| {
248- let bufsize = bufsize. unwrap_or ( DEF_BUF_SIZE ) ;
248+ let bufsize = bufsize. into_primitive ( ) . unwrap_or ( DEF_BUF_SIZE ) ;
249249
250- let mut d = header_from_wbits ( wbits, vm) ?. decompress ( ) ;
250+ let mut d = header_from_wbits ( wbits. into_primitive ( ) , vm) ?. decompress ( ) ;
251251
252252 _decompress ( data, & mut d, bufsize, None , false , vm) . and_then ( |( buf, stream_end) | {
253253 if stream_end {
@@ -265,7 +265,7 @@ mod zlib {
265265 #[ pyfunction]
266266 fn decompressobj ( args : DecompressobjArgs , vm : & VirtualMachine ) -> PyResult < PyDecompress > {
267267 #[ allow( unused_mut) ]
268- let mut decompress = header_from_wbits ( args. wbits , vm) ?. decompress ( ) ;
268+ let mut decompress = header_from_wbits ( args. wbits . into_primitive ( ) , vm) ?. decompress ( ) ;
269269 #[ cfg( feature = "zlib" ) ]
270270 if let OptionalArg :: Present ( dict) = args. zdict {
271271 dict. with_ref ( |d| decompress. set_dictionary ( d) . unwrap ( ) ) ;
@@ -325,11 +325,8 @@ mod zlib {
325325
326326 #[ pymethod]
327327 fn decompress ( & self , args : DecompressArgs , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
328- let max_length = if args. max_length == 0 {
329- None
330- } else {
331- Some ( args. max_length )
332- } ;
328+ let max_length = args. max_length . value ;
329+ let max_length = ( max_length != 0 ) . then_some ( max_length) ;
333330 let data = args. data . borrow_buf ( ) ;
334331 let data = & * data;
335332
@@ -362,12 +359,18 @@ mod zlib {
362359 }
363360
364361 #[ pymethod]
365- fn flush ( & self , length : OptionalArg < isize > , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
362+ fn flush ( & self , length : OptionalArg < ArgSize > , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
366363 let length = match length {
367- OptionalArg :: Present ( l) if l <= 0 => {
368- return Err ( vm. new_value_error ( "length must be greater than zero" . to_owned ( ) ) ) ;
364+ OptionalArg :: Present ( l) => {
365+ let l: isize = l. into ( ) ;
366+ if l <= 0 {
367+ return Err (
368+ vm. new_value_error ( "length must be greater than zero" . to_owned ( ) )
369+ ) ;
370+ } else {
371+ l as usize
372+ }
369373 }
370- OptionalArg :: Present ( l) => l as usize ,
371374 OptionalArg :: Missing => DEF_BUF_SIZE ,
372375 } ;
373376
@@ -396,14 +399,17 @@ mod zlib {
396399 struct DecompressArgs {
397400 #[ pyarg( positional) ]
398401 data : ArgBytesLike ,
399- #[ pyarg( any, default = "0" ) ]
400- max_length : usize ,
402+ #[ pyarg(
403+ any,
404+ default = "rustpython_vm::function::ArgPrimitiveIndex { value: 0 }"
405+ ) ]
406+ max_length : ArgPrimitiveIndex < usize > ,
401407 }
402408
403409 #[ derive( FromArgs ) ]
404410 struct DecompressobjArgs {
405411 #[ pyarg( any, optional) ]
406- wbits : OptionalArg < i8 > ,
412+ wbits : OptionalArg < ArgPrimitiveIndex < i8 > > ,
407413 #[ cfg( feature = "zlib" ) ]
408414 #[ pyarg( any, optional) ]
409415 zdict : OptionalArg < ArgBytesLike > ,
@@ -414,7 +420,7 @@ mod zlib {
414420 level : OptionalArg < i32 > ,
415421 // only DEFLATED is valid right now, it's w/e
416422 _method : OptionalArg < i32 > ,
417- wbits : OptionalArg < i8 > ,
423+ wbits : OptionalArg < ArgPrimitiveIndex < i8 > > ,
418424 // these aren't used.
419425 _mem_level : OptionalArg < i32 > , // this is memLevel in CPython
420426 _strategy : OptionalArg < i32 > ,
@@ -423,7 +429,7 @@ mod zlib {
423429 ) -> PyResult < PyCompress > {
424430 let level = compression_from_int ( level. into_option ( ) )
425431 . ok_or_else ( || vm. new_value_error ( "invalid initialization option" . to_owned ( ) ) ) ?;
426- let compress = header_from_wbits ( wbits, vm) ?. compress ( level) ;
432+ let compress = header_from_wbits ( wbits. into_primitive ( ) , vm) ?. compress ( level) ;
427433 Ok ( PyCompress {
428434 inner : PyMutex :: new ( CompressInner {
429435 compress,
0 commit comments