From 098ba20cd29b708f8b4432ed961d755c5b1c8544 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 15:54:25 +0300 Subject: [PATCH 01/66] Add `unnecessary_wraps` clippy rule --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 18f4c1b21c..3a32af3849 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -358,3 +358,4 @@ inconsistent_struct_constructor = "warn" manual_is_variant_and = "warn" map_unwrap_or = "warn" must_use_candidate = "warn" +unnecessary_wraps = "warn" From 0cf2f027e95c4a8b5ec49f15c30f0fe0e092e71a Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 15:49:44 +0300 Subject: [PATCH 02/66] fix ssl/cert.rs --- crates/stdlib/src/ssl/cert.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/stdlib/src/ssl/cert.rs b/crates/stdlib/src/ssl/cert.rs index f42f5ebf18..835e3f37c6 100644 --- a/crates/stdlib/src/ssl/cert.rs +++ b/crates/stdlib/src/ssl/cert.rs @@ -292,8 +292,8 @@ pub(super) fn is_ca_certificate(cert_der: &[u8]) -> bool { /// Convert an X509Name to Python nested tuple format for SSL certificate dicts /// /// Format: ((('CN', 'example.com'),), (('O', 'Example Org'),), ...) -fn name_to_py(vm: &VirtualMachine, name: &x509_parser::x509::X509Name<'_>) -> PyResult { - let list: Vec = name +fn name_to_py(vm: &VirtualMachine, name: &x509_parser::x509::X509Name<'_>) -> PyObjectRef { + let list = name .iter() .flat_map(|rdn| { // Each RDN can have multiple attributes @@ -308,9 +308,9 @@ fn name_to_py(vm: &VirtualMachine, name: &x509_parser::x509::X509Name<'_>) -> Py }) .collect::>() }) - .collect(); + .collect::>(); - Ok(vm.ctx.new_tuple(list).into()) + vm.ctx.new_tuple(list).into() } /// Convert DER-encoded certificate to Python dict (for getpeercert with binary_form=False) @@ -324,8 +324,8 @@ pub(super) fn cert_to_dict( let dict = vm.ctx.new_dict(); // Subject and Issuer - dict.set_item("subject", name_to_py(vm, cert.subject())?, vm)?; - dict.set_item("issuer", name_to_py(vm, cert.issuer())?, vm)?; + dict.set_item("subject", name_to_py(vm, cert.subject()), vm)?; + dict.set_item("issuer", name_to_py(vm, cert.issuer()), vm)?; // Version (X.509 v3 = version 2 in the cert, but Python uses 3) dict.set_item( From e076773a6d8c5eb105d631cb1f9ce113d4ef4a92 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 15:49:56 +0300 Subject: [PATCH 03/66] fix ssl.rs --- crates/stdlib/src/ssl.rs | 66 +++++++++++++++------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/crates/stdlib/src/ssl.rs b/crates/stdlib/src/ssl.rs index b523c29a65..b05bcea3c9 100644 --- a/crates/stdlib/src/ssl.rs +++ b/crates/stdlib/src/ssl.rs @@ -1517,7 +1517,7 @@ mod _ssl { } #[pymethod] - fn get_ciphers(&self, vm: &VirtualMachine) -> PyResult { + fn get_ciphers(&self, vm: &VirtualMachine) -> PyListRef { // Dynamically generate cipher list from rustls ALL_CIPHER_SUITES // This automatically includes all cipher suites supported by the current rustls version @@ -1575,7 +1575,7 @@ mod _ssl { }) .collect::>(); - Ok(PyListRef::from(vm.ctx.new_list(cipher_list))) + PyListRef::from(vm.ctx.new_list(cipher_list)) } #[pymethod] @@ -2427,10 +2427,10 @@ mod _ssl { } // Create and store a session object after successful handshake - fn create_session_after_handshake(&self, vm: &VirtualMachine) -> PyResult<()> { + fn create_session_after_handshake(&self, vm: &VirtualMachine) { // Only create session for client-side connections if self.server_side { - return Ok(()); + return; } // Check if session already exists @@ -2438,7 +2438,7 @@ mod _ssl { if let Some(ref s) = session_opt { if vm.is_none(s) { } else { - return Ok(()); + return; } } @@ -2489,8 +2489,6 @@ mod _ssl { let py_session = session.into_pyobject(vm); *self.session.write() = Some(py_session); - - Ok(()) } // Complete handshake and create session @@ -2550,7 +2548,7 @@ mod _ssl { Ok(()) } - fn complete_handshake(&self, vm: &VirtualMachine) -> PyResult<()> { + fn complete_handshake(&self, vm: &VirtualMachine) { *self.handshake_done.lock() = true; // Check if session was resumed - get value and release lock immediately @@ -2580,8 +2578,7 @@ mod _ssl { let _ = self.track_used_ca_from_capath(); } - self.create_session_after_handshake(vm)?; - Ok(()) + self.create_session_after_handshake(vm); } // Internal implementation with timeout control @@ -3435,7 +3432,7 @@ mod _ssl { if is_client { // CLIENT is simple - no SNI callback handling needed handshake_result.map_err(|e| e.into_py_err(vm))?; - self.complete_handshake(vm)?; + self.complete_handshake(vm); Ok(()) } else { // Use OpenSSL-compatible handshake for server @@ -3443,7 +3440,7 @@ mod _ssl { match handshake_result { Ok(()) => { // Handshake completed successfully - self.complete_handshake(vm)?; + self.complete_handshake(vm); Ok(()) } Err(SslError::SniCallbackRestart) => { @@ -3662,13 +3659,13 @@ mod _ssl { } #[pymethod] - fn pending(&self) -> PyResult { + fn pending(&self) -> usize { // Returns the number of already decrypted bytes available for read // This is critical for asyncore's readable() method which checks socket.pending() > 0 let mut conn_guard = self.connection.lock(); let conn = match conn_guard.as_mut() { Some(c) => c, - None => return Ok(0), // No connection established yet + None => return 0, // No connection established yet }; // Use rustls Reader's fill_buf() to check buffered plaintext @@ -3676,11 +3673,11 @@ mod _ssl { // This matches OpenSSL's SSL_pending() behavior let mut reader = conn.reader(); match reader.fill_buf() { - Ok(buf) => Ok(buf.len()), + Ok(buf) => buf.len(), Err(_) => { // WouldBlock or other errors mean no data available // Return 0 like OpenSSL does when buffer is empty - Ok(0) + 0 } } } @@ -3857,9 +3854,8 @@ mod _ssl { } #[pygetset(setter)] - fn set_owner(&self, owner: PyObjectRef, _vm: &VirtualMachine) -> PyResult<()> { + fn set_owner(&self, owner: PyObjectRef, _vm: &VirtualMachine) { *self.owner.write() = Some(owner); - Ok(()) } #[pygetset] @@ -3873,7 +3869,7 @@ mod _ssl { } #[pygetset(setter)] - fn set_context(&self, value: PyRef, _vm: &VirtualMachine) -> PyResult<()> { + fn set_context(&self, value: PyRef, _vm: &VirtualMachine) { // Update context reference immediately // SSL_set_SSL_CTX allows context changes at any time, // even after handshake completion @@ -3881,8 +3877,6 @@ mod _ssl { // Clear pending context as we've applied the change *self.pending_context.write() = None; - - Ok(()) } #[pygetset] @@ -3916,14 +3910,10 @@ mod _ssl { } #[pygetset] - fn session(&self, vm: &VirtualMachine) -> PyResult { + fn session(&self, vm: &VirtualMachine) -> PyObjectRef { // Return the stored session object if any let sess = self.session.read().clone(); - if let Some(s) = sess { - Ok(s) - } else { - Ok(vm.ctx.none()) - } + sess.unwrap_or_else(|| vm.ctx.none()) } #[pygetset(setter)] @@ -3999,18 +3989,12 @@ mod _ssl { } #[pymethod] - fn get_verified_chain(&self, vm: &VirtualMachine) -> PyResult> { + fn get_verified_chain(&self, vm: &VirtualMachine) -> Option { // Get peer certificates (what peer sent during handshake) let conn_guard = self.connection.lock(); - let Some(ref conn) = *conn_guard else { - return Ok(None); - }; - + let conn = (*conn_guard).as_ref()?; let peer_certs = conn.peer_certificates(); - - let Some(peer_certs_slice) = peer_certs else { - return Ok(None); - }; + let peer_certs_slice = peer_certs?; // Build the verified chain using cert module let ctx_guard = self.context.read(); @@ -4024,7 +4008,7 @@ mod _ssl { .map(|der_bytes| PySSLCertificate { der_bytes }.into_ref(&vm.ctx).into()) .collect(); - Ok(Some(vm.ctx.new_list(cert_list))) + Some(vm.ctx.new_list(cert_list)) } #[pymethod] @@ -4655,9 +4639,8 @@ mod _ssl { } #[pymethod] - fn write_eof(&self, _vm: &VirtualMachine) -> PyResult<()> { + fn write_eof(&self, _vm: &VirtualMachine) { *self.eof.write() = true; - Ok(()) } #[pygetset] @@ -4816,7 +4799,7 @@ mod _ssl { } #[pyfunction] - fn get_default_verify_paths(vm: &VirtualMachine) -> PyResult { + fn get_default_verify_paths(vm: &VirtualMachine) -> PyObjectRef { // Return default certificate paths as a tuple // Lib/ssl.py expects: (openssl_cafile_env, openssl_cafile, openssl_capath_env, openssl_capath) // parts[0] = environment variable name for cafile @@ -4846,7 +4829,8 @@ mod _ssl { vm.ctx.new_str("SSL_CERT_DIR").into(), // openssl_capath_env default_capath.map_or_else(|| vm.ctx.none(), |s| vm.ctx.new_str(s).into()), // openssl_capath ]); - Ok(tuple.into()) + + tuple.into() } #[pyfunction] From 873a6542d82e9b24f6b92089c92fe83e64833941 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 15:55:06 +0300 Subject: [PATCH 04/66] select.rs --- crates/stdlib/src/select.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/stdlib/src/select.rs b/crates/stdlib/src/select.rs index 90cff5fb44..0110e07339 100644 --- a/crates/stdlib/src/select.rs +++ b/crates/stdlib/src/select.rs @@ -40,6 +40,7 @@ mod decl { stdlib::time, }; + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py) -> PyResult<()> { #[cfg(windows)] crate::vm::windows::init_winsock(); @@ -275,17 +276,12 @@ mod decl { #[pyclass] impl PyPoll { #[pymethod] - fn register( - &self, - Fildes(fd): Fildes, - eventmask: OptionalArg, - ) -> PyResult<()> { + fn register(&self, Fildes(fd): Fildes, eventmask: OptionalArg) { let mask = match eventmask { OptionalArg::Present(event_mask) => event_mask.0, OptionalArg::Missing => DEFAULT_EVENTS, }; insert_fd(&mut self.fds.lock(), fd, mask); - Ok(()) } #[pymethod] From 3b8c074c732317655deacaf91d8181ec8d176565 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 15:58:39 +0300 Subject: [PATCH 05/66] unicodedata --- crates/stdlib/src/unicodedata.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/stdlib/src/unicodedata.rs b/crates/stdlib/src/unicodedata.rs index 9a666ee910..97d09f88ff 100644 --- a/crates/stdlib/src/unicodedata.rs +++ b/crates/stdlib/src/unicodedata.rs @@ -191,9 +191,9 @@ mod unicodedata { } #[pymethod] - fn normalize(&self, form: super::NormalizeForm, unistr: PyStrRef) -> PyResult { + fn normalize(&self, form: super::NormalizeForm, unistr: PyStrRef) -> Wtf8Buf { let text = unistr.as_wtf8(); - let normalized_text = match form { + match form { Nfc => { let normalizer = ComposingNormalizerBorrowed::new_nfc(); text.map_utf8(|s| normalizer.normalize_iter(s.chars())) @@ -214,12 +214,11 @@ mod unicodedata { text.map_utf8(|s| normalizer.normalize_iter(s.chars())) .collect() } - }; - Ok(normalized_text) + } } #[pymethod] - fn is_normalized(&self, form: super::NormalizeForm, unistr: PyStrRef) -> PyResult { + fn is_normalized(&self, form: super::NormalizeForm, unistr: PyStrRef) -> bool { let text = unistr.as_wtf8(); let normalized: Wtf8Buf = match form { Nfc => { @@ -243,7 +242,7 @@ mod unicodedata { .collect() } }; - Ok(text == &*normalized) + text == &*normalized } #[pymethod] From 627e9cbd555080e18eb0b4100af95b9003c1fd00 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:00:43 +0300 Subject: [PATCH 06/66] socket.rs --- crates/stdlib/src/socket.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/stdlib/src/socket.rs b/crates/stdlib/src/socket.rs index b19ae7e0ef..dd6f84d1c5 100644 --- a/crates/stdlib/src/socket.rs +++ b/crates/stdlib/src/socket.rs @@ -24,6 +24,7 @@ mod _socket { }; use rustpython_host_env::os::ErrorExt; + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py) -> PyResult<()> { #[cfg(windows)] crate::vm::windows::init_winsock(); @@ -31,6 +32,7 @@ mod _socket { __module_exec(vm, module); Ok(()) } + use core::{ mem::MaybeUninit, net::{Ipv4Addr, Ipv6Addr, SocketAddr}, @@ -2067,7 +2069,7 @@ mod _socket { }; // Build ancdata list - let ancdata = Self::parse_ancillary_data(&msg, vm)?; + let ancdata = Self::parse_ancillary_data(&msg, vm); // Build address tuple let address = if msg.msg_namelen > 0 { @@ -2089,7 +2091,7 @@ mod _socket { /// Parse ancillary data from a received message header #[cfg(all(unix, not(target_os = "redox")))] - fn parse_ancillary_data(msg: &libc::msghdr, vm: &VirtualMachine) -> PyResult { + fn parse_ancillary_data(msg: &libc::msghdr, vm: &VirtualMachine) -> PyObjectRef { let mut result = Vec::new(); // Calculate buffer end for truncation handling @@ -2120,7 +2122,7 @@ mod _socket { cmsg = unsafe { libc::CMSG_NXTHDR(msg, cmsg) }; } - Ok(vm.ctx.new_list(result).into()) + vm.ctx.new_list(result).into() } // based on nix's implementation From 0de0b227fded16590648208804260ca120adb762 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:03:14 +0300 Subject: [PATCH 07/66] pyexpat --- crates/stdlib/src/pyexpat.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/stdlib/src/pyexpat.rs b/crates/stdlib/src/pyexpat.rs index 5dae80c304..8323a4ea10 100644 --- a/crates/stdlib/src/pyexpat.rs +++ b/crates/stdlib/src/pyexpat.rs @@ -124,9 +124,9 @@ mod _pyexpat { namespace_separator: Option, intern: Option, vm: &VirtualMachine, - ) -> PyResult { + ) -> PyExpatLikeXmlParserRef { let intern_dict = intern.unwrap_or_else(|| vm.ctx.new_dict().into()); - Ok(Self { + Self { namespace_separator, start_element: MutableObject::new(vm.ctx.none()), end_element: MutableObject::new(vm.ctx.none()), @@ -157,7 +157,7 @@ mod _pyexpat { attlist_decl: MutableObject::new(vm.ctx.none()), skipped_entity: MutableObject::new(vm.ctx.none()), } - .into_ref(&vm.ctx)) + .into_ref(&vm.ctx) } #[extend_class] @@ -353,21 +353,21 @@ mod _pyexpat { data: Either, _isfinal: OptionalArg, vm: &VirtualMachine, - ) -> PyResult { + ) -> i32 { let bytes = match data { Either::A(s) => s.as_bytes().to_vec(), Either::B(b) => b.as_bytes().to_vec(), }; // Empty data is valid - used to finalize parsing if bytes.is_empty() { - return Ok(1); + return 1; } let reader = Cursor::>::new(bytes); let parser = self.create_config().create_reader(reader); // Note: xml-rs is stricter than libexpat; some errors are silently ignored // to maintain compatibility with existing Python code let _ = self.do_parse(vm, parser); - Ok(1) + 1 } #[pymethod(name = "ParseFile")] @@ -417,7 +417,7 @@ mod _pyexpat { // encoding parameter is currently not used (xml-rs handles encoding from XML declaration) let _ = args.encoding; - PyExpatLikeXmlParser::new(ns_sep, args.intern, vm) + Ok(PyExpatLikeXmlParser::new(ns_sep, args.intern, vm)) } // TODO: Tie this exception to the module's state. From 8a05e32151140b904e09c10417f5c0dd08afba33 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:04:09 +0300 Subject: [PATCH 08/66] mmap --- crates/stdlib/src/mmap.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/stdlib/src/mmap.rs b/crates/stdlib/src/mmap.rs index 2d1fd51248..e5e77218a1 100644 --- a/crates/stdlib/src/mmap.rs +++ b/crates/stdlib/src/mmap.rs @@ -1345,8 +1345,8 @@ mod mmap { } #[pymethod] - fn tell(&self) -> PyResult { - Ok(self.pos()) + fn tell(&self) -> usize { + self.pos() } #[pymethod] From b139644696c624e90a2b33e70236920c7ec52041 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:04:55 +0300 Subject: [PATCH 09/66] _opcode --- crates/stdlib/src/_opcode.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/stdlib/src/_opcode.rs b/crates/stdlib/src/_opcode.rs index 80f422d69d..dc38e45443 100644 --- a/crates/stdlib/src/_opcode.rs +++ b/crates/stdlib/src/_opcode.rs @@ -165,12 +165,9 @@ mod _opcode { } #[pyfunction] - fn get_executor( - _code: PyObjectRef, - _offset: i32, - vm: &VirtualMachine, - ) -> PyResult { - Ok(vm.ctx.none()) + fn get_executor(_code: PyObjectRef, _offset: i32, vm: &VirtualMachine) -> PyObjectRef { + // TODO + vm.ctx.none() } #[pyfunction] From 81e3a751cfdaee2d9fec18044e88040f1da4a0a2 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:09:37 +0300 Subject: [PATCH 10/66] locale --- crates/stdlib/src/locale.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/stdlib/src/locale.rs b/crates/stdlib/src/locale.rs index a22c6afe57..085d07b250 100644 --- a/crates/stdlib/src/locale.rs +++ b/crates/stdlib/src/locale.rs @@ -99,12 +99,15 @@ mod _locale { vm.ctx.new_list(group_vec) } - unsafe fn pystr_from_raw_cstr(vm: &VirtualMachine, raw_ptr: *const libc::c_char) -> PyResult { + unsafe fn pystr_from_raw_cstr( + vm: &VirtualMachine, + raw_ptr: *const libc::c_char, + ) -> PyObjectRef { let slice = unsafe { CStr::from_ptr(raw_ptr) }; // Fast path: ASCII/UTF-8 if let Ok(s) = slice.to_str() { - return Ok(vm.new_pyobj(s)); + return vm.new_pyobj(s); } // On Windows, locale strings use the ANSI code page encoding @@ -131,12 +134,12 @@ mod _locale { wide.as_mut_ptr(), len, ); - return Ok(vm.new_pyobj(String::from_utf16_lossy(&wide))); + return vm.new_pyobj(String::from_utf16_lossy(&wide)); } } } - Ok(vm.new_pyobj(String::from_utf8_lossy(slice.to_bytes()).into_owned())) + vm.new_pyobj(String::from_utf8_lossy(slice.to_bytes()).into_owned()) } #[pyattr(name = "Error", once)] @@ -179,7 +182,7 @@ mod _locale { ($lc:expr, $field:ident) => {{ result.set_item( stringify!($field), - pystr_from_raw_cstr(vm, (*$lc).$field)?, + pystr_from_raw_cstr(vm, (*$lc).$field), vm, )? }}; @@ -264,6 +267,7 @@ mod _locale { if cfg!(windows) && (args.category < LC_ALL || args.category > LC_TIME) { return Err(vm.new_exception_msg(error, "unsupported locale setting".into())); } + unsafe { let result = match args.locale.flatten() { None => libc::setlocale(args.category, ptr::null()), @@ -288,10 +292,12 @@ mod _locale { libc::setlocale(args.category, c_locale.as_ptr()) } }; + if result.is_null() { return Err(vm.new_exception_msg(error, "unsupported locale setting".into())); } - pystr_from_raw_cstr(vm, result) + + Ok(pystr_from_raw_cstr(vm, result)) } } From 0a1c1bfeb0b5608055bf27460393ccea31f1ba66 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:10:58 +0300 Subject: [PATCH 11/66] sha256 & sha512 --- crates/stdlib/src/sha256.rs | 1 + crates/stdlib/src/sha512.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/stdlib/src/sha256.rs b/crates/stdlib/src/sha256.rs index b4c26dc0dd..6cc2e2ccb2 100644 --- a/crates/stdlib/src/sha256.rs +++ b/crates/stdlib/src/sha256.rs @@ -13,6 +13,7 @@ mod _sha256 { Ok(local_sha256(args, vm)?.into_pyobject(vm)) } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py) -> PyResult<()> { let _ = vm.import("_hashlib", 0); __module_exec(vm, module); diff --git a/crates/stdlib/src/sha512.rs b/crates/stdlib/src/sha512.rs index b7c6f02ed6..e34f06577a 100644 --- a/crates/stdlib/src/sha512.rs +++ b/crates/stdlib/src/sha512.rs @@ -13,6 +13,7 @@ mod _sha512 { Ok(local_sha512(args, vm)?.into_pyobject(vm)) } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py) -> PyResult<()> { let _ = vm.import("_hashlib", 0); __module_exec(vm, module); From 518b7ecfe0c472bbcb80bc7a91a19a9ccea76019 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:15:12 +0300 Subject: [PATCH 12/66] contextvars --- crates/stdlib/src/contextvars.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/crates/stdlib/src/contextvars.rs b/crates/stdlib/src/contextvars.rs index 3207537afe..ee5942755c 100644 --- a/crates/stdlib/src/contextvars.rs +++ b/crates/stdlib/src/contextvars.rs @@ -140,9 +140,9 @@ mod _contextvars { }) } - fn contains(&self, needle: &Py) -> PyResult { + fn contains(&self, needle: &Py) -> bool { let vars = self.borrow_vars(); - Ok(vars.get(needle).is_some()) + vars.get(needle).is_some() } fn get_inner(&self, needle: &Py) -> Option { @@ -202,13 +202,13 @@ mod _contextvars { &self, key: PyRef, default: OptionalArg, - ) -> PyResult> { + ) -> Option { let found = self.get_inner(&key); - Ok(if found.is_some() { + if found.is_some() { found } else { default.into_option() - }) + } } // TODO: wrong return type @@ -265,7 +265,7 @@ mod _contextvars { static AS_SEQUENCE: LazyLock = LazyLock::new(|| PySequenceMethods { contains: atomic_func!(|seq, target, vm| { let target = target.try_to_value(vm)?; - PyContext::sequence_downcast(seq).contains(target) + Ok(PyContext::sequence_downcast(seq).contains(target)) }), ..PySequenceMethods::NOT_IMPLEMENTED }); @@ -335,7 +335,7 @@ mod _contextvars { } // contextvar_set in CPython - fn set_inner(zelf: &Py, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + fn set_inner(zelf: &Py, value: PyObjectRef, vm: &VirtualMachine) { let ctx = PyContext::current(vm); let mut vars = ctx.borrow_vars_mut(); @@ -348,8 +348,6 @@ mod _contextvars { idx: ctx.inner.idx.get(), }; zelf.cached.store(Some(cache)); - - Ok(()) } fn generate_hash(zelf: &Py, vm: &VirtualMachine) -> PyHash { @@ -408,11 +406,7 @@ mod _contextvars { } #[pymethod] - fn set( - zelf: &Py, - value: PyObjectRef, - vm: &VirtualMachine, - ) -> PyResult> { + fn set(zelf: &Py, value: PyObjectRef, vm: &VirtualMachine) -> PyRef { let ctx = PyContext::current(vm); let old_value = ctx.borrow_vars().get(zelf).map(|v| v.to_owned()); @@ -424,9 +418,9 @@ mod _contextvars { }; // ctx.vars borrow must be released - Self::set_inner(zelf, value, vm)?; + Self::set_inner(zelf, value, vm); - Ok(token.into_ref(&vm.ctx)) + token.into_ref(&vm.ctx) } #[pymethod] @@ -456,7 +450,7 @@ mod _contextvars { token.used.set(true); if let Some(old_value) = &token.old_value { - Self::set_inner(zelf, old_value.clone(), vm)?; + Self::set_inner(zelf, old_value.clone(), vm); } else { Self::delete(zelf, vm)?; } From 68edbf95f9f9e2700b1b8e936a6ad11f4eae5ae3 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:21:17 +0300 Subject: [PATCH 13/66] _asyncio --- crates/stdlib/src/_asyncio.rs | 46 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/crates/stdlib/src/_asyncio.rs b/crates/stdlib/src/_asyncio.rs index 29bbe39a0f..f3911b1e59 100644 --- a/crates/stdlib/src/_asyncio.rs +++ b/crates/stdlib/src/_asyncio.rs @@ -609,7 +609,7 @@ pub(crate) mod _asyncio { } #[pygetset] - fn _callbacks(&self, vm: &VirtualMachine) -> PyResult { + fn _callbacks(&self, vm: &VirtualMachine) -> PyObjectRef { let mut result = Vec::new(); if let Some(cb0) = self.fut_callback0.read().clone() { @@ -630,9 +630,9 @@ pub(crate) mod _asyncio { // Return None if no callbacks if result.is_empty() { - Ok(vm.ctx.none()) + vm.ctx.none() } else { - Ok(vm.ctx.new_list(result).into()) + vm.ctx.new_list(result).into() } } @@ -764,10 +764,10 @@ pub(crate) mod _asyncio { } #[pymethod] - fn __await__(zelf: PyRef, _vm: &VirtualMachine) -> PyResult { - Ok(PyFutureIter { + fn __await__(zelf: PyRef, _vm: &VirtualMachine) -> PyFutureIter { + PyFutureIter { future: PyRwLock::new(Some(zelf.into())), - }) + } } #[pyclassmethod] @@ -861,23 +861,23 @@ pub(crate) mod _asyncio { .and_then(|asyncio| asyncio.get_attr(vm.ctx.intern_str("base_futures"), vm)) { Ok(m) => m, - Err(_) => return get_future_repr_info_fallback(future, vm), + Err(_) => return Ok(get_future_repr_info_fallback(future, vm)), } }; let func = match vm.get_attribute_opt(module, vm.ctx.intern_str("_future_repr_info")) { Ok(Some(f)) => f, - _ => return get_future_repr_info_fallback(future, vm), + _ => return Ok(get_future_repr_info_fallback(future, vm)), }; let info = match func.call((future.to_owned(),), vm) { Ok(i) => i, - Err(_) => return get_future_repr_info_fallback(future, vm), + Err(_) => return Ok(get_future_repr_info_fallback(future, vm)), }; let list: PyListRef = match info.downcast() { Ok(l) => l, - Err(_) => return get_future_repr_info_fallback(future, vm), + Err(_) => return Ok(get_future_repr_info_fallback(future, vm)), }; let mut result = Wtf8Buf::new(); @@ -893,17 +893,17 @@ pub(crate) mod _asyncio { Ok(result) } - fn get_future_repr_info_fallback(future: &PyObject, vm: &VirtualMachine) -> PyResult { + fn get_future_repr_info_fallback(future: &PyObject, vm: &VirtualMachine) -> Wtf8Buf { // Fallback: build repr from properties directly if let Ok(Some(state)) = vm.get_attribute_opt(future.to_owned(), vm.ctx.intern_str("_state")) { - let s = state + state .str(vm) - .map_or_else(|_| Wtf8Buf::from("unknown"), |s| s.as_wtf8().to_lowercase()); - return Ok(s); + .map_or_else(|_| Wtf8Buf::from("unknown"), |s| s.as_wtf8().to_lowercase()) + } else { + Wtf8Buf::from("state=unknown") } - Ok(Wtf8Buf::from("state=unknown")) } fn get_task_repr_info(task: &PyObject, vm: &VirtualMachine) -> PyResult { @@ -1820,10 +1820,10 @@ pub(crate) mod _asyncio { } #[pymethod] - fn __await__(zelf: PyRef, _vm: &VirtualMachine) -> PyResult { - Ok(PyFutureIter { + fn __await__(zelf: PyRef, _vm: &VirtualMachine) -> PyFutureIter { + PyFutureIter { future: PyRwLock::new(Some(zelf.into())), - }) + } } #[pyclassmethod] @@ -1960,7 +1960,7 @@ pub(crate) mod _asyncio { _register_eager_task(task_obj.clone(), vm)?; // Swap current task - save previous task - let prev_task = _swap_current_task(loop_obj.clone(), task_obj.clone(), vm)?; + let prev_task = _swap_current_task(loop_obj.clone(), task_obj.clone(), vm); // Get coro and context let coro = zelf.task_coro.read().clone(); @@ -2525,7 +2525,11 @@ pub(crate) mod _asyncio { } #[pyfunction] - fn _swap_current_task(loop_: PyObjectRef, task: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn _swap_current_task( + loop_: PyObjectRef, + task: PyObjectRef, + vm: &VirtualMachine, + ) -> PyObjectRef { // Per-thread swap, matching CPython's swap_current_task let prev = vm .asyncio_running_task @@ -2550,7 +2554,7 @@ pub(crate) mod _asyncio { } } - Ok(prev) + prev } /// Reset task state after fork in child process. From c8f47b0fe18ff08ef90b14bf40f949285bcec8d5 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:32:38 +0300 Subject: [PATCH 14/66] warn.rs --- crates/vm/src/warn.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/crates/vm/src/warn.rs b/crates/vm/src/warn.rs index cde821cfaa..39573b1804 100644 --- a/crates/vm/src/warn.rs +++ b/crates/vm/src/warn.rs @@ -114,7 +114,7 @@ fn get_warnings_attr( vm: &VirtualMachine, attr_name: &'static PyStrInterned, try_import: bool, -) -> PyResult> { +) -> Option { let module = if try_import && !vm .state @@ -123,38 +123,36 @@ fn get_warnings_attr( { match vm.import("warnings", 0) { Ok(module) => module, - Err(_) => return Ok(None), + Err(_) => return None, } } else { match vm.sys_module.get_attr(identifier!(vm, modules), vm) { Ok(modules) => match modules.get_item(vm.ctx.intern_str("warnings"), vm) { Ok(module) => module, - Err(_) => return Ok(None), + Err(_) => return None, }, - Err(_) => return Ok(None), + Err(_) => return None, } }; - match module.get_attr(attr_name, vm) { - Ok(attr) => Ok(Some(attr)), - Err(_) => Ok(None), - } + + module.get_attr(attr_name, vm).ok() } /// Get the warnings filters list from sys.modules['warnings'].filters, /// falling back to vm.state.warnings.filters. -fn get_warnings_filters(vm: &VirtualMachine) -> PyResult { - if let Some(filters_obj) = get_warnings_attr(vm, identifier!(&vm.ctx, filters), false)? +fn get_warnings_filters(vm: &VirtualMachine) -> PyListRef { + if let Some(filters_obj) = get_warnings_attr(vm, identifier!(&vm.ctx, filters), false) && let Ok(filters) = filters_obj.try_into_value::(vm) { - return Ok(filters); + return filters; } - Ok(vm.state.warnings.filters.clone()) + vm.state.warnings.filters.clone() } /// Get the default action from sys.modules['warnings']._defaultaction, /// falling back to vm.state.warnings.default_action. fn get_default_action(vm: &VirtualMachine) -> PyResult { - if let Some(action) = get_warnings_attr(vm, identifier!(&vm.ctx, defaultaction), false)? { + if let Some(action) = get_warnings_attr(vm, identifier!(&vm.ctx, defaultaction), false) { if !action.class().is(vm.ctx.types.str_type) { return Err(vm.new_type_error(format!( "_warnings.defaultaction must be a string, not '{}'", @@ -169,7 +167,7 @@ fn get_default_action(vm: &VirtualMachine) -> PyResult { /// Get the once registry from sys.modules['warnings']._onceregistry, /// falling back to vm.state.warnings.once_registry. fn get_once_registry(vm: &VirtualMachine) -> PyResult { - if let Some(registry) = get_warnings_attr(vm, identifier!(&vm.ctx, onceregistry), false)? { + if let Some(registry) = get_warnings_attr(vm, identifier!(&vm.ctx, onceregistry), false) { if !registry.class().is(vm.ctx.types.dict_type) { return Err(vm.new_type_error(format!( "_warnings.onceregistry must be a dict, not '{}'", @@ -265,7 +263,7 @@ fn get_filter( module: PyObjectRef, vm: &VirtualMachine, ) -> PyResult { - let filters = get_warnings_filters(vm)?; + let filters = get_warnings_filters(vm); // filters could change while we are iterating over it. // Re-check list length each iteration (matches C behavior). @@ -463,15 +461,17 @@ fn call_show_warning( source: Option, vm: &VirtualMachine, ) -> PyResult<()> { - let Some(show_fn) = - get_warnings_attr(vm, identifier!(&vm.ctx, _showwarnmsg), source.is_some())? + let Some(show_fn) = get_warnings_attr(vm, identifier!(&vm.ctx, _showwarnmsg), source.is_some()) else { - return show_warning(filename, lineno, text, category, source_line, vm); + show_warning(filename, lineno, text, category, source_line, vm); + return Ok(()); }; + if !show_fn.is_callable() { return Err(vm.new_type_error("warnings._showwarnmsg() must be set to a callable")); } - let Some(warnmsg_cls) = get_warnings_attr(vm, identifier!(&vm.ctx, WarningMessage), false)? + + let Some(warnmsg_cls) = get_warnings_attr(vm, identifier!(&vm.ctx, WarningMessage), false) else { return Err(vm.new_runtime_error("unable to get warnings.WarningMessage")); }; @@ -488,6 +488,7 @@ fn call_show_warning( ], vm, )?; + show_fn.call((msg,), vm)?; Ok(()) } @@ -499,7 +500,7 @@ fn show_warning( category: PyTypeRef, _source_line: Option, vm: &VirtualMachine, -) -> PyResult<()> { +) { let stderr = crate::stdlib::sys::PyStderr(vm); writeln!( stderr, @@ -509,7 +510,6 @@ fn show_warning( category.name(), text ); - Ok(()) } /// Check if a frame's filename starts with any of the given prefixes. From 2da829dfc72a38f2b2cd01b98c0ebf284a7b9534 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:35:28 +0300 Subject: [PATCH 15/66] slot --- crates/vm/src/types/slot.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/vm/src/types/slot.rs b/crates/vm/src/types/slot.rs index 3d64d121b6..2e8f5cf58f 100644 --- a/crates/vm/src/types/slot.rs +++ b/crates/vm/src/types/slot.rs @@ -579,7 +579,8 @@ fn bool_wrapper(num: PyNumber<'_>, vm: &VirtualMachine) -> PyResult { Ok(crate::builtins::bool_::get_value(&result)) } -// PyObject_SelfIter in CPython +/// PyObject_SelfIter in CPython +#[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] const fn self_iter(zelf: PyObjectRef, _vm: &VirtualMachine) -> PyResult { Ok(zelf) } From d280bda2f9d1655bc5a7df04c6c70909b82b207a Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:41:04 +0300 Subject: [PATCH 16/66] sys --- crates/vm/src/stdlib/sys.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/vm/src/stdlib/sys.rs b/crates/vm/src/stdlib/sys.rs index db978becbb..88f487778a 100644 --- a/crates/vm/src/stdlib/sys.rs +++ b/crates/vm/src/stdlib/sys.rs @@ -85,16 +85,15 @@ pub mod sys { #[pyclass] impl BootstrapStderr { #[pymethod] - fn write(&self, s: PyStrRef) -> PyResult { + fn write(&self, s: PyStrRef) -> usize { let bytes = s.as_bytes(); let _ = std::io::stderr().write_all(bytes); - Ok(bytes.len()) + bytes.len() } #[pymethod] - fn flush(&self) -> PyResult<()> { + fn flush(&self) { let _ = std::io::stderr().flush(); - Ok(()) } } @@ -997,14 +996,14 @@ pub mod sys { } #[pyfunction] - fn _getframemodulename(depth: OptionalArg, vm: &VirtualMachine) -> PyResult { + fn _getframemodulename(depth: OptionalArg, vm: &VirtualMachine) -> PyObjectRef { let depth = depth.into_option().unwrap_or(0); // Get the frame at the specified depth let func_obj = { let frames = vm.frames.borrow(); if depth >= frames.len() { - return Ok(vm.ctx.none()); + return vm.ctx.none(); } let idx = frames.len() - depth - 1; // SAFETY: the FrameRef is alive on the call stack while it's in the Vec @@ -1014,15 +1013,14 @@ pub mod sys { // If the frame has a function object, return its __module__ attribute if let Some(func_obj) = func_obj { - match func_obj.get_attr(identifier!(vm, __module__), vm) { - Ok(module) => Ok(module), - Err(_) => { + func_obj + .get_attr(identifier!(vm, __module__), vm) + .unwrap_or_else( // CPython clears the error and returns None - Ok(vm.ctx.none()) - } - } + |_| vm.ctx.none(), + ) } else { - Ok(vm.ctx.none()) + vm.ctx.none() } } From bd57c081685e538beec3a76d75d59e86fa28c5d2 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:42:25 +0300 Subject: [PATCH 17/66] _thread --- crates/vm/src/stdlib/_thread.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/vm/src/stdlib/_thread.rs b/crates/vm/src/stdlib/_thread.rs index 3ce360996f..8f25be9c38 100644 --- a/crates/vm/src/stdlib/_thread.rs +++ b/crates/vm/src/stdlib/_thread.rs @@ -140,11 +140,10 @@ pub(crate) mod _thread { #[cfg(unix)] #[pymethod] - fn _at_fork_reinit(&self, _vm: &VirtualMachine) -> PyResult<()> { + fn _at_fork_reinit(&self, _vm: &VirtualMachine) { // Overwrite lock state to unlocked. Do NOT call unlock() here — // after fork(), unlock_slow() would try to unpark stale waiters. unsafe { rustpython_common::lock::zero_reinit_after_fork(&self.mu) }; - Ok(()) } #[pymethod] @@ -235,12 +234,11 @@ pub(crate) mod _thread { #[cfg(unix)] #[pymethod] - fn _at_fork_reinit(&self, _vm: &VirtualMachine) -> PyResult<()> { + fn _at_fork_reinit(&self, _vm: &VirtualMachine) { // Overwrite lock state to unlocked. Do NOT call unlock() here — // after fork(), unlock_slow() would try to unpark stale waiters. self.count.store(0, core::sync::atomic::Ordering::Relaxed); unsafe { rustpython_common::lock::zero_reinit_after_fork(&self.mu) }; - Ok(()) } #[pymethod] From 87e5bf08a67153913a266d3b8a70ce757ee1e133 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 16:43:27 +0300 Subject: [PATCH 18/66] _signal --- crates/vm/src/stdlib/_signal.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/vm/src/stdlib/_signal.rs b/crates/vm/src/stdlib/_signal.rs index 60e5c39a85..93880b017f 100644 --- a/crates/vm/src/stdlib/_signal.rs +++ b/crates/vm/src/stdlib/_signal.rs @@ -703,6 +703,7 @@ pub(crate) mod _signal { WAKEUP.store(INVALID_WAKEUP, Ordering::Relaxed); } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn module_exec( vm: &VirtualMachine, module: &Py, From 541d39b719468097c63543cb803c22d991590043 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 17:10:48 +0300 Subject: [PATCH 19/66] _ctypes --- crates/vm/src/stdlib/_ctypes.rs | 15 ++---- crates/vm/src/stdlib/_ctypes/array.rs | 53 ++++++++++--------- crates/vm/src/stdlib/_ctypes/base.rs | 63 +++++++++++------------ crates/vm/src/stdlib/_ctypes/function.rs | 3 +- crates/vm/src/stdlib/_ctypes/pointer.rs | 53 ++++++++++--------- crates/vm/src/stdlib/_ctypes/structure.rs | 2 +- crates/vm/src/stdlib/_ctypes/union.rs | 2 +- 7 files changed, 96 insertions(+), 95 deletions(-) diff --git a/crates/vm/src/stdlib/_ctypes.rs b/crates/vm/src/stdlib/_ctypes.rs index f4799dd795..2c5fc3e537 100644 --- a/crates/vm/src/stdlib/_ctypes.rs +++ b/crates/vm/src/stdlib/_ctypes.rs @@ -585,13 +585,9 @@ pub(crate) mod _ctypes { if handle.is_null() { let err = unsafe { libc::dlerror() }; let msg = if err.is_null() { - "dlopen() error".to_string() + "dlopen() error" } else { - unsafe { - core::ffi::CStr::from_ptr(err) - .to_string_lossy() - .into_owned() - } + unsafe { &core::ffi::CStr::from_ptr(err).to_string_lossy() } }; return Err(vm.new_os_error(msg)); } @@ -605,22 +601,20 @@ pub(crate) mod _ctypes { } #[pyfunction(name = "FreeLibrary")] - fn free_library(handle: usize) -> PyResult<()> { + fn free_library(handle: usize) { let cache = library::libcache(); let mut cache_write = cache.write(); cache_write.drop_lib(handle); - Ok(()) } #[cfg(not(windows))] #[pyfunction] - fn dlclose(handle: usize, _vm: &VirtualMachine) -> PyResult<()> { + fn dlclose(handle: usize, _vm: &VirtualMachine) { // Remove from cache, which triggers SharedLibrary drop. // libloading::Library calls dlclose automatically on Drop. let cache = library::libcache(); let mut cache_write = cache.write(); cache_write.drop_lib(handle); - Ok(()) } #[cfg(not(windows))] @@ -1295,6 +1289,7 @@ pub(crate) mod _ctypes { Ok(S_OK) } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn module_exec( vm: &VirtualMachine, module: &Py, diff --git a/crates/vm/src/stdlib/_ctypes/array.rs b/crates/vm/src/stdlib/_ctypes/array.rs index 657f0a2146..11695f4e49 100644 --- a/crates/vm/src/stdlib/_ctypes/array.rs +++ b/crates/vm/src/stdlib/_ctypes/array.rs @@ -613,13 +613,13 @@ impl PyCArray { }; let buffer = buffer_lock.read(); - Self::read_element_from_buffer( + Ok(Self::read_element_from_buffer( &buffer, final_offset, element_size, type_code.as_deref(), vm, - ) + )) } /// Helper to read an element value from a buffer at given offset @@ -629,14 +629,14 @@ impl PyCArray { element_size: usize, type_code: Option<&str>, vm: &VirtualMachine, - ) -> PyResult { + ) -> PyObjectRef { match type_code { Some("c") => { // Return single byte as bytes if offset < buffer.len() { - Ok(vm.ctx.new_bytes(vec![buffer[offset]]).into()) + vm.ctx.new_bytes(vec![buffer[offset]]).into() } else { - Ok(vm.ctx.new_bytes(vec![0]).into()) + vm.ctx.new_bytes(vec![0]).into() } } Some("u") => { @@ -645,25 +645,28 @@ impl PyCArray { let s = char::from_u32(code) .map(|c| c.to_string()) .unwrap_or_default(); - Ok(vm.ctx.new_str(s).into()) + vm.ctx.new_str(s).into() } else { - Ok(vm.ctx.new_str("").into()) + vm.ctx.new_str("").into() } } Some("z") => { // c_char_p: pointer to bytes - dereference to get string if offset + element_size > buffer.len() { - return Ok(vm.ctx.none()); + return vm.ctx.none(); } + let ptr_bytes = &buffer[offset..offset + element_size]; let ptr_val = usize::from_ne_bytes( ptr_bytes .try_into() .unwrap_or([0; core::mem::size_of::()]), ); + if ptr_val == 0 { - return Ok(vm.ctx.none()); + return vm.ctx.none(); } + // Read null-terminated string from pointer address unsafe { let ptr = ptr_val as *const u8; @@ -672,23 +675,26 @@ impl PyCArray { len += 1; } let bytes = core::slice::from_raw_parts(ptr, len); - Ok(vm.ctx.new_bytes(bytes.to_vec()).into()) + vm.ctx.new_bytes(bytes.to_vec()).into() } } Some("Z") => { // c_wchar_p: pointer to wchar_t - dereference to get string if offset + element_size > buffer.len() { - return Ok(vm.ctx.none()); + return vm.ctx.none(); } + let ptr_bytes = &buffer[offset..offset + element_size]; let ptr_val = usize::from_ne_bytes( ptr_bytes .try_into() .unwrap_or([0; core::mem::size_of::()]), ); + if ptr_val == 0 { - return Ok(vm.ctx.none()); + return vm.ctx.none(); } + // Read null-terminated wide string using WCHAR_SIZE unsafe { let ptr = ptr_val as *const u8; @@ -710,8 +716,9 @@ impl PyCArray { } pos += WCHAR_SIZE; } + let s: String = chars.into_iter().collect(); - Ok(vm.ctx.new_str(s).into()) + vm.ctx.new_str(s).into() } } Some("f") => { @@ -720,7 +727,7 @@ impl PyCArray { .first_chunk::<4>() .copied() .map_or(0.0, f32::from_ne_bytes); - Ok(vm.ctx.new_float(val as f64).into()) + vm.ctx.new_float(val as f64).into() } Some("d" | "g") => { // c_double / c_longdouble - read f64 from first 8 bytes @@ -728,13 +735,13 @@ impl PyCArray { .first_chunk::<8>() .copied() .map_or(0.0, f64::from_ne_bytes); - Ok(vm.ctx.new_float(val).into()) + vm.ctx.new_float(val).into() } _ => { if let Some(bytes) = buffer[offset..].get(..element_size) { - Ok(Self::bytes_to_int(bytes, element_size, type_code, vm)) + Self::bytes_to_int(bytes, element_size, type_code, vm) } else { - Ok(vm.ctx.new_int(0).into()) + vm.ctx.new_int(0).into() } } } @@ -1169,11 +1176,11 @@ impl AsBuffer for PyCArray { // CharArray and WCharArray getsets - added dynamically via add_getset // CharArray_get_value -fn char_array_get_value(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { +fn char_array_get_value(obj: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { let zelf = obj.downcast_ref::().unwrap(); let buffer = zelf.0.buffer.read(); let len = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len()); - Ok(vm.ctx.new_bytes(buffer[..len].to_vec()).into()) + vm.ctx.new_bytes(buffer[..len].to_vec()).into() } // CharArray_set_value @@ -1197,10 +1204,10 @@ fn char_array_set_value(obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachin } // CharArray_get_raw -fn char_array_get_raw(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { +fn char_array_get_raw(obj: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { let zelf = obj.downcast_ref::().unwrap(); let buffer = zelf.0.buffer.read(); - Ok(vm.ctx.new_bytes(buffer.to_vec()).into()) + vm.ctx.new_bytes(buffer.to_vec()).into() } // CharArray_set_raw @@ -1222,10 +1229,10 @@ fn char_array_set_raw( } // WCharArray_get_value -fn wchar_array_get_value(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { +fn wchar_array_get_value(obj: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { let zelf = obj.downcast_ref::().unwrap(); let buffer = zelf.0.buffer.read(); - Ok(vm.ctx.new_str(wstring_from_bytes(&buffer)).into()) + vm.ctx.new_str(wstring_from_bytes(&buffer)).into() } // WCharArray_set_value diff --git a/crates/vm/src/stdlib/_ctypes/base.rs b/crates/vm/src/stdlib/_ctypes/base.rs index f104afa065..ed0084025c 100644 --- a/crates/vm/src/stdlib/_ctypes/base.rs +++ b/crates/vm/src/stdlib/_ctypes/base.rs @@ -1072,7 +1072,7 @@ impl PyCData { let (mut bytes, converted_value) = if let Some(type_code) = &field_type_code { PyCField::value_to_bytes_for_type(type_code, &value, size, vm)? } else { - (PyCField::value_to_bytes(&value, size, vm)?, None) + (PyCField::value_to_bytes(&value, size, vm), None) }; // Swap bytes for opposite endianness @@ -1479,11 +1479,10 @@ impl Constructor for PyCField { .try_to_value(vm)?; // Validate byte_size matches the type - let type_size = super::base::get_field_size(field_type.as_object(), vm)? as isize; + let type_size = super::base::get_field_size(field_type.as_object(), vm) as isize; if byte_size != type_size { return Err(vm.new_value_error(format!( - "byte_size {} does not match type size {}", - byte_size, type_size + "byte_size {byte_size} does not match type size {type_size}" ))); } @@ -1588,14 +1587,14 @@ impl GetDescriptor for PyCField { impl PyCField { /// Convert a Python value to bytes - fn value_to_bytes(value: &PyObject, size: usize, vm: &VirtualMachine) -> PyResult> { + fn value_to_bytes(value: &PyObject, size: usize, vm: &VirtualMachine) -> Vec { // 1. Handle bytes objects if let Some(bytes) = value.downcast_ref::() { let src = bytes.as_bytes(); let mut result = vec![0u8; size]; let len = core::cmp::min(src.len(), size); result[..len].copy_from_slice(&src[..len]); - Ok(result) + result } // 2. Handle ctypes array instances (copy their buffer) else if let Some(cdata) = value.downcast_ref::() { @@ -1603,7 +1602,7 @@ impl PyCField { let mut result = vec![0u8; size]; let len = core::cmp::min(buffer.len(), size); result[..len].copy_from_slice(&buffer[..len]); - Ok(result) + result } // 4. Handle float values (check before int, since float.try_int would truncate) else if let Some(float_val) = value.downcast_ref::() { @@ -1611,9 +1610,9 @@ impl PyCField { match size { 4 => { let val = f as f32; - Ok(val.to_ne_bytes().to_vec()) + val.to_ne_bytes().to_vec() } - 8 => Ok(f.to_ne_bytes().to_vec()), + 8 => f.to_ne_bytes().to_vec(), _ => unreachable!("wrong payload size"), } } @@ -1623,24 +1622,24 @@ impl PyCField { match size { 1 => { let val = i.to_i8().unwrap_or(0); - Ok(val.to_ne_bytes().to_vec()) + val.to_ne_bytes().to_vec() } 2 => { let val = i.to_i16().unwrap_or(0); - Ok(val.to_ne_bytes().to_vec()) + val.to_ne_bytes().to_vec() } 4 => { let val = i.to_i32().unwrap_or(0); - Ok(val.to_ne_bytes().to_vec()) + val.to_ne_bytes().to_vec() } 8 => { let val = i.to_i64().unwrap_or(0); - Ok(val.to_ne_bytes().to_vec()) + val.to_ne_bytes().to_vec() } - _ => Ok(vec![0u8; size]), + _ => vec![0u8; size], } } else { - Ok(vec![0u8; size]) + vec![0u8; size] } } @@ -1712,7 +1711,7 @@ impl PyCField { if vm.is_none(value) { return Ok((vec![0u8; size], None)); } - Ok((PyCField::value_to_bytes(value, size, vm)?, None)) + Ok((PyCField::value_to_bytes(value, size, vm), None)) } "Z" => { // c_wchar_p: store pointer to null-terminated wchar_t buffer @@ -1737,7 +1736,7 @@ impl PyCField { if vm.is_none(value) { return Ok((vec![0u8; size], None)); } - Ok((PyCField::value_to_bytes(value, size, vm)?, None)) + Ok((PyCField::value_to_bytes(value, size, vm), None)) } "P" => { // c_void_p: store integer as pointer @@ -1753,9 +1752,9 @@ impl PyCField { if vm.is_none(value) { return Ok((vec![0u8; size], None)); } - Ok((PyCField::value_to_bytes(value, size, vm)?, None)) + Ok((PyCField::value_to_bytes(value, size, vm), None)) } - _ => Ok((PyCField::value_to_bytes(value, size, vm)?, None)), + _ => Ok((PyCField::value_to_bytes(value, size, vm), None)), } } @@ -1947,7 +1946,7 @@ pub(super) fn call_paramfunc(obj: &PyObject, vm: &VirtualMachine) -> PyResult simple_paramfunc(obj, vm), ParamFunc::Array => array_paramfunc(obj, vm), ParamFunc::Pointer => pointer_paramfunc(obj, vm), - ParamFunc::Structure | ParamFunc::Union => struct_union_paramfunc(obj, &stg_info, vm), + ParamFunc::Structure | ParamFunc::Union => Ok(struct_union_paramfunc(obj, &stg_info, vm)), ParamFunc::None => Err(vm.new_type_error("no paramfunc")), } } @@ -2022,36 +2021,32 @@ fn pointer_paramfunc(obj: &PyObject, vm: &VirtualMachine) -> PyResult PyResult { +fn struct_union_paramfunc(obj: &PyObject, stg_info: &StgInfo, _vm: &VirtualMachine) -> CArgObject { // Get buffer pointer // For large structs (> sizeof(void*)), we'd need to allocate and copy. // For now, just point to buffer directly and keep obj reference for memory safety. let buffer = if let Some(cdata) = obj.downcast_ref::() { cdata.buffer.read() } else { - return Ok(CArgObject { + return CArgObject { tag: b'V', value: FfiArgValue::Pointer(0), obj: obj.to_owned(), size: stg_info.size, offset: 0, - }); + }; }; let ptr_val = buffer.as_ptr() as usize; let size = buffer.len(); - Ok(CArgObject { + CArgObject { tag: b'V', value: FfiArgValue::Pointer(ptr_val), obj: obj.to_owned(), size, offset: 0, - }) + } } // FfiArgValue - Owned FFI argument value @@ -2467,11 +2462,11 @@ pub(super) fn check_other_endian_support( } /// Get the size of a ctypes field type -pub(super) fn get_field_size(field_type: &PyObject, vm: &VirtualMachine) -> PyResult { +pub(super) fn get_field_size(field_type: &PyObject, vm: &VirtualMachine) -> usize { if let Some(type_obj) = field_type.downcast_ref::() && let Some(stg_info) = type_obj.stg_info_opt() { - return Ok(stg_info.size); + return stg_info.size; } if let Some(size) = field_type @@ -2483,7 +2478,7 @@ pub(super) fn get_field_size(field_type: &PyObject, vm: &VirtualMachine) -> PyRe (s.len() == 1).then(|| super::get_size(&s)) }) { - return Ok(size); + return size; } if let Some(s) = field_type @@ -2493,10 +2488,10 @@ pub(super) fn get_field_size(field_type: &PyObject, vm: &VirtualMachine) -> PyRe .and_then(|size| size.try_int(vm).ok()) .and_then(|n| n.as_bigint().to_usize()) { - return Ok(s); + return s; } - Ok(core::mem::size_of::()) + core::mem::size_of::() } /// Get the alignment of a ctypes field type diff --git a/crates/vm/src/stdlib/_ctypes/function.rs b/crates/vm/src/stdlib/_ctypes/function.rs index 00bbe2ad52..6418158e91 100644 --- a/crates/vm/src/stdlib/_ctypes/function.rs +++ b/crates/vm/src/stdlib/_ctypes/function.rs @@ -1903,14 +1903,13 @@ impl PyCFuncPtr { } #[pygetset(name = "argtypes", setter)] - fn set_argtypes(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + fn set_argtypes(&self, value: PyObjectRef, vm: &VirtualMachine) { if vm.is_none(&value) { *self.argtypes.write() = None; } else { // Store the argtypes object directly as it is *self.argtypes.write() = Some(value); } - Ok(()) } // errcheck getter/setter diff --git a/crates/vm/src/stdlib/_ctypes/pointer.rs b/crates/vm/src/stdlib/_ctypes/pointer.rs index 57535c175c..b900f9fc56 100644 --- a/crates/vm/src/stdlib/_ctypes/pointer.rs +++ b/crates/vm/src/stdlib/_ctypes/pointer.rs @@ -418,7 +418,12 @@ impl PyCPointer { && let Ok(type_str) = type_attr.str(vm) { let type_code = type_str.to_string(); - return Self::read_value_at_address(addr, element_size, Some(&type_code), vm); + return Ok(Self::read_value_at_address( + addr, + element_size, + Some(&type_code), + vm, + )); } // Complex type: create instance that references the memory directly (not a copy) @@ -658,55 +663,55 @@ impl PyCPointer { size: usize, type_code: Option<&str>, vm: &VirtualMachine, - ) -> PyResult { + ) -> PyObjectRef { unsafe { let ptr = addr as *const u8; match type_code { // Single-byte types don't need read_unaligned - Some("c") => Ok(vm.ctx.new_bytes(vec![*ptr]).into()), - Some("b") => Ok(vm.ctx.new_int(*ptr as i8 as i32).into()), - Some("B") => Ok(vm.ctx.new_int(*ptr as i32).into()), + Some("c") => vm.ctx.new_bytes(vec![*ptr]).into(), + Some("b") => vm.ctx.new_int(*ptr as i8 as i32).into(), + Some("B") => vm.ctx.new_int(*ptr as i32).into(), // Multi-byte types need read_unaligned for safety on strict-alignment architectures - Some("h") => Ok(vm + Some("h") => vm .ctx .new_int(core::ptr::read_unaligned(ptr as *const i16) as i32) - .into()), - Some("H") => Ok(vm + .into(), + Some("H") => vm .ctx .new_int(core::ptr::read_unaligned(ptr as *const u16) as i32) - .into()), - Some("i" | "l") => Ok(vm + .into(), + Some("i" | "l") => vm .ctx .new_int(core::ptr::read_unaligned(ptr as *const i32)) - .into()), - Some("I" | "L") => Ok(vm + .into(), + Some("I" | "L") => vm .ctx .new_int(core::ptr::read_unaligned(ptr as *const u32)) - .into()), - Some("q") => Ok(vm + .into(), + Some("q") => vm .ctx .new_int(core::ptr::read_unaligned(ptr as *const i64)) - .into()), - Some("Q") => Ok(vm + .into(), + Some("Q") => vm .ctx .new_int(core::ptr::read_unaligned(ptr as *const u64)) - .into()), - Some("f") => Ok(vm + .into(), + Some("f") => vm .ctx .new_float(core::ptr::read_unaligned(ptr as *const f32) as f64) - .into()), - Some("d" | "g") => Ok(vm + .into(), + Some("d" | "g") => vm .ctx .new_float(core::ptr::read_unaligned(ptr as *const f64)) - .into()), - Some("P" | "z" | "Z") => Ok(vm + .into(), + Some("P" | "z" | "Z") => vm .ctx .new_int(core::ptr::read_unaligned(ptr as *const usize)) - .into()), + .into(), _ => { // Default: read as bytes let bytes = core::slice::from_raw_parts(ptr, size).to_vec(); - Ok(vm.ctx.new_bytes(bytes).into()) + vm.ctx.new_bytes(bytes).into() } } } diff --git a/crates/vm/src/stdlib/_ctypes/structure.rs b/crates/vm/src/stdlib/_ctypes/structure.rs index 6651392432..a087fee2ae 100644 --- a/crates/vm/src/stdlib/_ctypes/structure.rs +++ b/crates/vm/src/stdlib/_ctypes/structure.rs @@ -331,7 +331,7 @@ impl PyCStructType { } // Get size and alignment of the field type - let size = super::base::get_field_size(&field_type, vm)?; + let size = super::base::get_field_size(&field_type, vm); let field_align = super::base::get_field_align(&field_type, vm); // Calculate effective alignment (PyCField_FromDesc) diff --git a/crates/vm/src/stdlib/_ctypes/union.rs b/crates/vm/src/stdlib/_ctypes/union.rs index a2845fc78c..d2c0ce1a02 100644 --- a/crates/vm/src/stdlib/_ctypes/union.rs +++ b/crates/vm/src/stdlib/_ctypes/union.rs @@ -229,7 +229,7 @@ impl PyCUnionType { super::base::check_other_endian_support(&field_type, vm)?; } - let size = super::base::get_field_size(&field_type, vm)?; + let size = super::base::get_field_size(&field_type, vm); let field_align = super::base::get_field_align(&field_type, vm); // Calculate effective alignment From 88c64f16779877d59154fca4042a0a0b26d2357b Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 17:16:42 +0300 Subject: [PATCH 20/66] os --- crates/vm/src/stdlib/os.rs | 43 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/crates/vm/src/stdlib/os.rs b/crates/vm/src/stdlib/os.rs index 5b95e0be85..67fd4d0dd2 100644 --- a/crates/vm/src/stdlib/os.rs +++ b/crates/vm/src/stdlib/os.rs @@ -637,13 +637,13 @@ pub(super) mod _os { #[pyclass(flags(DISALLOW_INSTANTIATION), with(Representable))] impl DirEntry { #[pygetset] - fn name(&self, vm: &VirtualMachine) -> PyResult { - Ok(self.mode.process_path(&self.file_name, vm)) + fn name(&self, vm: &VirtualMachine) -> PyObjectRef { + self.mode.process_path(&self.file_name, vm) } #[pygetset] - fn path(&self, vm: &VirtualMachine) -> PyResult { - Ok(self.mode.process_path(&self.pathval, vm)) + fn path(&self, vm: &VirtualMachine) -> PyObjectRef { + self.mode.process_path(&self.pathval, vm) } /// Build the DirFd to use for stat calls. @@ -837,30 +837,30 @@ pub(super) mod _os { #[cfg(unix)] #[pymethod] - fn inode(&self, _vm: &VirtualMachine) -> PyResult { - Ok(self.ino.load()) + fn inode(&self, _vm: &VirtualMachine) -> u64 { + self.ino.load() } #[cfg(not(any(unix, windows)))] #[pymethod] - fn inode(&self, _vm: &VirtualMachine) -> PyResult> { - Ok(self.ino.load()) + fn inode(&self, _vm: &VirtualMachine) -> Option { + self.ino.load() } #[cfg(not(windows))] #[pymethod] - const fn is_junction(&self, _vm: &VirtualMachine) -> PyResult { - Ok(false) + const fn is_junction(&self, _vm: &VirtualMachine) -> bool { + false } #[cfg(windows)] #[pymethod] - fn is_junction(&self, _vm: &VirtualMachine) -> PyResult { - Ok(junction::exists(self.pathval.clone()).unwrap_or(false)) + fn is_junction(&self, _vm: &VirtualMachine) -> bool { + junction::exists(self.pathval.clone()).unwrap_or(false) } #[pymethod] - fn __fspath__(&self, vm: &VirtualMachine) -> PyResult { + fn __fspath__(&self, vm: &VirtualMachine) -> PyObjectRef { self.path(vm) } @@ -941,6 +941,7 @@ pub(super) mod _os { Err(vm.new_type_error("cannot pickle 'ScandirIterator' object")) } } + impl Destructor for ScandirIterator { fn del(zelf: &Py, vm: &VirtualMachine) -> PyResult<()> { // Emit ResourceWarning if the iterator is not yet exhausted/closed @@ -956,7 +957,9 @@ pub(super) mod _os { Ok(()) } } + impl SelfIter for ScandirIterator {} + impl IterNext for ScandirIterator { fn next(zelf: &crate::Py, vm: &VirtualMachine) -> PyResult { let entryref: &mut Option = &mut zelf.entries.write(); @@ -2107,14 +2110,14 @@ pub(super) mod _os { } #[pyfunction] - fn device_encoding(fd: i32, _vm: &VirtualMachine) -> PyResult> { + fn device_encoding(fd: i32, _vm: &VirtualMachine) -> Option { if !isatty(fd) { - return Ok(None); + return None; } cfg_select! { any(target_os = "android", target_os = "redox") => { - Ok(Some("UTF-8".to_owned())) + Some("UTF-8".to_owned()) } windows => { use windows_sys::Win32::System::Console; @@ -2124,19 +2127,17 @@ pub(super) mod _os { _ => 0, }; - Ok(Some(format!("cp{cp}"))) + Some(format!("cp{cp}")) } _ => { - let encoding = unsafe { + Some(unsafe { let encoding = libc::nl_langinfo(libc::CODESET); if encoding.is_null() || encoding.read() == b'\0' as libc::c_char { "UTF-8".to_owned() } else { core::ffi::CStr::from_ptr(encoding).to_string_lossy().into_owned() } - }; - - Ok(Some(encoding)) + }) } } } From 66830b08d72860807a0d07f360d0ff439b31d41a Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 17:22:51 +0300 Subject: [PATCH 21/66] typevar --- crates/vm/src/stdlib/typevar.rs | 62 +++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/crates/vm/src/stdlib/typevar.rs b/crates/vm/src/stdlib/typevar.rs index b28fad21bd..260cd8eaaf 100644 --- a/crates/vm/src/stdlib/typevar.rs +++ b/crates/vm/src/stdlib/typevar.rs @@ -173,40 +173,46 @@ pub(crate) mod typevar { } #[pygetset] - fn evaluate_bound(&self, vm: &VirtualMachine) -> PyResult { + fn evaluate_bound(&self, vm: &VirtualMachine) -> PyObjectRef { if !vm.is_none(&self.evaluate_bound) { - return Ok(self.evaluate_bound.clone()); + return self.evaluate_bound.clone(); } + let bound = self.bound.lock(); if !vm.is_none(&bound) { - return Ok(const_evaluator_alloc(bound.clone(), vm)); + return const_evaluator_alloc(bound.clone(), vm); } - Ok(vm.ctx.none()) + + vm.ctx.none() } #[pygetset] - fn evaluate_constraints(&self, vm: &VirtualMachine) -> PyResult { + fn evaluate_constraints(&self, vm: &VirtualMachine) -> PyObjectRef { if !vm.is_none(&self.evaluate_constraints) { - return Ok(self.evaluate_constraints.clone()); + return self.evaluate_constraints.clone(); } + let constraints = self.constraints.lock(); if !vm.is_none(&constraints) { - return Ok(const_evaluator_alloc(constraints.clone(), vm)); + return const_evaluator_alloc(constraints.clone(), vm); } - Ok(vm.ctx.none()) + + vm.ctx.none() } #[pygetset] - fn evaluate_default(&self, vm: &VirtualMachine) -> PyResult { + fn evaluate_default(&self, vm: &VirtualMachine) -> PyObjectRef { let evaluator = self.evaluate_default.lock().clone(); if !vm.is_none(&evaluator) { - return Ok(evaluator); + return evaluator; } + let default_value = self.default_value.lock().clone(); if !default_value.is(&vm.ctx.typing_no_default) { - return Ok(const_evaluator_alloc(default_value, vm)); + return const_evaluator_alloc(default_value, vm); } - Ok(vm.ctx.none()) + + vm.ctx.none() } #[pymethod] @@ -480,21 +486,21 @@ pub(crate) mod typevar { } #[pygetset] - fn args(zelf: crate::PyRef, vm: &VirtualMachine) -> PyResult { + fn args(zelf: crate::PyRef, vm: &VirtualMachine) -> PyObjectRef { let self_obj: PyObjectRef = zelf.into(); let psa = ParamSpecArgs { __origin__: self_obj, }; - Ok(psa.into_ref(&vm.ctx).into()) + psa.into_ref(&vm.ctx).into() } #[pygetset] - fn kwargs(zelf: crate::PyRef, vm: &VirtualMachine) -> PyResult { + fn kwargs(zelf: crate::PyRef, vm: &VirtualMachine) -> PyObjectRef { let self_obj: PyObjectRef = zelf.into(); let psk = ParamSpecKwargs { __origin__: self_obj, }; - Ok(psk.into_ref(&vm.ctx).into()) + psk.into_ref(&vm.ctx).into() } #[pygetset] @@ -539,21 +545,23 @@ pub(crate) mod typevar { } #[pygetset] - fn evaluate_default(&self, vm: &VirtualMachine) -> PyResult { + fn evaluate_default(&self, vm: &VirtualMachine) -> PyObjectRef { let evaluator = self.evaluate_default.lock().clone(); if !vm.is_none(&evaluator) { - return Ok(evaluator); + return evaluator; } + let default_value = self.default_value.lock().clone(); if !default_value.is(&vm.ctx.typing_no_default) { - return Ok(const_evaluator_alloc(default_value, vm)); + return const_evaluator_alloc(default_value, vm); } - Ok(vm.ctx.none()) + + vm.ctx.none() } #[pymethod] - fn __reduce__(&self) -> PyResult { - Ok(self.name.clone()) + fn __reduce__(&self) -> PyObjectRef { + self.name.clone() } #[pymethod] @@ -748,16 +756,18 @@ pub(crate) mod typevar { } #[pygetset] - fn evaluate_default(&self, vm: &VirtualMachine) -> PyResult { + fn evaluate_default(&self, vm: &VirtualMachine) -> PyObjectRef { let evaluator = self.evaluate_default.lock().clone(); if !vm.is_none(&evaluator) { - return Ok(evaluator); + return evaluator; } + let default_value = self.default_value.lock().clone(); if !default_value.is(&vm.ctx.typing_no_default) { - return Ok(const_evaluator_alloc(default_value, vm)); + return const_evaluator_alloc(default_value, vm); } - Ok(vm.ctx.none()) + + vm.ctx.none() } #[pymethod] From 70f35ecd075b7ac5a141f6371efb3af8189a02b4 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 17:26:13 +0300 Subject: [PATCH 22/66] time --- crates/vm/src/stdlib/time.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/vm/src/stdlib/time.rs b/crates/vm/src/stdlib/time.rs index 1c7f1f6b32..31e41a89b0 100644 --- a/crates/vm/src/stdlib/time.rs +++ b/crates/vm/src/stdlib/time.rs @@ -1002,6 +1002,7 @@ pub mod decl { #[allow(unused_imports)] use super::platform::*; + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn module_exec( vm: &VirtualMachine, module: &Py, From 871438f5ca025c47186fb6a7df2ecbce39f4ca04 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 17:27:37 +0300 Subject: [PATCH 23/66] itertools --- crates/vm/src/stdlib/itertools.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/vm/src/stdlib/itertools.rs b/crates/vm/src/stdlib/itertools.rs index 0566db1f61..f3dbf0ceb8 100644 --- a/crates/vm/src/stdlib/itertools.rs +++ b/crates/vm/src/stdlib/itertools.rs @@ -956,12 +956,12 @@ mod decl { } impl PyItertoolsTeeData { - fn new(iterable: PyIter, _vm: &VirtualMachine) -> PyResult> { - Ok(PyRc::new(Self { + fn new(iterable: PyIter, _vm: &VirtualMachine) -> PyRc { + PyRc::new(Self { iterable, values: PyMutex::new(vec![]), running: AtomicBool::new(false), - })) + }) } fn get_item(&self, vm: &VirtualMachine, index: usize) -> PyResult { @@ -1043,7 +1043,7 @@ mod decl { return vm.call_special_method(&iterator, identifier!(vm, __copy__), ()); } Ok(Self { - tee_data: PyItertoolsTeeData::new(iterator, vm)?, + tee_data: PyItertoolsTeeData::new(iterator, vm), index: AtomicCell::new(0), } .into_ref_with_type(vm, class.to_owned())? From 98632669bab89e5f115ef3c11d61b5b83dfd07c4 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 17:28:52 +0300 Subject: [PATCH 24/66] builtins --- crates/vm/src/stdlib/builtins.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/vm/src/stdlib/builtins.rs b/crates/vm/src/stdlib/builtins.rs index bf047ade30..f39ae2d1e0 100644 --- a/crates/vm/src/stdlib/builtins.rs +++ b/crates/vm/src/stdlib/builtins.rs @@ -928,7 +928,7 @@ mod builtins { } #[pyfunction] - fn oct(number: ArgIndex, vm: &VirtualMachine) -> PyResult { + fn oct(number: ArgIndex, vm: &VirtualMachine) -> PyObjectRef { let number = number.into_int_ref(); let n = number.as_bigint(); let s = if n.is_negative() { @@ -937,7 +937,7 @@ mod builtins { format!("0o{n:o}") }; - Ok(vm.ctx.new_str(s).into()) + vm.ctx.new_str(s).into() } #[pyfunction] From 61b312ce507c51bb33226cbc05b4173ab681d226 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 17:31:16 +0300 Subject: [PATCH 25/66] _typing --- crates/vm/src/stdlib/_typing.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/vm/src/stdlib/_typing.rs b/crates/vm/src/stdlib/_typing.rs index 8fae71a106..5e1426b886 100644 --- a/crates/vm/src/stdlib/_typing.rs +++ b/crates/vm/src/stdlib/_typing.rs @@ -44,13 +44,13 @@ pub(crate) mod decl { } #[pyfunction(name = "override")] - pub(crate) fn r#override(func: PyObjectRef, vm: &VirtualMachine) -> PyResult { + pub(crate) fn r#override(func: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { // Set __override__ attribute to True // Skip the attribute silently if it is not writable. // AttributeError happens if the object has __slots__ or a // read-only property, TypeError if it's a builtin class. let _ = func.set_attr("__override__", vm.ctx.true_value.clone(), vm); - Ok(func) + func } #[pyclass(no_attr, name = "NoDefaultType", module = "typing")] @@ -302,11 +302,11 @@ pub(crate) mod decl { } #[pygetset] - fn evaluate_value(&self, vm: &VirtualMachine) -> PyResult { + fn evaluate_value(&self, vm: &VirtualMachine) -> PyObjectRef { if self.is_lazy { - return Ok(self.compute_value.clone()); + return self.compute_value.clone(); } - Ok(const_evaluator_alloc(self.compute_value.clone(), vm)) + const_evaluator_alloc(self.compute_value.clone(), vm) } /// Check type_params ordering: non-default params must precede default params. @@ -492,6 +492,7 @@ pub(crate) mod decl { Ok(PyTuple::new_ref(new_params, &vm.ctx)) } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn module_exec( vm: &VirtualMachine, module: &Py, From 7138481b08ff676e79af48e971db309973eb2ff3 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 17:34:21 +0300 Subject: [PATCH 26/66] _symtable --- crates/vm/src/stdlib/_symtable.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/crates/vm/src/stdlib/_symtable.rs b/crates/vm/src/stdlib/_symtable.rs index 9b3c38c93e..4945cbeedc 100644 --- a/crates/vm/src/stdlib/_symtable.rs +++ b/crates/vm/src/stdlib/_symtable.rs @@ -172,9 +172,8 @@ mod _symtable { } #[pygetset] - fn children(&self, vm: &VirtualMachine) -> PyResult> { - let children = self - .symtable + fn children(&self, vm: &VirtualMachine) -> Vec { + self.symtable .sub_tables .iter() .flat_map(|t| { @@ -186,8 +185,7 @@ mod _symtable { } }) .map(|t| to_py_symbol_table(t.clone()).into_pyobject(vm)) - .collect(); - Ok(children) + .collect() } #[pygetset] @@ -196,24 +194,22 @@ mod _symtable { } #[pygetset] - fn identifiers(&self, vm: &VirtualMachine) -> PyResult> { - let symbols = self - .symtable + fn identifiers(&self, vm: &VirtualMachine) -> Vec { + self.symtable .symbols .keys() .map(|s| vm.ctx.new_str(s.as_str()).into()) - .collect(); - Ok(symbols) + .collect() } #[pygetset] - fn symbols(&self, vm: &VirtualMachine) -> PyResult { + fn symbols(&self, vm: &VirtualMachine) -> PyDictRef { let dict = vm.ctx.new_dict(); for (name, symbol) in &self.symtable.symbols { dict.set_item(name, vm.new_pyobj(symbol.flags.bits()), vm) .unwrap(); } - Ok(dict) + dict } #[pygetset] @@ -319,13 +315,11 @@ mod _symtable { } #[pymethod] - fn get_namespaces(&self, vm: &VirtualMachine) -> PyResult> { - let namespaces = self - .namespaces + fn get_namespaces(&self, vm: &VirtualMachine) -> Vec { + self.namespaces .iter() .map(|table| to_py_symbol_table(table.clone()).into_pyobject(vm)) - .collect(); - Ok(namespaces) + .collect() } #[pymethod] From f845619e9cdf9d3135ba81204eb822b9a90be7da Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 10:49:31 +0300 Subject: [PATCH 27/66] _operator --- crates/vm/src/stdlib/_operator.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/vm/src/stdlib/_operator.rs b/crates/vm/src/stdlib/_operator.rs index 38b0f715d3..6d7de468b4 100644 --- a/crates/vm/src/stdlib/_operator.rs +++ b/crates/vm/src/stdlib/_operator.rs @@ -54,13 +54,13 @@ mod _operator { } #[pyfunction] - fn is_(a: PyObjectRef, b: PyObjectRef) -> PyResult { - Ok(a.is(&b)) + fn is_(a: PyObjectRef, b: PyObjectRef) -> bool { + a.is(&b) } #[pyfunction] - fn is_not(a: PyObjectRef, b: PyObjectRef) -> PyResult { - Ok(!a.is(&b)) + fn is_not(a: PyObjectRef, b: PyObjectRef) -> bool { + !a.is(&b) } #[pyfunction] @@ -363,11 +363,11 @@ mod _operator { } #[pymethod] - fn __reduce__(zelf: PyRef, vm: &VirtualMachine) -> PyResult<(PyTypeRef, PyTupleRef)> { + fn __reduce__(zelf: PyRef, vm: &VirtualMachine) -> (PyTypeRef, PyTupleRef) { let attrs = vm .ctx .new_tuple(zelf.attrs.iter().map(|v| v.clone().into()).collect()); - Ok((zelf.class().to_owned(), attrs)) + (zelf.class().to_owned(), attrs) } // Go through dotted parts of string and call getattr on whatever is returned. From e1f5344a685dad6b9f27cdf333480de5eb10f9e3 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 10:50:25 +0300 Subject: [PATCH 28/66] _io --- crates/vm/src/stdlib/_io.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/crates/vm/src/stdlib/_io.rs b/crates/vm/src/stdlib/_io.rs index 3eb4985e12..9115d2dd10 100644 --- a/crates/vm/src/stdlib/_io.rs +++ b/crates/vm/src/stdlib/_io.rs @@ -5953,13 +5953,8 @@ mod fileio { /// fileio_dealloc_warn in Modules/_io/fileio.c #[pymethod(name = "_dealloc_warn")] - fn _dealloc_warn_method( - zelf: &Py, - source: PyObjectRef, - vm: &VirtualMachine, - ) -> PyResult<()> { + fn _dealloc_warn_method(zelf: &Py, source: PyObjectRef, vm: &VirtualMachine) { Self::dealloc_warn(zelf, source, vm); - Ok(()) } } From c315d9aff11e81eef2c5b78c8feafb40817e0416 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 10:52:23 +0300 Subject: [PATCH 29/66] _imp --- crates/vm/src/stdlib/_imp.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crates/vm/src/stdlib/_imp.rs b/crates/vm/src/stdlib/_imp.rs index b0c40ba440..216921e1c5 100644 --- a/crates/vm/src/stdlib/_imp.rs +++ b/crates/vm/src/stdlib/_imp.rs @@ -183,8 +183,8 @@ mod _imp { use version::PYC_MAGIC_NUMBER_TOKEN; #[pyfunction] - const fn extension_suffixes() -> PyResult> { - Ok(Vec::new()) + const fn extension_suffixes() -> Vec { + Vec::new() } #[pyfunction] @@ -297,14 +297,12 @@ mod _imp { } #[pyfunction] - fn _frozen_module_names(vm: &VirtualMachine) -> PyResult> { - let names = vm - .state + fn _frozen_module_names(vm: &VirtualMachine) -> Vec { + vm.state .frozen .keys() .map(|&name| vm.ctx.new_utf8_str(name).into()) - .collect(); - Ok(names) + .collect() } #[allow(clippy::type_complexity)] From f3edb2a9edb167d105374b3820106a878e5c9a3e Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 10:53:36 +0300 Subject: [PATCH 30/66] _functools --- crates/vm/src/stdlib/_functools.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/vm/src/stdlib/_functools.rs b/crates/vm/src/stdlib/_functools.rs index b36fbbcddb..cd0c40de09 100644 --- a/crates/vm/src/stdlib/_functools.rs +++ b/crates/vm/src/stdlib/_functools.rs @@ -177,7 +177,7 @@ mod _functools { } #[pymethod] - fn __reduce__(zelf: &Py, vm: &VirtualMachine) -> PyResult { + fn __reduce__(zelf: &Py, vm: &VirtualMachine) -> PyObjectRef { let inner = zelf.inner.read(); let partial_type = zelf.class(); @@ -193,14 +193,13 @@ mod _functools { inner.keywords.clone().into(), dict_obj, ]); - Ok(vm - .ctx + vm.ctx .new_tuple(vec![ partial_type.to_owned().into(), vm.ctx.new_tuple(vec![inner.func.clone()]).into(), state.into(), ]) - .into()) + .into() } #[pymethod] From 55510510594950a10918e8c031ebd979784abe80 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 10:55:51 +0300 Subject: [PATCH 31/66] _collections --- crates/vm/src/stdlib/_collections.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/vm/src/stdlib/_collections.rs b/crates/vm/src/stdlib/_collections.rs index 03438f28c0..e5127431c8 100644 --- a/crates/vm/src/stdlib/_collections.rs +++ b/crates/vm/src/stdlib/_collections.rs @@ -251,11 +251,11 @@ mod _collections { } #[pymethod] - fn __reversed__(zelf: PyRef) -> PyResult { - Ok(PyReverseDequeIterator { + fn __reversed__(zelf: PyRef) -> PyReverseDequeIterator { + PyReverseDequeIterator { state: zelf.state.load(), internal: PyMutex::new(PositionIterInternal::new(zelf, 0)), - }) + } } #[pymethod] @@ -688,7 +688,7 @@ mod _collections { (DequeIterArgs { deque, index }, _kwargs): Self::Args, _vm: &VirtualMachine, ) -> PyResult { - let iter = PyDeque::__reversed__(deque)?; + let iter = PyDeque::__reversed__(deque); if let OptionalArg::Present(index) = index { let index = max(index, 0) as usize; iter.internal.lock().position = index; @@ -708,20 +708,21 @@ mod _collections { fn __reduce__( zelf: PyRef, vm: &VirtualMachine, - ) -> PyResult<(PyTypeRef, (PyDequeRef, PyObjectRef))> { + ) -> (PyTypeRef, (PyDequeRef, PyObjectRef)) { let internal = zelf.internal.lock(); let deque = match &internal.status { Active(obj) => obj.clone(), Exhausted => PyDeque::default().into_ref(&vm.ctx), }; - Ok(( + ( zelf.class().to_owned(), (deque, vm.ctx.new_int(internal.position).into()), - )) + ) } } impl SelfIter for PyReverseDequeIterator {} + impl IterNext for PyReverseDequeIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|deque, pos| { From 35affc4df8e46011f2ca0f0d8da131bcf0ec4344 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 10:58:48 +0300 Subject: [PATCH 32/66] _ast --- crates/vm/src/stdlib/_ast.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/vm/src/stdlib/_ast.rs b/crates/vm/src/stdlib/_ast.rs index 8307d0bae6..9065d76fdd 100644 --- a/crates/vm/src/stdlib/_ast.rs +++ b/crates/vm/src/stdlib/_ast.rs @@ -414,7 +414,7 @@ pub(crate) fn parse( }; let obj = top.ast_to_object(vm, &source_file); if type_comments && obj.class().is(pyast::NodeModModule::static_type()) { - let type_ignores = type_ignores_from_source(vm, source)?; + let type_ignores = type_ignores_from_source(vm, source); let dict = obj.as_object().dict().unwrap(); dict.set_item("type_ignores", vm.ctx.new_list(type_ignores).into(), vm) .unwrap(); @@ -512,10 +512,7 @@ pub(crate) fn parse_func_type( Ok(func_type.ast_to_object(vm, &source_file)) } -fn type_ignores_from_source( - vm: &VirtualMachine, - source: &str, -) -> Result, CompileError> { +fn type_ignores_from_source(vm: &VirtualMachine, source: &str) -> Vec { let mut ignores = Vec::new(); for (idx, line) in source.lines().enumerate() { let Some(pos) = line.find("#") else { @@ -542,7 +539,7 @@ fn type_ignores_from_source( .unwrap(); ignores.push(node.into()); } - Ok(ignores) + ignores } #[cfg(feature = "parser")] From 4c57a30fc75d44abd3656687dbba7564f2b9d01e Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:01:33 +0300 Subject: [PATCH 33/66] frame.rs --- crates/vm/src/frame.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/vm/src/frame.rs b/crates/vm/src/frame.rs index f95946e03f..3e5ca27ad4 100644 --- a/crates/vm/src/frame.rs +++ b/crates/vm/src/frame.rs @@ -2194,7 +2194,7 @@ impl ExecutingFrame<'_> { self.push_value(set.into()); Ok(None) } - Instruction::BuildSlice { argc } => self.execute_build_slice(vm, argc.get(arg)), + Instruction::BuildSlice { argc } => Ok(self.execute_build_slice(vm, argc.get(arg))), /* Instruction::ToBool => { dbg!("Shouldn't be called outside of match statements for now") @@ -6471,7 +6471,7 @@ impl ExecutingFrame<'_> { &mut self, vm: &VirtualMachine, argc: bytecode::BuildSliceArgCount, - ) -> FrameResult { + ) -> Option { let step = match argc { bytecode::BuildSliceArgCount::Two => None, bytecode::BuildSliceArgCount::Three => Some(self.pop_value()), @@ -6486,7 +6486,7 @@ impl ExecutingFrame<'_> { } .into_ref(&vm.ctx); self.push_value(obj.into()); - Ok(None) + None } fn collect_positional_args(&mut self, nargs: u32) -> FuncArgs { From 788c0da2e01395f8fc29e1258448952c3918f5d1 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:20:01 +0300 Subject: [PATCH 34/66] exceptions --- crates/vm/src/exceptions.rs | 41 ++++++++++++++++--------------------- crates/vm/src/stdlib/_io.rs | 9 +------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/crates/vm/src/exceptions.rs b/crates/vm/src/exceptions.rs index 538b60c6d4..8b6bb8a325 100644 --- a/crates/vm/src/exceptions.rs +++ b/crates/vm/src/exceptions.rs @@ -674,22 +674,22 @@ impl PyBaseException { #[pyclass] impl Py { #[pymethod] - pub(super) fn __str__(&self, vm: &VirtualMachine) -> PyResult { + pub(super) fn __str__(&self, vm: &VirtualMachine) -> PyStrRef { let str_args = vm.exception_args_as_string(self.args(), true); - Ok(match str_args.into_iter().exactly_one() { + match str_args.into_iter().exactly_one() { Err(i) if i.len() == 0 => vm.ctx.empty_str.to_owned(), Ok(s) => s, Err(i) => PyStr::from(format!("({})", i.format(", "))).into_ref(&vm.ctx), - }) + } } } #[pyclass] impl PyRef { #[pymethod] - fn with_traceback(self, tb: Option) -> PyResult { + fn with_traceback(self, tb: Option) -> Self { *self.traceback.write() = tb; - Ok(self) + self } #[pymethod] @@ -1092,11 +1092,7 @@ fn make_arg_getter(idx: usize) -> impl Fn(PyBaseExceptionRef) -> Option PyResult<()> { +fn syntax_error_set_msg(exc: PyBaseExceptionRef, value: PySetterValue, vm: &VirtualMachine) { let mut args = exc.args.write(); let mut new_args = args.as_slice().to_vec(); // Ensure the message slot at index 0 always exists for SyntaxError.args. @@ -1108,7 +1104,6 @@ fn syntax_error_set_msg( PySetterValue::Delete => new_args[0] = vm.ctx.none(), } *args = PyTuple::new_ref(new_args, &vm.ctx); - Ok(()) } fn system_exit_code(exc: PyBaseExceptionRef) -> Option { @@ -1117,11 +1112,11 @@ fn system_exit_code(exc: PyBaseExceptionRef) -> Option { // - size == 1: code is args[0] // - size > 1: code is args (the whole tuple) let args = exc.args.read(); - match args.len() { - 0 => None, - 1 => Some(args.first().unwrap().clone()), - _ => Some(args.as_object().to_owned()), - } + Some(match args.len() { + 0 => return None, + 1 => args.first().unwrap().clone(), + _ => args.as_object().to_owned(), + }) } #[cfg(feature = "serde")] @@ -1712,16 +1707,16 @@ pub(super) mod types { #[pyexception] impl PyKeyError { #[pymethod] - fn __str__(zelf: &Py, vm: &VirtualMachine) -> PyResult { + fn __str__(zelf: &Py, vm: &VirtualMachine) -> PyStrRef { let args = zelf.args(); - Ok(if args.len() == 1 { + if args.len() == 1 { vm.exception_args_as_string(args, false) .into_iter() .exactly_one() .unwrap() } else { - zelf.__str__(vm)? - }) + zelf.__str__(vm) + } } } @@ -2065,7 +2060,7 @@ pub(super) mod types { } // fallback to BaseException.__str__ - zelf.__str__(vm) + Ok(zelf.__str__(vm)) } #[pymethod] @@ -2334,7 +2329,7 @@ pub(super) mod types { #[pyexception(with(Initializer))] impl PySyntaxError { #[pymethod] - fn __str__(zelf: &Py, vm: &VirtualMachine) -> PyResult { + fn __str__(zelf: &Py, vm: &VirtualMachine) -> PyStrRef { fn basename(filename: &Wtf8) -> &Wtf8 { let bytes = filename.as_bytes(); let pos = if cfg!(windows) { @@ -2387,7 +2382,7 @@ pub(super) mod types { (None, None) => msg.as_wtf8().to_owned(), }; - Ok(vm.ctx.new_str(msg_with_location_info)) + vm.ctx.new_str(msg_with_location_info) } } diff --git a/crates/vm/src/stdlib/_io.rs b/crates/vm/src/stdlib/_io.rs index 9115d2dd10..4f541e5e82 100644 --- a/crates/vm/src/stdlib/_io.rs +++ b/crates/vm/src/stdlib/_io.rs @@ -1591,14 +1591,7 @@ mod _io { let zelf: PyRef = zelf.try_into_value(vm)?; let (raw, BufferSize { buffer_size }): (PyObjectRef, _) = args.bind(vm).map_err(|e| { - let str_repr = e - .__str__(vm) - .as_ref() - .map_or_else( - |_| "".as_ref(), - |s| s.as_wtf8(), - ) - .to_owned(); + let str_repr = e.__str__(vm).as_wtf8().to_owned(); let msg = format!("{}() {}", Self::CLASS_NAME, str_repr); vm.new_exception_msg(e.class().to_owned(), msg.into()) })?; From a23b6230492482d1b1657d3938af934a6a3b6133 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:24:21 +0300 Subject: [PATCH 35/66] codecs --- crates/vm/src/codecs.rs | 11 +++++------ crates/vm/src/stdlib/_codecs.rs | 4 ++-- crates/vm/src/vm/mod.rs | 8 ++++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/crates/vm/src/codecs.rs b/crates/vm/src/codecs.rs index b152586916..03340d423a 100644 --- a/crates/vm/src/codecs.rs +++ b/crates/vm/src/codecs.rs @@ -214,31 +214,30 @@ impl CodecsRegistry { Ok(()) } - pub fn unregister(&self, search_function: PyObjectRef) -> PyResult<()> { + pub fn unregister(&self, search_function: PyObjectRef) { let mut inner = self.inner.write(); // Do nothing if search_path is not created yet or was cleared. if inner.search_path.is_empty() { - return Ok(()); + return; } + for (i, item) in inner.search_path.iter().enumerate() { if item.get_id() == search_function.get_id() { if !inner.search_cache.is_empty() { inner.search_cache.clear(); } inner.search_path.remove(i); - return Ok(()); + return; } } - Ok(()) } - pub(crate) fn register_manual(&self, name: &str, codec: PyCodec) -> PyResult<()> { + pub(crate) fn register_manual(&self, name: &str, codec: PyCodec) { let name = normalize_encoding_name(name); self.inner .write() .search_cache .insert(name.into_owned(), codec); - Ok(()) } pub fn lookup(&self, encoding: &str, vm: &VirtualMachine) -> PyResult { diff --git a/crates/vm/src/stdlib/_codecs.rs b/crates/vm/src/stdlib/_codecs.rs index adab191509..a91b490300 100644 --- a/crates/vm/src/stdlib/_codecs.rs +++ b/crates/vm/src/stdlib/_codecs.rs @@ -23,8 +23,8 @@ mod _codecs { } #[pyfunction] - fn unregister(search_function: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { - vm.state.codec_registry.unregister(search_function) + fn unregister(search_function: PyObjectRef, vm: &VirtualMachine) { + vm.state.codec_registry.unregister(search_function); } #[pyfunction] diff --git a/crates/vm/src/vm/mod.rs b/crates/vm/src/vm/mod.rs index 3cfe464290..194bc59776 100644 --- a/crates/vm/src/vm/mod.rs +++ b/crates/vm/src/vm/mod.rs @@ -846,7 +846,7 @@ impl VirtualMachine { let codec_info = getregentry.call((), self)?; self.state .codec_registry - .register_manual("ascii", codec_info.try_into_value(self)?)?; + .register_manual("ascii", codec_info.try_into_value(self)?); // Register utf-8 encoding (also as "utf8" alias since normalize_encoding_name // maps "utf-8" → "utf_8" but leaves "utf8" as-is) @@ -857,10 +857,10 @@ impl VirtualMachine { let utf8_codec: crate::codecs::PyCodec = codec_info.try_into_value(self)?; self.state .codec_registry - .register_manual("utf-8", utf8_codec.clone())?; + .register_manual("utf-8", utf8_codec.clone()); self.state .codec_registry - .register_manual("utf8", utf8_codec)?; + .register_manual("utf8", utf8_codec); // Register latin-1 / iso8859-1 aliases needed very early for stdio // bootstrap (e.g. PYTHONIOENCODING=latin-1). @@ -873,7 +873,7 @@ impl VirtualMachine { for name in ["latin-1", "latin_1", "latin1", "iso8859-1", "iso8859_1"] { self.state .codec_registry - .register_manual(name, latin1_codec.clone())?; + .register_manual(name, latin1_codec.clone()); } } Ok(()) From ea7a0ad21c6ef8739e1aaf44be17f232f4c4a0f4 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:26:52 +0300 Subject: [PATCH 36/66] zip --- crates/vm/src/builtins/zip.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/vm/src/builtins/zip.rs b/crates/vm/src/builtins/zip.rs index b090cd424d..1a74639000 100644 --- a/crates/vm/src/builtins/zip.rs +++ b/crates/vm/src/builtins/zip.rs @@ -47,7 +47,7 @@ impl Constructor for PyZip { #[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyZip { #[pymethod] - fn __reduce__(zelf: PyRef, vm: &VirtualMachine) -> PyResult { + fn __reduce__(zelf: PyRef, vm: &VirtualMachine) -> PyTupleRef { let cls = zelf.class().to_owned(); let iterators = zelf .iterators @@ -55,23 +55,23 @@ impl PyZip { .map(|obj| obj.clone().into()) .collect::>(); let tuple_iter = vm.ctx.new_tuple(iterators); - Ok(if zelf.strict.load(atomic::Ordering::Acquire) { + if zelf.strict.load(atomic::Ordering::Acquire) { vm.new_tuple((cls, tuple_iter, true)) } else { vm.new_tuple((cls, tuple_iter)) - }) + } } #[pymethod] - fn __setstate__(zelf: PyRef, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + fn __setstate__(zelf: PyRef, state: PyObjectRef, vm: &VirtualMachine) { if let Ok(obj) = ArgIntoBool::try_from_object(vm, state) { zelf.strict.store(obj.into(), atomic::Ordering::Release); } - Ok(()) } } impl SelfIter for PyZip {} + impl IterNext for PyZip { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { if zelf.iterators.is_empty() { From 5518f2a51278f278f04f1194c678361555fc757c Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:27:48 +0300 Subject: [PATCH 37/66] slice --- crates/vm/src/builtins/slice.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/vm/src/builtins/slice.rs b/crates/vm/src/builtins/slice.rs index 96fb3fb5a5..3c5f13b382 100644 --- a/crates/vm/src/builtins/slice.rs +++ b/crates/vm/src/builtins/slice.rs @@ -248,14 +248,14 @@ impl PySlice { #[pymethod] fn __reduce__( zelf: PyRef, - ) -> PyResult<( + ) -> ( PyTypeRef, (Option, PyObjectRef, Option), - )> { - Ok(( + ) { + ( zelf.class().to_owned(), (zelf.start.clone(), zelf.stop.clone(), zelf.step.clone()), - )) + ) } // TODO: Uncomment when Python adds __class_getitem__ to slice From 28eae26ab1cd0e832698adf09f9cb7631239eecd Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:30:34 +0300 Subject: [PATCH 38/66] set --- crates/vm/src/builtins/set.rs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/crates/vm/src/builtins/set.rs b/crates/vm/src/builtins/set.rs index 22959a856b..064521e7ce 100644 --- a/crates/vm/src/builtins/set.rs +++ b/crates/vm/src/builtins/set.rs @@ -509,18 +509,15 @@ fn extract_set(obj: &PyObject) -> Option<&PySetInner> { }) } -fn reduce_set( - zelf: &PyObject, - vm: &VirtualMachine, -) -> PyResult<(PyTypeRef, PyTupleRef, Option)> { - Ok(( +fn reduce_set(zelf: &PyObject, vm: &VirtualMachine) -> (PyTypeRef, PyTupleRef, Option) { + ( zelf.class().to_owned(), #[expect(clippy::or_fun_call, reason = "changing this won't compile")] vm.new_tuple((extract_set(zelf) .unwrap_or(&PySetInner::default()) .elements(),)), zelf.dict(), - )) + ) } #[pyclass( @@ -766,7 +763,7 @@ impl PySet { fn __reduce__( zelf: PyRef, vm: &VirtualMachine, - ) -> PyResult<(PyTypeRef, PyTupleRef, Option)> { + ) -> (PyTypeRef, PyTupleRef, Option) { reduce_set(zelf.as_ref(), vm) } @@ -1140,7 +1137,7 @@ impl PyFrozenSet { fn __reduce__( zelf: PyRef, vm: &VirtualMachine, - ) -> PyResult<(PyTypeRef, PyTupleRef, Option)> { + ) -> (PyTypeRef, PyTupleRef, Option) { reduce_set(zelf.as_ref(), vm) } @@ -1362,12 +1359,9 @@ impl PySetIterator { } #[pymethod] - fn __reduce__( - zelf: PyRef, - vm: &VirtualMachine, - ) -> PyResult<(PyObjectRef, (PyObjectRef,))> { + fn __reduce__(zelf: PyRef, vm: &VirtualMachine) -> (PyObjectRef, (PyObjectRef,)) { let internal = zelf.internal.lock(); - Ok(( + ( builtins_iter(vm), (vm.ctx .new_list(match &internal.status { @@ -1377,7 +1371,7 @@ impl PySetIterator { } }) .into(),), - )) + ) } } From fb0eb31e0340025ffc9f63d511b49c243281f84a Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:40:08 +0300 Subject: [PATCH 39/66] range --- crates/vm/src/builtins/range.rs | 54 ++++++++++++++++----------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/crates/vm/src/builtins/range.rs b/crates/vm/src/builtins/range.rs index fb2ef07c8e..ef141cdd69 100644 --- a/crates/vm/src/builtins/range.rs +++ b/crates/vm/src/builtins/range.rs @@ -275,7 +275,7 @@ impl PyRange { } #[pymethod] - fn __reversed__(&self, vm: &VirtualMachine) -> PyResult { + fn __reversed__(&self, vm: &VirtualMachine) -> PyObjectRef { let start = self.start.as_bigint(); let step = self.step.as_bigint(); @@ -285,29 +285,27 @@ impl PyRange { let start = &new_stop + length.clone() * step; let step = -step; - Ok( - if let (Some(start), Some(step), Some(_)) = - (start.to_isize(), step.to_isize(), new_stop.to_isize()) - { - PyRangeIterator { - index: AtomicCell::new(0), - start, - step, - // Cannot fail. If start, stop and step all successfully convert to isize, then result of zelf.len will - // always fit in a usize. - length: length.to_usize().unwrap_or(0), - } - .into_pyobject(vm) - } else { - PyLongRangeIterator { - index: AtomicCell::new(0), - start, - step, - length, - } - .into_pyobject(vm) - }, - ) + if let (Some(start), Some(step), Some(_)) = + (start.to_isize(), step.to_isize(), new_stop.to_isize()) + { + PyRangeIterator { + index: AtomicCell::new(0), + start, + step, + // Cannot fail. If start, stop and step all successfully convert to isize, then result of zelf.len will + // always fit in a usize. + length: length.to_usize().unwrap_or(0), + } + .into_pyobject(vm) + } else { + PyLongRangeIterator { + index: AtomicCell::new(0), + start, + step, + length, + } + .into_pyobject(vm) + } } fn __len__(&self) -> BigInt { @@ -618,7 +616,7 @@ impl PyLongRangeIterator { } #[pymethod] - fn __reduce__(&self, vm: &VirtualMachine) -> PyResult { + fn __reduce__(&self, vm: &VirtualMachine) -> PyTupleRef { range_iter_reduce( self.start.clone(), self.length.clone(), @@ -680,7 +678,7 @@ impl PyRangeIterator { } #[pymethod] - fn __reduce__(&self, vm: &VirtualMachine) -> PyResult { + fn __reduce__(&self, vm: &VirtualMachine) -> PyTupleRef { range_iter_reduce( BigInt::from(self.start), BigInt::from(self.length), @@ -721,7 +719,7 @@ fn range_iter_reduce( step: BigInt, index: usize, vm: &VirtualMachine, -) -> PyResult { +) -> PyTupleRef { let iter = builtins_iter(vm); let stop = start.clone() + length * step.clone(); let range = PyRange { @@ -729,7 +727,7 @@ fn range_iter_reduce( stop: PyInt::from(stop).into_ref(&vm.ctx), step: PyInt::from(step).into_ref(&vm.ctx), }; - Ok(vm.new_tuple((iter, (range,), index))) + vm.new_tuple((iter, (range,), index)) } // Silently clips state (i.e index) in range [0, usize::MAX]. From 8f3d7a4f59baf21687ecb51ee440d3a6341e1145 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:44:10 +0300 Subject: [PATCH 40/66] type --- crates/vm/src/builtins/type.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/vm/src/builtins/type.rs b/crates/vm/src/builtins/type.rs index 813b47061a..91deeb7b3f 100644 --- a/crates/vm/src/builtins/type.rs +++ b/crates/vm/src/builtins/type.rs @@ -2667,10 +2667,10 @@ fn subtype_set_dict(obj: PyObjectRef, value: PySetterValue, vm: &VirtualMachine) } // subtype_get_weakref -fn subtype_get_weakref(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { +fn subtype_get_weakref(obj: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { // Return the first weakref in the weakref list, or None let weakref = obj.get_weakrefs(); - Ok(weakref.unwrap_or_else(|| vm.ctx.none())) + weakref.unwrap_or_else(|| vm.ctx.none()) } // subtype_set_weakref: __weakref__ is read-only From 77d76bb40a297120bdb8d6f7c64f6282a7b07246 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:45:21 +0300 Subject: [PATCH 41/66] str --- crates/vm/src/builtins/str.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/vm/src/builtins/str.rs b/crates/vm/src/builtins/str.rs index 8a36d9350b..6ae3e045cf 100644 --- a/crates/vm/src/builtins/str.rs +++ b/crates/vm/src/builtins/str.rs @@ -1559,13 +1559,13 @@ impl PyStr { } #[pymethod] - fn __str__(zelf: &Py, vm: &VirtualMachine) -> PyResult { + fn __str__(zelf: &Py, vm: &VirtualMachine) -> PyStrRef { if zelf.class().is(vm.ctx.types.str_type) { // Already exact str, just return a reference - Ok(zelf.to_owned()) + zelf.to_owned() } else { // Subclass, create a new exact str - Ok(PyStr::from(zelf.data.clone()).into_ref(&vm.ctx)) + PyStr::from(zelf.data.clone()).into_ref(&vm.ctx) } } } From a6113f97c616b99e3a28bf7303f0f6534b916061 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:54:26 +0300 Subject: [PATCH 42/66] object --- crates/vm/src/builtins/object.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/vm/src/builtins/object.rs b/crates/vm/src/builtins/object.rs index 7ee22e5cbd..a41d4dea4e 100644 --- a/crates/vm/src/builtins/object.rs +++ b/crates/vm/src/builtins/object.rs @@ -104,6 +104,7 @@ impl Constructor for PyBaseObject { } } +#[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn generic_alloc(cls: PyTypeRef, _nitems: usize, vm: &VirtualMachine) -> PyResult { // Only create dict if the class has HAS_DICT flag (i.e., __slots__ was not defined // or __dict__ is in __slots__) @@ -542,6 +543,7 @@ impl PyBaseObject { common_reduce(obj, proto, vm) } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] #[pyslot] fn slot_hash(zelf: &PyObject, _vm: &VirtualMachine) -> PyResult { Ok(zelf.get_id() as _) From 6868642265bf541d510fdf1cfb36d72d628b51fd Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:55:40 +0300 Subject: [PATCH 43/66] map --- crates/vm/src/builtins/map.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/vm/src/builtins/map.rs b/crates/vm/src/builtins/map.rs index eff2ed72ff..4dda9caf21 100644 --- a/crates/vm/src/builtins/map.rs +++ b/crates/vm/src/builtins/map.rs @@ -61,24 +61,23 @@ impl PyMap { } #[pymethod] - fn __reduce__(zelf: PyRef, vm: &VirtualMachine) -> PyResult { + fn __reduce__(zelf: PyRef, vm: &VirtualMachine) -> PyTupleRef { let cls = zelf.class().to_owned(); let mut vec = vec![zelf.mapper.clone()]; vec.extend(zelf.iterators.iter().map(|o| o.clone().into())); let tuple_args = vm.ctx.new_tuple(vec); - Ok(if zelf.strict.load(atomic::Ordering::Acquire) { + if zelf.strict.load(atomic::Ordering::Acquire) { vm.new_tuple((cls, tuple_args, true)) } else { vm.new_tuple((cls, tuple_args)) - }) + } } #[pymethod] - fn __setstate__(zelf: PyRef, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + fn __setstate__(zelf: PyRef, state: PyObjectRef, vm: &VirtualMachine) { if let Ok(obj) = ArgIntoBool::try_from_object(vm, state) { zelf.strict.store(obj.into(), atomic::Ordering::Release); } - Ok(()) } } From 43296ecd2ed0b71ce00add83c86f7ad01560c9f0 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:56:48 +0300 Subject: [PATCH 44/66] int --- crates/vm/src/builtins/int.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/vm/src/builtins/int.rs b/crates/vm/src/builtins/int.rs index 9839765622..5468a011c1 100644 --- a/crates/vm/src/builtins/int.rs +++ b/crates/vm/src/builtins/int.rs @@ -446,7 +446,7 @@ impl PyInt { zelf: PyRef, ndigits: OptionalOption, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyRef { if let Some(ndigits) = ndigits.flatten() { let ndigits = ndigits.as_bigint(); // round(12345, -2) == 12300 @@ -477,10 +477,10 @@ impl PyInt { BigInt::from(0) }; let rounded = (rounded + correction) * sign; - return Ok(vm.ctx.new_int(rounded)); + return vm.ctx.new_int(rounded); } } - Ok(zelf) + zelf } #[pymethod] From afc8151b93a8a4ab7a989c1041c1d2f1a0f04955 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 11:57:56 +0300 Subject: [PATCH 45/66] genericalias --- crates/vm/src/builtins/genericalias.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/vm/src/builtins/genericalias.rs b/crates/vm/src/builtins/genericalias.rs index 0f22241c0f..b6f6012fd4 100644 --- a/crates/vm/src/builtins/genericalias.rs +++ b/crates/vm/src/builtins/genericalias.rs @@ -402,7 +402,7 @@ fn subs_tvars( } // CPython's _unpack_args equivalent -fn unpack_args(item: PyObjectRef, vm: &VirtualMachine) -> PyResult { +fn unpack_args(item: PyObjectRef, vm: &VirtualMachine) -> PyTupleRef { let mut new_args = Vec::new(); let arg_items = if let Ok(tuple) = item.try_to_ref::(vm) { @@ -442,7 +442,7 @@ fn unpack_args(item: PyObjectRef, vm: &VirtualMachine) -> PyResult { new_args.push(item); } - Ok(PyTuple::new_ref(new_args, &vm.ctx)) + PyTuple::new_ref(new_args, &vm.ctx) } // _Py_subs_parameters @@ -459,7 +459,7 @@ pub(crate) fn subs_parameters( } // Step 1: Unpack args - let mut item: PyObjectRef = unpack_args(item, vm)?.into(); + let mut item: PyObjectRef = unpack_args(item, vm).into(); // Step 2: Call __typing_prepare_subst__ on each parameter for param in parameters.iter() { From 1e3efd3ec27403334bf4231f6be366ab9e6df1a5 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:03:33 +0300 Subject: [PATCH 46/66] function --- crates/vm/src/builtins/function.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/vm/src/builtins/function.rs b/crates/vm/src/builtins/function.rs index 9b34aa8bff..a057388666 100644 --- a/crates/vm/src/builtins/function.rs +++ b/crates/vm/src/builtins/function.rs @@ -867,6 +867,7 @@ impl PyFunction { *self.name.lock() = name; } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] #[pymember] fn __doc__(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { // When accessed from instance, obj is the PyFunction instance @@ -879,6 +880,7 @@ impl PyFunction { } } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] #[pymember(setter)] fn set___doc__(vm: &VirtualMachine, zelf: PyObjectRef, value: PySetterValue) -> PyResult<()> { let zelf: PyRef = zelf.downcast().unwrap_or_else(|_| unreachable!()); From 79744d200cf2dbd06ad2b81c7647c2b0ae70bfe5 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:05:51 +0300 Subject: [PATCH 47/66] frame --- crates/vm/src/builtins/frame.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/vm/src/builtins/frame.rs b/crates/vm/src/builtins/frame.rs index bfce1ba829..9dcbacec8e 100644 --- a/crates/vm/src/builtins/frame.rs +++ b/crates/vm/src/builtins/frame.rs @@ -595,6 +595,7 @@ impl Frame { *storage = value.unwrap_or_none(vm); } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] #[pymember(type = "bool")] fn f_trace_lines(vm: &VirtualMachine, zelf: PyObjectRef) -> PyResult { let zelf: FrameRef = zelf.downcast().unwrap_or_else(|_| unreachable!()); @@ -626,6 +627,7 @@ impl Frame { } } + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] #[pymember(type = "bool")] fn f_trace_opcodes(vm: &VirtualMachine, zelf: PyObjectRef) -> PyResult { let zelf: FrameRef = zelf.downcast().unwrap_or_else(|_| unreachable!()); From cb20bfd2df2189e0143306a4814886e437cafcee Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:06:01 +0300 Subject: [PATCH 48/66] asyncgenerator --- crates/vm/src/builtins/asyncgenerator.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/vm/src/builtins/asyncgenerator.rs b/crates/vm/src/builtins/asyncgenerator.rs index 3825191ea5..9edf38c43a 100644 --- a/crates/vm/src/builtins/asyncgenerator.rs +++ b/crates/vm/src/builtins/asyncgenerator.rs @@ -766,12 +766,11 @@ impl PyAnextAwaitable { } #[pymethod] - fn close(&self, vm: &VirtualMachine) -> PyResult<()> { + fn close(&self, vm: &VirtualMachine) { self.state.store(AwaitableState::Closed); if let Ok(awaitable) = self.get_awaitable_iter(vm) { let _ = vm.call_method(&awaitable, "close", ()); } - Ok(()) } /// Convert StopAsyncIteration to StopIteration(default_value) From bf9789a91dc8973c88f27091848e2f12b5a2f462 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:11:33 +0300 Subject: [PATCH 49/66] format --- crates/common/src/format.rs | 44 +++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/crates/common/src/format.rs b/crates/common/src/format.rs index 1eee5ba67e..a2d875d22d 100644 --- a/crates/common/src/format.rs +++ b/crates/common/src/format.rs @@ -580,7 +580,13 @@ impl FormatSpec { }, }; - self.format_sign_and_align(&AsciiStr::new(&magnitude_str), sign_str, FormatAlign::Right) + Ok( + self.format_sign_and_align( + &AsciiStr::new(&magnitude_str), + sign_str, + FormatAlign::Right, + ), + ) } /// Format a float with locale-aware 'n' format. @@ -620,7 +626,13 @@ impl FormatSpec { } }; - self.format_sign_and_align(&AsciiStr::new(&magnitude_str), sign_str, FormatAlign::Right) + Ok( + self.format_sign_and_align( + &AsciiStr::new(&magnitude_str), + sign_str, + FormatAlign::Right, + ), + ) } /// Format a complex number with locale-aware 'n' format. @@ -672,7 +684,7 @@ impl FormatSpec { // No parentheses for 'n' format (CPython: add_parens=0) let magnitude_str = format!("{grouped_re}{grouped_im}"); - self.format_sign_and_align(&AsciiStr::new(&magnitude_str), "", FormatAlign::Right) + Ok(self.format_sign_and_align(&AsciiStr::new(&magnitude_str), "", FormatAlign::Right)) } pub fn format_bool(&self, input: bool) -> Result { @@ -789,7 +801,13 @@ impl FormatSpec { } }; let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, sign_str); - self.format_sign_and_align(&AsciiStr::new(&magnitude_str), sign_str, FormatAlign::Right) + Ok( + self.format_sign_and_align( + &AsciiStr::new(&magnitude_str), + sign_str, + FormatAlign::Right, + ), + ) } #[inline] @@ -863,11 +881,11 @@ impl FormatSpec { }; let sign_prefix = format!("{sign_str}{prefix}"); let magnitude_str = self.add_magnitude_separators(raw_magnitude_str, &sign_prefix); - self.format_sign_and_align( + Ok(self.format_sign_and_align( &AsciiStr::new(&magnitude_str), &sign_prefix, FormatAlign::Right, - ) + )) } pub fn format_string(&self, s: &T) -> Result @@ -883,7 +901,7 @@ impl FormatSpec { Some(p) => s.deref().chars().take(p).collect(), None => s.deref().to_owned(), }; - self.format_sign_and_align(&truncated, "", FormatAlign::Left) + Ok(self.format_sign_and_align(&truncated, "", FormatAlign::Left)) } _ => { let ch = char::from(self.format_type.as_ref().unwrap()); @@ -905,7 +923,11 @@ impl FormatSpec { } match &self.fill.unwrap_or_else(|| ' '.into()).to_char() { Some('0') => Err(FormatSpecError::ZeroPadding), - _ => self.format_sign_and_align(&AsciiStr::new(&magnitude_str), "", FormatAlign::Right), + _ => Ok(self.format_sign_and_align( + &AsciiStr::new(&magnitude_str), + "", + FormatAlign::Right, + )), } } @@ -1017,7 +1039,7 @@ impl FormatSpec { magnitude_str: &T, sign_str: &str, default_align: FormatAlign, - ) -> Result + ) -> String where T: CharLen + Deref, { @@ -1030,7 +1052,7 @@ impl FormatSpec { }); let magnitude_str = magnitude_str.deref(); - Ok(match align { + match align { FormatAlign::Left => format!( "{}{}{}", sign_str, @@ -1057,7 +1079,7 @@ impl FormatSpec { Self::compute_fill_string(fill_char, right_fill_chars_needed); format!("{left_fill_string}{sign_str}{magnitude_str}{right_fill_string}") } - }) + } } } From af6b119d871ccaeae6814562f1c6b4e35396396c Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:13:49 +0300 Subject: [PATCH 50/66] pypayload --- crates/derive-impl/src/lib.rs | 2 +- crates/derive-impl/src/pypayload.rs | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/derive-impl/src/lib.rs b/crates/derive-impl/src/lib.rs index 54336db2b8..78c2fde53f 100644 --- a/crates/derive-impl/src/lib.rs +++ b/crates/derive-impl/src/lib.rs @@ -84,7 +84,7 @@ pub fn py_freeze(input: TokenStream, compiler: &dyn Compiler) -> TokenStream { #[must_use] pub fn pypayload(input: DeriveInput) -> TokenStream { - result_to_tokens(pypayload::impl_pypayload(input)) + pypayload::impl_pypayload(input) } #[must_use] diff --git a/crates/derive-impl/src/pypayload.rs b/crates/derive-impl/src/pypayload.rs index eeae27c3fa..e1c3bcb697 100644 --- a/crates/derive-impl/src/pypayload.rs +++ b/crates/derive-impl/src/pypayload.rs @@ -1,17 +1,16 @@ use proc_macro2::TokenStream; use quote::quote; -use syn::{DeriveInput, Result}; +use syn::DeriveInput; -pub(crate) fn impl_pypayload(input: DeriveInput) -> Result { +pub(crate) fn impl_pypayload(input: DeriveInput) -> TokenStream { let ty = &input.ident; - let ret = quote! { + quote! { impl ::rustpython_vm::PyPayload for #ty { #[inline] fn class(_ctx: &::rustpython_vm::vm::Context) -> &'static rustpython_vm::Py<::rustpython_vm::builtins::PyType> { ::static_type() } } - }; - Ok(ret) + } } From dc3ec7ef1472ba68468989079c90b6ba8cf1f81c Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:19:24 +0300 Subject: [PATCH 51/66] pyclass --- crates/derive-impl/src/lib.rs | 2 +- crates/derive-impl/src/pyclass.rs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/derive-impl/src/lib.rs b/crates/derive-impl/src/lib.rs index 78c2fde53f..c1fc09d7c2 100644 --- a/crates/derive-impl/src/lib.rs +++ b/crates/derive-impl/src/lib.rs @@ -53,7 +53,7 @@ pub fn pyclass(attr: PunctuatedNestedMeta, item: Item) -> TokenStream { #[must_use] pub fn pyexception(attr: PunctuatedNestedMeta, item: Item) -> TokenStream { if matches!(item, syn::Item::Impl(_)) { - result_to_tokens(pyclass::impl_pyexception_impl(attr, item)) + pyclass::impl_pyexception_impl(attr, item) } else { result_to_tokens(pyclass::impl_pyexception(attr, item)) } diff --git a/crates/derive-impl/src/pyclass.rs b/crates/derive-impl/src/pyclass.rs index 625bbd0baf..4190b376d7 100644 --- a/crates/derive-impl/src/pyclass.rs +++ b/crates/derive-impl/src/pyclass.rs @@ -804,9 +804,9 @@ pub(crate) fn impl_pyexception(attr: PunctuatedNestedMeta, item: Item) -> Result Ok(ret) } -pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> Result { +pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> TokenStream { let Item::Impl(imp) = item else { - return Ok(item.into_token_stream()); + return item.into_token_stream(); }; // Check if with(Constructor) is specified. If Constructor trait is used, don't generate slot_new @@ -878,7 +878,7 @@ pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> R quote!(, #(#extra_attrs),*) }; - Ok(quote! { + quote! { #[pyclass(flags(BASETYPE, HAS_DICT), with(#(#with_items),*) #extra_attrs_tokens)] impl #generics #self_ty { #(#items)* @@ -886,7 +886,7 @@ pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> R #slot_new #slot_init - }) + } } macro_rules! define_content_item { @@ -1944,12 +1944,12 @@ fn extract_impl_attrs(attr: PunctuatedNestedMeta, item: &Ident) -> Result( index: usize, attr_name: AttrName, -) -> Result>> +) -> Box> where Item: ItemLike + ToTokens + GetIdent, { use AttrName::*; - Ok(match attr_name { + match attr_name { attr_name @ (Method | ClassMethod | StaticMethod) => Box::new(MethodItem { inner: ContentItemInner { index, attr_name }, }), @@ -1968,7 +1968,7 @@ where Member => Box::new(MemberItem { inner: ContentItemInner { index, attr_name }, }), - }) + } } fn attrs_to_content_items( @@ -1976,7 +1976,7 @@ fn attrs_to_content_items( item_new: F, ) -> Result<(Vec, Vec)> where - F: Fn(usize, AttrName) -> Result, + F: Fn(usize, AttrName) -> R, { let mut cfgs: Vec = Vec::new(); let mut result = Vec::new(); @@ -2018,7 +2018,7 @@ where } }; - result.push(item_new(i, attr_name)?); + result.push(item_new(i, attr_name)); } Ok((result, cfgs)) } From cdc45c3011636e82b15a2b02aa01f2a0ace929f9 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:23:05 +0300 Subject: [PATCH 52/66] util --- crates/derive-impl/src/pyclass.rs | 8 ++++---- crates/derive-impl/src/pymodule.rs | 10 +++++----- crates/derive-impl/src/util.rs | 7 +++---- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/crates/derive-impl/src/pyclass.rs b/crates/derive-impl/src/pyclass.rs index 4190b376d7..030e3eadd8 100644 --- a/crates/derive-impl/src/pyclass.rs +++ b/crates/derive-impl/src/pyclass.rs @@ -644,8 +644,8 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result { @@ -1053,7 +1053,7 @@ impl ModuleItem for AttributeItem { args.context .attribute_items - .add_item(ident, py_names, cfgs, tokens, 1)?; + .add_item(ident, py_names, cfgs, tokens, 1); Ok(()) } diff --git a/crates/derive-impl/src/util.rs b/crates/derive-impl/src/util.rs index e1ad9ed2b8..3ad41679c3 100644 --- a/crates/derive-impl/src/util.rs +++ b/crates/derive-impl/src/util.rs @@ -45,7 +45,7 @@ impl ItemNursery { cfgs: Vec, tokens: TokenStream, sort_order: usize, - ) -> Result<()> { + ) { self.0.push(NurseryItem { attr_name, py_names, @@ -53,7 +53,6 @@ impl ItemNursery { tokens, sort_order, }); - Ok(()) } pub(crate) fn validate(self) -> Result { @@ -216,8 +215,8 @@ impl ItemMetaInner { Ok(value) } - pub(crate) fn _has_key(&self, key: &str) -> Result { - Ok(matches!(self.meta_map.get(key), Some((_, _)))) + pub(crate) fn _has_key(&self, key: &str) -> bool { + matches!(self.meta_map.get(key), Some((_, _))) } pub(crate) fn _bool(&self, key: &str) -> Result { From 8c253af4df81013cb443b12dd4c19e53a59e6d95 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:24:30 +0300 Subject: [PATCH 53/66] string_parser --- crates/codegen/src/string_parser.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/codegen/src/string_parser.rs b/crates/codegen/src/string_parser.rs index a7ad8c35a4..ee6c08a563 100644 --- a/crates/codegen/src/string_parser.rs +++ b/crates/codegen/src/string_parser.rs @@ -68,7 +68,7 @@ impl StringParser { self.source[self.cursor..].as_bytes().first().copied() } - fn parse_unicode_literal(&mut self, literal_number: usize) -> Result { + fn parse_unicode_literal(&mut self, literal_number: usize) -> CodePoint { let mut p: u32 = 0u32; for i in 1..=literal_number { match self.next_char() { @@ -79,7 +79,7 @@ impl StringParser { None => unreachable!(), } } - Ok(CodePoint::from_u32(p).unwrap()) + CodePoint::from_u32(p).unwrap() } fn parse_octet(&mut self, o: u8) -> char { @@ -134,9 +134,9 @@ impl StringParser { 't' => '\t'.into(), 'v' => '\x0b'.into(), o @ '0'..='7' => self.parse_octet(o as u8).into(), - 'x' => self.parse_unicode_literal(2)?, - 'u' if !self.flags.is_byte_string() => self.parse_unicode_literal(4)?, - 'U' if !self.flags.is_byte_string() => self.parse_unicode_literal(8)?, + 'x' => self.parse_unicode_literal(2), + 'u' if !self.flags.is_byte_string() => self.parse_unicode_literal(4), + 'U' if !self.flags.is_byte_string() => self.parse_unicode_literal(8), 'N' if !self.flags.is_byte_string() => self.parse_unicode_name()?.into(), // Special cases where the escape sequence is not a single character '\n' => return Ok(None), From eb403217ef4f5473918e74d91d172bcdcb282ab0 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:46:28 +0300 Subject: [PATCH 54/66] compile --- crates/codegen/src/compile.rs | 142 ++++++++++++++++------------------ 1 file changed, 67 insertions(+), 75 deletions(-) diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index ddf720f52e..8877336b95 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -1231,37 +1231,37 @@ impl Compiler { /// Get the cell-relative index of a free variable. /// Returns ncells + freevar_idx. Fixed up to localsplus index during finalize. - fn get_free_var_index(&mut self, name: &str) -> CompileResult { + fn get_free_var_index(&mut self, name: &str) -> oparg::VarNum { let info = self.code_stack.last_mut().unwrap(); let idx = info .metadata .freevars .get_index_of(name) .unwrap_or_else(|| info.metadata.freevars.insert_full(name.to_owned()).0); - Ok((idx + info.metadata.cellvars.len()).to_u32().into()) + (idx + info.metadata.cellvars.len()).to_u32().into() } /// Get the cell-relative index of a cell variable. /// Returns cellvar_idx. Fixed up to localsplus index during finalize. - fn get_cell_var_index(&mut self, name: &str) -> CompileResult { + fn get_cell_var_index(&mut self, name: &str) -> oparg::VarNum { let info = self.code_stack.last_mut().unwrap(); let idx = info .metadata .cellvars .get_index_of(name) .unwrap_or_else(|| info.metadata.cellvars.insert_full(name.to_owned()).0); - Ok(idx.to_u32().into()) + idx.to_u32().into() } /// Get the index of a local variable. - fn get_local_var_index(&mut self, name: &str) -> CompileResult { + fn get_local_var_index(&mut self, name: &str) -> oparg::VarNum { let info = self.code_stack.last_mut().unwrap(); let idx = info .metadata .varnames .get_index_of(name) .unwrap_or_else(|| info.metadata.varnames.insert_full(name.to_owned()).0); - Ok(idx.to_u32().into()) + idx.to_u32().into() } /// Get the index of a global name. @@ -1479,8 +1479,8 @@ impl Compiler { // Load __class__ from cell/free variable let scope = self.get_ref_type("__class__").map_err(|e| self.error(e))?; let idx = match scope { - SymbolScope::Cell => self.get_cell_var_index("__class__")?, - SymbolScope::Free => self.get_free_var_index("__class__")?, + SymbolScope::Cell => self.get_cell_var_index("__class__"), + SymbolScope::Free => self.get_free_var_index("__class__"), _ => { return Err(self.error(CodegenErrorType::SyntaxError( "super(): __class__ cell not found".to_owned(), @@ -1929,14 +1929,14 @@ impl Compiler { // Emit format validation: if format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError // VALUE_WITH_FAKE_GLOBALS = 2 (from annotationlib.Format) - self.emit_format_validation()?; + self.emit_format_validation(); Ok(Some(saved_ctx)) } /// Emit format parameter validation for annotation scope /// if format > VALUE_WITH_FAKE_GLOBALS (2): raise NotImplementedError - fn emit_format_validation(&mut self) -> CompileResult<()> { + fn emit_format_validation(&mut self) { // Load format parameter (first local variable, index 0) emit!( self, @@ -1976,8 +1976,6 @@ impl Compiler { // Body label - continue with annotation evaluation self.switch_to_block(body_block); - - Ok(()) } /// Push a new fblock @@ -2228,11 +2226,9 @@ impl Compiler { self._name_inner(name, |i| &mut i.metadata.names) } - fn varname(&mut self, name: &str) -> CompileResult { - // Note: __debug__ checks are now handled in symboltable phase - Ok(oparg::VarNum::from_u32( - self._name_inner(name, |i| &mut i.metadata.varnames), - )) + fn varname(&mut self, name: &str) -> oparg::VarNum { + // NOTE: __debug__ checks are now handled in symboltable phase + oparg::VarNum::from_u32(self._name_inner(name, |i| &mut i.metadata.varnames)) } fn _name_inner( @@ -2784,8 +2780,8 @@ impl Compiler { match op_type { NameOp::Deref => { let i = match actual_scope { - SymbolScope::Free => self.get_free_var_index(&name)?, - SymbolScope::Cell => self.get_cell_var_index(&name)?, + SymbolScope::Free => self.get_free_var_index(&name), + SymbolScope::Cell => self.get_cell_var_index(&name), _ => unreachable!("Invalid scope for Deref operation"), }; @@ -2800,7 +2796,7 @@ impl Compiler { emit!(self, Instruction::LoadFromDictOrDeref { i }); // can_see_class_scope: LOAD_DEREF(__classdict__) first } else if can_see_class_scope { - let classdict_idx = self.get_free_var_index("__classdict__")?; + let classdict_idx = self.get_free_var_index("__classdict__"); emit!(self, Instruction::LoadDeref { i: classdict_idx }); emit!(self, Instruction::LoadFromDictOrDeref { i }); } else { @@ -2812,7 +2808,7 @@ impl Compiler { }; } NameOp::Fast => { - let var_num = self.get_local_var_index(&name)?; + let var_num = self.get_local_var_index(&name); match usage { NameUsage::Load => emit!(self, Instruction::LoadFast { var_num }), NameUsage::Store => emit!(self, Instruction::StoreFast { var_num }), @@ -2852,7 +2848,7 @@ impl Compiler { match usage { NameUsage::Load => { // Load __classdict__ first (it's a free variable in annotation scope) - let classdict_idx = self.get_free_var_index("__classdict__")?; + let classdict_idx = self.get_free_var_index("__classdict__"); emit!(self, Instruction::LoadDeref { i: classdict_idx }); emit!(self, Instruction::LoadFromDictOrGlobals { i: idx }); } @@ -3414,16 +3410,16 @@ impl Compiler { .chain(kw_without_defaults) .chain(kw_with_defaults.into_iter().map(|(arg, _)| arg)); for name in args_iter { - self.varname(name.name.as_str())?; + self.varname(name.name.as_str()); } if let Some(name) = parameters.vararg.as_deref() { self.current_code_info().flags |= bytecode::CodeFlags::VARARGS; - self.varname(name.name.as_str())?; + self.varname(name.name.as_str()); } if let Some(name) = parameters.kwarg.as_deref() { self.current_code_info().flags |= bytecode::CodeFlags::VARKEYWORDS; - self.varname(name.name.as_str())?; + self.varname(name.name.as_str()); } Ok(()) @@ -3472,7 +3468,7 @@ impl Compiler { .varnames .insert(".format".to_owned()); - self.emit_format_validation()?; + self.emit_format_validation(); // TypeParams scope is function-like let prev_ctx = self.ctx; @@ -3525,7 +3521,7 @@ impl Compiler { .metadata .varnames .insert(".format".to_owned()); - self.emit_format_validation()?; + self.emit_format_validation(); let prev_ctx = self.ctx; self.ctx = CompileContext { @@ -5108,7 +5104,7 @@ impl Compiler { .insert("format".to_owned()); // Emit format validation: if format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError - self.emit_format_validation()?; + self.emit_format_validation(); emit!(self, Instruction::BuildMap { count: 0 }); @@ -5147,7 +5143,7 @@ impl Compiler { value: simple_idx.into(), }); if parent_scope_type == CompilerScope::Class { - let idx = self.get_free_var_index("__conditional_annotations__")?; + let idx = self.get_free_var_index("__conditional_annotations__"); emit!(self, Instruction::LoadDeref { i: idx }); } else { let cond_annotations_name = self.name("__conditional_annotations__"); @@ -5681,7 +5677,7 @@ impl Compiler { // setup so nested generic classes match CPython's prologue order. if self.current_symbol_table().needs_classdict { emit!(self, Instruction::LoadLocals); - let classdict_idx = self.get_cell_var_index("__classdict__")?; + let classdict_idx = self.get_cell_var_index("__classdict__"); emit!(self, Instruction::StoreDeref { i: classdict_idx }); } @@ -5751,7 +5747,7 @@ impl Compiler { // Store __classdictcell__ if __classdict__ is a cell variable if self.current_symbol_table().needs_classdict { - let classdict_idx = u32::from(self.get_cell_var_index("__classdict__")?); + let classdict_idx = u32::from(self.get_cell_var_index("__classdict__")); emit!(self, PseudoInstruction::LoadClosure { i: classdict_idx }); self.set_no_location(); let classdictcell = self.name("__classdictcell__"); @@ -6596,24 +6592,23 @@ impl Compiler { /// Ensures that `pc.fail_pop` has at least `n + 1` entries. /// If not, new labels are generated and pushed until the required size is reached. - fn ensure_fail_pop(&mut self, pc: &mut PatternContext, n: usize) -> CompileResult<()> { + fn ensure_fail_pop(&mut self, pc: &mut PatternContext, n: usize) { let required_size = n + 1; if required_size <= pc.fail_pop.len() { - return Ok(()); + return; } while pc.fail_pop.len() < required_size { let new_block = self.new_block(); pc.fail_pop.push(new_block); } - Ok(()) } - fn jump_to_fail_pop(&mut self, pc: &mut PatternContext, op: JumpOp) -> CompileResult<()> { + fn jump_to_fail_pop(&mut self, pc: &mut PatternContext, op: JumpOp) { // Compute the total number of items to pop: // items on top plus the captured objects. let pops = pc.on_top + pc.stores.len(); // Ensure that the fail_pop vector has at least `pops + 1` elements. - self.ensure_fail_pop(pc, pops)?; + self.ensure_fail_pop(pc, pops); // Emit a jump using the jump target stored at index `pops`. match op { JumpOp::Jump => { @@ -6622,7 +6617,7 @@ impl Compiler { PseudoInstruction::Jump { delta: pc.fail_pop[pops] } - ); + ) } JumpOp::PopJumpIfFalse => { emit!( @@ -6630,19 +6625,18 @@ impl Compiler { Instruction::PopJumpIfFalse { delta: pc.fail_pop[pops] } - ); + ) } - } - Ok(()) + }; } /// Emits the necessary POP instructions for all failure targets in the pattern context, /// then resets the fail_pop vector. - fn emit_and_reset_fail_pop(&mut self, pc: &mut PatternContext) -> CompileResult<()> { + fn emit_and_reset_fail_pop(&mut self, pc: &mut PatternContext) { // If the fail_pop vector is empty, nothing needs to be done. if pc.fail_pop.is_empty() { debug_assert!(pc.fail_pop.is_empty()); - return Ok(()); + return; } // Iterate over the fail_pop vector in reverse order, skipping the first label. for &label in pc.fail_pop.iter().skip(1).rev() { @@ -6655,11 +6649,10 @@ impl Compiler { pc.fail_pop.clear(); // Free the memory used by the vector. pc.fail_pop.shrink_to_fit(); - Ok(()) } /// Duplicate the effect of Python 3.10's ROT_* instructions using SWAPs. - fn pattern_helper_rotate(&mut self, mut count: usize) -> CompileResult<()> { + fn pattern_helper_rotate(&mut self, mut count: usize) { // Rotate TOS (top of stack) to position `count` down // This is done by a series of swaps // For count=1, no rotation needed (already at top) @@ -6675,7 +6668,6 @@ impl Compiler { ); count -= 1; } - Ok(()) } /// Helper to store a captured name for a star pattern. @@ -6711,7 +6703,7 @@ impl Compiler { // Calculate how many items to rotate: let rotations = pc.on_top + pc.stores.len() + 1; - self.pattern_helper_rotate(rotations)?; + self.pattern_helper_rotate(rotations); // Append the name to the captured stores. pc.stores.push(name.to_string()); @@ -6993,7 +6985,7 @@ impl Compiler { // At this point the TOS is a tuple of (nargs + n_attrs) attributes (or None). pc.on_top += 1; - self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?; + self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse); // Unpack the tuple into (nargs + n_attrs) items. let total = nargs + n_attrs; @@ -7065,7 +7057,7 @@ impl Compiler { emit!(self, Instruction::MatchMapping); // Stack: [subject, is_mapping] - self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?; + self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse); // Stack: [subject] // Special case: empty pattern {} with no rest @@ -7088,7 +7080,7 @@ impl Compiler { opname: ComparisonOperator::GreaterOrEqual } ); - self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?; + self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse); // Stack: [subject] } @@ -7163,7 +7155,7 @@ impl Compiler { ); // Stack: [subject, keys_tuple, values_tuple, bool] - self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?; + self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse); // Stack: [subject, keys_tuple, values_tuple] // Unpack values (the original values_tuple) @@ -7302,7 +7294,7 @@ impl Compiler { // Also perform the same rotation on the evaluation stack. self.set_source_range(alt.range()); for _ in 0..=i_stores { - self.pattern_helper_rotate(i_control + 1)?; + self.pattern_helper_rotate(i_control + 1); } } } @@ -7312,7 +7304,7 @@ impl Compiler { self.set_source_range(alt.range()); emit!(self, PseudoInstruction::Jump { delta: end }); self.set_source_range(alt.range()); - self.emit_and_reset_fail_pop(pc)?; + self.emit_and_reset_fail_pop(pc); } // Restore the original pattern context. @@ -7325,7 +7317,7 @@ impl Compiler { // No alternative matched: pop the subject and fail. self.set_source_range(p.range()); emit!(self, Instruction::PopTop); - self.jump_to_fail_pop(pc, JumpOp::Jump)?; + self.jump_to_fail_pop(pc, JumpOp::Jump); // Use the label "end". self.switch_to_block(end); @@ -7336,7 +7328,7 @@ impl Compiler { for i in 0..n_stores { // Rotate the capture to its proper place. self.set_source_range(p.range()); - self.pattern_helper_rotate(n_rots)?; + self.pattern_helper_rotate(n_rots); let name = &control.as_ref().unwrap()[i]; // Check for duplicate binding. if pc.stores.contains(name) { @@ -7384,7 +7376,7 @@ impl Compiler { // Keep the subject on top during the sequence and length checks. pc.on_top += 1; emit!(self, Instruction::MatchSequence); - self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?; + self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse); if star.is_none() { // No star: len(subject) == size @@ -7396,7 +7388,7 @@ impl Compiler { opname: ComparisonOperator::Equal } ); - self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?; + self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse); } else if size > 1 { // Star exists: len(subject) >= size - 1 emit!(self, Instruction::GetLen); @@ -7409,7 +7401,7 @@ impl Compiler { opname: ComparisonOperator::GreaterOrEqual } ); - self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?; + self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse); } // Whatever comes next should consume the subject. @@ -7441,7 +7433,7 @@ impl Compiler { } ); emit!(self, Instruction::ToBool); - self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?; + self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse); Ok(()) } @@ -7449,7 +7441,7 @@ impl Compiler { &mut self, p: &ast::PatternMatchSingleton, pc: &mut PatternContext, - ) -> CompileResult<()> { + ) { // Load the singleton constant value. self.emit_load_const(match p.value { ast::Singleton::None => ConstantData::None, @@ -7459,8 +7451,7 @@ impl Compiler { // Compare using the "Is" operator. emit!(self, Instruction::IsOp { invert: Invert::No }); // Jump to the failure label if the comparison is false. - self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?; - Ok(()) + self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse); } fn compile_pattern( @@ -7475,7 +7466,8 @@ impl Compiler { self.compile_pattern_value(pattern_type, pattern_context) } ast::Pattern::MatchSingleton(pattern_type) => { - self.compile_pattern_singleton(pattern_type, pattern_context) + self.compile_pattern_singleton(pattern_type, pattern_context); + Ok(()) } ast::Pattern::MatchSequence(pattern_type) => { self.compile_pattern_sequence(pattern_type, pattern_context) @@ -7544,7 +7536,7 @@ impl Compiler { } if let Some(ref guard) = m.guard { - self.ensure_fail_pop(pattern_context, 0)?; + self.ensure_fail_pop(pattern_context, 0); self.compile_jump_if_inner( guard, false, @@ -7573,7 +7565,7 @@ impl Compiler { last.match_success_jump = true; } self.set_source_range(m.pattern.range()); - self.emit_and_reset_fail_pop(pattern_context)?; + self.emit_and_reset_fail_pop(pattern_context); } if has_default { @@ -9834,7 +9826,7 @@ impl Compiler { // Set qualname for comprehension self.set_qualname(); - let arg0 = self.varname(".0")?; + let arg0 = self.varname(".0"); let return_none = init_collection.is_none(); @@ -10184,7 +10176,7 @@ impl Compiler { self.set_source_range(comprehension_range); let mut total_stack_items: usize = 0; for name in &pushed_locals { - let var_num = self.varname(name)?; + let var_num = self.varname(name); emit!(self, Instruction::LoadFastAndClear { var_num }); total_stack_items += 1; // If the comp symbol is CELL, emit MAKE_CELL to create fresh cell @@ -10197,9 +10189,9 @@ impl Compiler { .get(name) .is_some_and(|s| s.scope == SymbolScope::Free) { - self.get_free_var_index(name)? + self.get_free_var_index(name) } else { - self.get_cell_var_index(name)? + self.get_cell_var_index(name) }; emit!(self, Instruction::MakeCell { i }); } @@ -10375,7 +10367,7 @@ impl Compiler { } ); for name in pushed_locals.iter().rev() { - let var_num = self.varname(name)?.as_u32(); + let var_num = self.varname(name).as_u32(); emit!(self, PseudoInstruction::StoreFastMaybeNull { var_num }); } // Re-raise the exception @@ -10397,7 +10389,7 @@ impl Compiler { // Restore saved locals (StoreFast restores the saved cell object for merged cells) for name in pushed_locals.iter().rev() { - let var_num = self.varname(name)?.as_u32(); + let var_num = self.varname(name).as_u32(); emit!(self, PseudoInstruction::StoreFastMaybeNull { var_num }); } self.set_source_range(saved_source_range); @@ -11481,7 +11473,8 @@ impl Compiler { )?; } self.set_source_range(fstring_range); - self.finish_fstring(pending_literal, pending_literal_no_location, element_count) + self.finish_fstring(pending_literal, pending_literal_no_location, element_count); + Ok(()) } fn compile_fstring_parts_joined(&mut self, fstring: &[ast::FStringPart]) -> CompileResult<()> { @@ -11545,7 +11538,7 @@ impl Compiler { mut pending_literal: Option, mut pending_literal_no_location: bool, mut element_count: u32, - ) -> CompileResult<()> { + ) { let keep_empty = element_count == 0; self.emit_pending_fstring_literal( &mut pending_literal, @@ -11567,8 +11560,6 @@ impl Compiler { } ); } - - Ok(()) } fn finish_fstring_join( @@ -11690,7 +11681,8 @@ impl Compiler { &mut element_count, false, )?; - self.finish_fstring(pending_literal, pending_literal_no_location, element_count) + self.finish_fstring(pending_literal, pending_literal_no_location, element_count); + Ok(()) } fn compile_fstring_elements_joined( From 06d7edf5cb7d3357001b31f7adf75db539ac78f5 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 12:55:03 +0300 Subject: [PATCH 55/66] Don't create `print_source_line` fn if no host_env --- crates/vm/src/exceptions.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/crates/vm/src/exceptions.rs b/crates/vm/src/exceptions.rs index 8b6bb8a325..6a1b8d48ae 100644 --- a/crates/vm/src/exceptions.rs +++ b/crates/vm/src/exceptions.rs @@ -392,15 +392,6 @@ fn print_source_line( Ok(()) } -#[cfg(not(feature = "host_env"))] -fn print_source_line( - _output: &mut W, - _filename: &str, - _lineno: usize, -) -> Result<(), W::Error> { - Ok(()) -} - /// Print exception occurrence location from traceback element fn write_traceback_entry( output: &mut W, @@ -414,6 +405,8 @@ fn write_traceback_entry( tb_entry.lineno, tb_entry.frame.code.obj_name )?; + + #[cfg(feature = "host_env")] print_source_line(output, filename, tb_entry.lineno.get())?; Ok(()) From c8a425d39602786c8c453c3816e74af51d12329f Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:02:38 +0300 Subject: [PATCH 56/66] clippy when no host_env --- crates/vm/src/stdlib/_io.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/vm/src/stdlib/_io.rs b/crates/vm/src/stdlib/_io.rs index 4f541e5e82..2a26da7272 100644 --- a/crates/vm/src/stdlib/_io.rs +++ b/crates/vm/src/stdlib/_io.rs @@ -5303,6 +5303,10 @@ mod _io { } } + #[cfg_attr( + not(feature = "host_env"), + expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature") + )] pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py) -> PyResult<()> { // Call auto-generated initialization first __module_exec(vm, module); From 49d5d753327be61f59e7149c107a148581ff9ef0 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:04:34 +0300 Subject: [PATCH 57/66] sys --- crates/vm/src/stdlib/sys.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/vm/src/stdlib/sys.rs b/crates/vm/src/stdlib/sys.rs index 88f487778a..46875248a1 100644 --- a/crates/vm/src/stdlib/sys.rs +++ b/crates/vm/src/stdlib/sys.rs @@ -1073,8 +1073,8 @@ pub mod sys { /// Stub for non-threading builds - returns empty dict #[cfg(not(feature = "threading"))] #[pyfunction] - fn _current_frames(vm: &VirtualMachine) -> PyResult { - Ok(vm.ctx.new_dict()) + fn _current_frames(vm: &VirtualMachine) -> PyDictRef { + vm.ctx.new_dict() } #[pyfunction] From faadcdb0c1662c3f38437f4a79394588c4f60913 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:05:41 +0300 Subject: [PATCH 58/66] pwd --- crates/vm/src/stdlib/pwd.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/vm/src/stdlib/pwd.rs b/crates/vm/src/stdlib/pwd.rs index e8aee608cc..726c9ad7e5 100644 --- a/crates/vm/src/stdlib/pwd.rs +++ b/crates/vm/src/stdlib/pwd.rs @@ -98,7 +98,7 @@ mod pwd { // TODO: maybe merge this functionality into nix? #[cfg(not(target_os = "android"))] #[pyfunction] - fn getpwall(vm: &VirtualMachine) -> PyResult> { + fn getpwall(vm: &VirtualMachine) -> Vec { // setpwent, getpwent, etc are not thread safe. Could use fgetpwent_r, but this is easier static GETPWALL: parking_lot::Mutex<()> = parking_lot::Mutex::new(()); let _guard = GETPWALL.lock(); @@ -112,6 +112,6 @@ mod pwd { } unsafe { libc::endpwent() }; - Ok(list) + list } } From 9c4dd84633269a49ecf73c981f0d3d1f99cf972b Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:09:14 +0300 Subject: [PATCH 59/66] os --- crates/vm/src/stdlib/os.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/vm/src/stdlib/os.rs b/crates/vm/src/stdlib/os.rs index 67fd4d0dd2..448e747101 100644 --- a/crates/vm/src/stdlib/os.rs +++ b/crates/vm/src/stdlib/os.rs @@ -1907,6 +1907,10 @@ pub(super) mod _os { #[pyclass(with(PyStructSequence))] impl PyTimesResult {} + #[allow( + clippy::unnecessary_wraps, + reason = "Can return an error on some platforms" + )] #[cfg(all(any(unix, windows), not(target_os = "redox")))] #[pyfunction] fn times(vm: &VirtualMachine) -> PyResult { From 9216bac1b96bfec07bff2d417f62dfa3c39f892e Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:13:40 +0300 Subject: [PATCH 60/66] windows related --- crates/vm/src/stdlib/_ctypes.rs | 34 ++++++++++++++++----------------- crates/vm/src/stdlib/nt.rs | 6 +++--- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/crates/vm/src/stdlib/_ctypes.rs b/crates/vm/src/stdlib/_ctypes.rs index 2c5fc3e537..8c874ae55f 100644 --- a/crates/vm/src/stdlib/_ctypes.rs +++ b/crates/vm/src/stdlib/_ctypes.rs @@ -547,13 +547,13 @@ pub(crate) mod _ctypes { name: String, _load_flags: OptionalArg, vm: &VirtualMachine, - ) -> PyResult { + ) -> usize { // TODO: audit functions first // TODO: load_flags let cache = library::libcache(); let mut cache_write = cache.write(); let (id, _) = cache_write.get_or_insert_lib(&name, vm).unwrap(); - Ok(id) + id } #[cfg(not(windows))] @@ -741,13 +741,13 @@ pub(crate) mod _ctypes { #[cfg(target_os = "windows")] #[pyfunction(name = "_check_HRESULT")] - fn check_hresult(_self: PyObjectRef, hr: i32, _vm: &VirtualMachine) -> PyResult { + fn check_hresult(_self: PyObjectRef, hr: i32, _vm: &VirtualMachine) -> i32 { // TODO: fixme if hr < 0 { // vm.ctx.new_windows_error(hr) todo!(); } else { - Ok(hr) + hr } } @@ -957,8 +957,8 @@ pub(crate) mod _ctypes { #[cfg(windows)] #[pyfunction] - fn get_last_error() -> PyResult { - Ok(super::function::get_last_error_value()) + fn get_last_error() -> Pu32 { + super::function::get_last_error_value() } #[cfg(windows)] @@ -1189,7 +1189,7 @@ pub(crate) mod _ctypes { #[cfg(windows)] #[pyfunction(name = "FormatError")] - fn format_error_func(code: OptionalArg, _vm: &VirtualMachine) -> PyResult { + fn format_error_func(code: OptionalArg, _vm: &VirtualMachine) -> String { use windows_sys::Win32::Foundation::{GetLastError, LocalFree}; use windows_sys::Win32::System::Diagnostics::Debug::{ FORMAT_MESSAGE_ALLOCATE_BUFFER, FORMAT_MESSAGE_FROM_SYSTEM, @@ -1214,22 +1214,20 @@ pub(crate) mod _ctypes { }; if len == 0 || buffer.is_null() { - return Ok("".to_string()); + return "".to_string(); } - let message = unsafe { + unsafe { let slice = core::slice::from_raw_parts(buffer, len as usize); let msg = String::from_utf16_lossy(slice).trim_end().to_string(); LocalFree(buffer as *mut _); msg - }; - - Ok(message) + } } #[cfg(windows)] #[pyfunction(name = "CopyComPointer")] - fn copy_com_pointer(src: PyObjectRef, dst: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn copy_com_pointer(src: PyObjectRef, dst: PyObjectRef, vm: &VirtualMachine) -> i32 { use windows_sys::Win32::Foundation::{E_POINTER, S_OK}; // 1. Extract pointer-to-pointer address from dst (byref() result) @@ -1238,15 +1236,15 @@ pub(crate) mod _ctypes { let base = if let Some(cdata) = carg.obj.downcast_ref::() { cdata.buffer.read().as_ptr() as usize } else { - return Ok(E_POINTER); + return E_POINTER; }; (base as isize + carg.offset) as usize } else { - return Ok(E_POINTER); + return E_POINTER; }; if pdst == 0 { - return Ok(E_POINTER); + return E_POINTER; } // 2. Extract COM pointer value from src @@ -1265,7 +1263,7 @@ pub(crate) mod _ctypes { 0 } } else { - return Ok(E_POINTER); + return E_POINTER; }; // 3. Call IUnknown::AddRef if src is non-NULL @@ -1286,7 +1284,7 @@ pub(crate) mod _ctypes { *(pdst as *mut usize) = src_ptr; } - Ok(S_OK) + S_OK } #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] diff --git a/crates/vm/src/stdlib/nt.rs b/crates/vm/src/stdlib/nt.rs index 1dfc5f9c06..ddba602824 100644 --- a/crates/vm/src/stdlib/nt.rs +++ b/crates/vm/src/stdlib/nt.rs @@ -127,13 +127,13 @@ pub(crate) mod module { } #[pyfunction] - pub(super) fn _supports_virtual_terminal() -> PyResult { + pub(super) fn _supports_virtual_terminal() -> bool { let mut mode = 0; let handle = unsafe { Console::GetStdHandle(Console::STD_ERROR_HANDLE) }; if unsafe { Console::GetConsoleMode(handle, &mut mode) } == 0 { - return Ok(false); + return false; } - Ok(mode & Console::ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0) + mode & Console::ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0 } #[derive(FromArgs)] From 2b85da66ace76c24e378ee07daf15c72bd776ac1 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:16:03 +0300 Subject: [PATCH 61/66] winreg --- crates/vm/src/stdlib/winreg.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/vm/src/stdlib/winreg.rs b/crates/vm/src/stdlib/winreg.rs index 2409945952..ffe6351409 100644 --- a/crates/vm/src/stdlib/winreg.rs +++ b/crates/vm/src/stdlib/winreg.rs @@ -195,15 +195,15 @@ mod winreg { } #[pymethod] - fn Detach(&self) -> PyResult { + fn Detach(&self) -> usize { // Atomically swap the handle with null and return the old value let old_hkey = self.hkey.swap(core::ptr::null_mut()); - Ok(old_hkey as usize) + old_hkey as usize } #[pymethod] - fn __enter__(zelf: PyRef, _vm: &VirtualMachine) -> PyResult> { - Ok(zelf) + fn __enter__(zelf: PyRef, _vm: &VirtualMachine) -> PyRef { + zelf } #[pymethod] @@ -216,8 +216,8 @@ mod winreg { } #[pymethod] - fn __str__(zelf: &Py, vm: &VirtualMachine) -> PyResult> { - Ok(vm.ctx.new_str(format!("", zelf.hkey.load()))) + fn __str__(zelf: &Py, vm: &VirtualMachine) -> PyRef { + vm.ctx.new_str(format!("", zelf.hkey.load())) } } From 8fac28fac62a7fbb2795364c9a30a4d2fe3ece4b Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:17:49 +0300 Subject: [PATCH 62/66] vm_class --- crates/wasm/src/vm_class.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/wasm/src/vm_class.rs b/crates/wasm/src/vm_class.rs index 96b1b2a17f..1d0b8701c3 100644 --- a/crates/wasm/src/vm_class.rs +++ b/crates/wasm/src/vm_class.rs @@ -26,6 +26,7 @@ mod _window { use super::{js_module, wasm_builtins}; use rustpython_vm::{Py, PyPayload, PyResult, VirtualMachine, builtins::PyModule}; + #[expect(clippy::unnecessary_wraps, reason = "Needs to comply with a signature")] pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py) -> PyResult<()> { __module_exec(vm, module); extend_module!(vm, module, { From 2cd5d81f8950fe8339d247ff0d28f4cc1d09cf65 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:22:19 +0300 Subject: [PATCH 63/66] typo --- crates/vm/src/stdlib/_ctypes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/vm/src/stdlib/_ctypes.rs b/crates/vm/src/stdlib/_ctypes.rs index 8c874ae55f..97947bb2fe 100644 --- a/crates/vm/src/stdlib/_ctypes.rs +++ b/crates/vm/src/stdlib/_ctypes.rs @@ -957,7 +957,7 @@ pub(crate) mod _ctypes { #[cfg(windows)] #[pyfunction] - fn get_last_error() -> Pu32 { + fn get_last_error() -> u32 { super::function::get_last_error_value() } From 0ac4d04878c4d9f7901c80e6f7f9dc96feb1ecc1 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:24:25 +0300 Subject: [PATCH 64/66] _sqlite3 --- crates/stdlib/src/_sqlite3.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/stdlib/src/_sqlite3.rs b/crates/stdlib/src/_sqlite3.rs index d55557d17d..21739a28c0 100644 --- a/crates/stdlib/src/_sqlite3.rs +++ b/crates/stdlib/src/_sqlite3.rs @@ -2211,12 +2211,11 @@ mod _sqlite3 { )] impl Row { #[pymethod] - fn keys(&self, _vm: &VirtualMachine) -> PyResult> { - Ok(self - .description + fn keys(&self, _vm: &VirtualMachine) -> Vec { + self.description .iter() .map(|x| x.downcast_ref::().unwrap().as_slice()[0].clone()) - .collect()) + .collect() } fn subscript(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult { From 35cacd97d0bcd7e86e79f939823f2d503328b50e Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:25:03 +0300 Subject: [PATCH 65/66] grp --- crates/stdlib/src/grp.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/stdlib/src/grp.rs b/crates/stdlib/src/grp.rs index eec901b0e5..0e09c69562 100644 --- a/crates/stdlib/src/grp.rs +++ b/crates/stdlib/src/grp.rs @@ -84,7 +84,7 @@ mod grp { } #[pyfunction] - fn getgrall(vm: &VirtualMachine) -> PyResult> { + fn getgrall(vm: &VirtualMachine) -> Vec { // setgrent, getgrent, etc are not thread safe. Could use fgetgrent_r, but this is easier static GETGRALL: parking_lot::Mutex<()> = parking_lot::Mutex::new(()); let _guard = GETGRALL.lock(); @@ -98,6 +98,6 @@ mod grp { } unsafe { libc::endgrent() }; - Ok(list) + list } } From fc58a80e457530fd52279d097dc38f2a99964c91 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:39:36 +0300 Subject: [PATCH 66/66] overlapped --- crates/stdlib/src/overlapped.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/stdlib/src/overlapped.rs b/crates/stdlib/src/overlapped.rs index 86238ed9ea..77c3365186 100644 --- a/crates/stdlib/src/overlapped.rs +++ b/crates/stdlib/src/overlapped.rs @@ -1867,7 +1867,7 @@ mod _overlapped { } #[pyfunction] - fn FormatMessage(error_code: u32, _vm: &VirtualMachine) -> PyResult { + fn FormatMessage(error_code: u32, _vm: &VirtualMachine) -> String { use windows_sys::Win32::Foundation::LocalFree; use windows_sys::Win32::System::Diagnostics::Debug::{ FORMAT_MESSAGE_ALLOCATE_BUFFER, FORMAT_MESSAGE_FROM_SYSTEM, @@ -1898,7 +1898,7 @@ mod _overlapped { if !buffer.is_null() { unsafe { LocalFree(buffer as *mut _) }; } - return Ok(format!("unknown error code {}", error_code)); + return format!("unknown error code {error_code}"); } // Convert to Rust string, trimming trailing whitespace @@ -1907,7 +1907,7 @@ mod _overlapped { unsafe { LocalFree(buffer as *mut _) }; - Ok(msg) + msg } #[pyfunction]