From aea4d509c91d89ea432e833222801013fca86436 Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" Date: Sat, 26 Aug 2023 23:21:32 +0900 Subject: [PATCH 1/3] Fix win_lib_path to actually work --- pylib/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylib/build.rs b/pylib/build.rs index 1ea80e25a..52a2e3a63 100644 --- a/pylib/build.rs +++ b/pylib/build.rs @@ -9,7 +9,7 @@ fn main() { if cfg!(windows) { if let Ok(real_path) = std::fs::read_to_string("Lib") { - println!("rustc-env:win_lib_path={real_path:?}"); + println!("cargo:rustc-env=win_lib_path={real_path:?}"); } } } From d4d362a9ca0ce6b1cdfa4b631cdc5d427a99005b Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" Date: Sat, 26 Aug 2023 17:22:17 +0900 Subject: [PATCH 2/3] Update to Rust 1.72.0 --- compiler/codegen/src/compile.rs | 2 +- compiler/core/src/marshal.rs | 4 +- derive-impl/src/pyclass.rs | 5 +- derive-impl/src/util.rs | 22 ++++++--- src/interpreter.rs | 2 +- stdlib/src/array.rs | 16 +++---- stdlib/src/socket.rs | 4 +- stdlib/src/sqlite.rs | 84 +++++++++++++++++++++++++-------- vm/src/builtins/dict.rs | 1 + vm/src/builtins/range.rs | 2 +- vm/src/function/mod.rs | 2 +- vm/src/function/protocol.rs | 4 +- vm/src/object/core.rs | 8 ++-- vm/src/protocol/callable.rs | 7 ++- vm/src/stdlib/posix.rs | 2 +- 15 files changed, 114 insertions(+), 51 deletions(-) diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index 359ecd860..217c2dc02 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -976,7 +976,7 @@ impl Compiler { .chain(&args.posonlyargs) .chain(&args.args) .map(|arg| arg.as_arg()) - .chain(kw_without_defaults.into_iter()) + .chain(kw_without_defaults) .chain(kw_with_defaults.into_iter().map(|(arg, _)| arg)); for name in args_iter { self.varname(name.arg.as_str())?; diff --git a/compiler/core/src/marshal.rs b/compiler/core/src/marshal.rs index a5d3089ff..9f3bb8e22 100644 --- a/compiler/core/src/marshal.rs +++ b/compiler/core/src/marshal.rs @@ -484,7 +484,9 @@ impl Write for Vec { } pub(crate) fn write_len(buf: &mut W, len: usize) { - let Ok(len) = len.try_into() else { panic!("too long to serialize") }; + let Ok(len) = len.try_into() else { + panic!("too long to serialize") + }; buf.write_u32(len); } diff --git a/derive-impl/src/pyclass.rs b/derive-impl/src/pyclass.rs index 3f7881107..55705c742 100644 --- a/derive-impl/src/pyclass.rs +++ b/derive-impl/src/pyclass.rs @@ -1327,7 +1327,10 @@ impl ItemMeta for SlotItemMeta { Some(HashMap::default()) }; let (Some(meta_map), None) = (meta_map, nested.next()) else { - bail_span!(meta_ident, "#[pyslot] must be of the form #[pyslot] or #[pyslot(slot_name)]") + bail_span!( + meta_ident, + "#[pyslot] must be of the form #[pyslot] or #[pyslot(slot_name)]" + ) }; Ok(Self::from_inner(ItemMetaInner { item_ident, diff --git a/derive-impl/src/util.rs b/derive-impl/src/util.rs index c7b98ab2c..9f827f9e9 100644 --- a/derive-impl/src/util.rs +++ b/derive-impl/src/util.rs @@ -167,9 +167,16 @@ impl ItemMetaInner { pub fn _optional_str(&self, key: &str) -> Result> { let value = if let Some((_, meta)) = self.meta_map.get(key) { let Meta::NameValue(syn::MetaNameValue { - lit: syn::Lit::Str(lit), .. - }) = meta else { - bail_span!(meta, "#[{}({} = ...)] must exist as a string", self.meta_name(), key) + lit: syn::Lit::Str(lit), + .. + }) = meta + else { + bail_span!( + meta, + "#[{}({} = ...)] must exist as a string", + self.meta_name(), + key + ) }; Some(lit.value()) } else { @@ -203,7 +210,10 @@ impl ItemMetaInner { key: &str, ) -> Result>> { let value = if let Some((_, meta)) = self.meta_map.get(key) { - let Meta::List(syn::MetaList { path: _, nested, .. }) = meta else { + let Meta::List(syn::MetaList { + path: _, nested, .. + }) = meta + else { bail_span!(meta, "#[{}({}(...))] must be a list", self.meta_name(), key) }; Some(nested.into_iter()) @@ -445,11 +455,11 @@ impl ExceptionItemMeta { return Ok({ let type_name = inner.item_name(); let Some(py_name) = type_name.as_str().strip_prefix("Py") else { - bail_span!( + bail_span!( inner.item_ident, "#[pyexception] expects its underlying type to be named `Py` prefixed" ) - }; + }; py_name.to_string() }) } diff --git a/src/interpreter.rs b/src/interpreter.rs index 89b0bc6e0..39a9662cd 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -66,6 +66,6 @@ pub fn init_stdlib(vm: &mut VirtualMachine) { .push(rustpython_pylib::LIB_PATH.to_owned()) } - settings.path_list.extend(path_list.into_iter()); + settings.path_list.extend(path_list); } } diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index b0739e768..a0beb5d1e 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -1484,14 +1484,14 @@ mod array { _ => false, }; match code { - 0 | 1 => Ok(Self::Int8 { signed }), - 2 | 3 | 4 | 5 => Ok(Self::Int16 { signed, big_endian }), - 6 | 7 | 8 | 9 => Ok(Self::Int32 { signed, big_endian }), - 10 | 11 | 12 | 13 => Ok(Self::Int64 { signed, big_endian }), - 14 | 15 => Ok(Self::Ieee754Float { big_endian }), - 16 | 17 => Ok(Self::Ieee754Double { big_endian }), - 18 | 19 => Ok(Self::Utf16 { big_endian }), - 20 | 21 => Ok(Self::Utf32 { big_endian }), + 0..=1 => Ok(Self::Int8 { signed }), + 2..=5 => Ok(Self::Int16 { signed, big_endian }), + 6..=9 => Ok(Self::Int32 { signed, big_endian }), + 10..=13 => Ok(Self::Int64 { signed, big_endian }), + 14..=15 => Ok(Self::Ieee754Float { big_endian }), + 16..=17 => Ok(Self::Ieee754Double { big_endian }), + 18..=19 => Ok(Self::Utf16 { big_endian }), + 20..=21 => Ok(Self::Utf32 { big_endian }), _ => Err(code), } } diff --git a/stdlib/src/socket.rs b/stdlib/src/socket.rs index 1543d56e5..b417a1739 100644 --- a/stdlib/src/socket.rs +++ b/stdlib/src/socket.rs @@ -935,7 +935,7 @@ mod _socket { )) })?; match tuple.len() { - 2 | 3 | 4 => {} + 2..=4 => {} _ => return Err(vm.new_type_error( "AF_INET6 address must be a tuple (host, port[, flowinfo[, scopeid]])" .to_owned(), @@ -1914,7 +1914,7 @@ mod _socket { vm: &VirtualMachine, ) -> Result<(String, String), IoOrPyException> { match address.len() { - 2 | 3 | 4 => {} + 2..=4 => {} _ => { return Err(vm .new_type_error("illegal sockaddr argument".to_owned()) diff --git a/stdlib/src/sqlite.rs b/stdlib/src/sqlite.rs index cb8c5f89c..ecc5fea71 100644 --- a/stdlib/src/sqlite.rs +++ b/stdlib/src/sqlite.rs @@ -99,8 +99,11 @@ mod _sqlite { )* fn setup_module_exceptions(module: &PyObject, vm: &VirtualMachine) { $( - let exception = [<$x:snake:upper>].get_or_init( - || vm.ctx.new_exception_type("_sqlite3", stringify!($x), Some(vec![$base(vm).to_owned()]))); + #[allow(clippy::redundant_closure_call)] + let exception = [<$x:snake:upper>].get_or_init(|| { + let base = $base(vm); + vm.ctx.new_exception_type("_sqlite3", stringify!($x), Some(vec![base.to_owned()])) + }); module.set_attr(stringify!($x), exception.clone().into_object(), vm).unwrap(); )* } @@ -455,7 +458,9 @@ mod _sqlite { let context = SqliteContext::from(context); let (_, vm) = (*context.user_data::()).retrieve(); let instance = context.aggregate_context::<*const PyObject>(); - let Some(instance) = (*instance).as_ref() else { return; }; + let Some(instance) = (*instance).as_ref() else { + return; + }; Self::callback_result_from_method(context, instance, "finalize", vm); } @@ -895,7 +900,7 @@ mod _sqlite { let cursor = cursor.downcast::().map_err(|x| { vm.new_type_error(format!("factory must return a cursor, not {}", x.class())) })?; - unsafe { cursor.row_factory.swap(zelf.row_factory.to_owned()) }; + let _ = unsafe { cursor.row_factory.swap(zelf.row_factory.to_owned()) }; cursor } else { let row_factory = zelf.row_factory.to_owned(); @@ -1077,7 +1082,17 @@ mod _sqlite { }; let db = self.db_lock(vm)?; let Some(data) = CallbackData::new(args.func, vm) else { - return db.create_function(name.as_ptr(), args.narg, flags, null_mut(), None, None, None, None, vm); + return db.create_function( + name.as_ptr(), + args.narg, + flags, + null_mut(), + None, + None, + None, + None, + vm, + ); }; db.create_function( @@ -1098,7 +1113,17 @@ mod _sqlite { let name = args.name.to_cstring(vm)?; let db = self.db_lock(vm)?; let Some(data) = CallbackData::new(args.aggregate_class, vm) else { - return db.create_function(name.as_ptr(), args.narg, SQLITE_UTF8, null_mut(), None, None, None, None, vm); + return db.create_function( + name.as_ptr(), + args.narg, + SQLITE_UTF8, + null_mut(), + None, + None, + None, + None, + vm, + ); }; db.create_function( @@ -1125,7 +1150,14 @@ mod _sqlite { let db = self.db_lock(vm)?; let Some(data) = CallbackData::new(callable.clone(), vm) else { unsafe { - sqlite3_create_collation_v2(db.db, name.as_ptr(), SQLITE_UTF8, null_mut(), None, None); + sqlite3_create_collation_v2( + db.db, + name.as_ptr(), + SQLITE_UTF8, + null_mut(), + None, + None, + ); } return Ok(()); }; @@ -1149,7 +1181,7 @@ mod _sqlite { // TODO: replace with Result.inspect_err when stable if let Err(exc) = db.check(ret, vm) { // create_collation do not call destructor if error occur - unsafe { Box::from_raw(data) }; + let _ = unsafe { Box::from_raw(data) }; Err(exc) } else { Ok(()) @@ -1168,7 +1200,18 @@ mod _sqlite { let db = self.db_lock(vm)?; let Some(data) = CallbackData::new(aggregate_class, vm) else { unsafe { - sqlite3_create_window_function(db.db, name.as_ptr(), narg, SQLITE_UTF8, null_mut(), None, None, None, None, None) + sqlite3_create_window_function( + db.db, + name.as_ptr(), + narg, + SQLITE_UTF8, + null_mut(), + None, + None, + None, + None, + None, + ) }; return Ok(()); }; @@ -1214,10 +1257,8 @@ mod _sqlite { #[pymethod] fn set_trace_callback(&self, callable: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { let db = self.db_lock(vm)?; - let Some(data )= CallbackData::new(callable, vm) else { - unsafe { - sqlite3_trace_v2(db.db, SQLITE_TRACE_STMT as u32, None, null_mut()) - }; + let Some(data) = CallbackData::new(callable, vm) else { + unsafe { sqlite3_trace_v2(db.db, SQLITE_TRACE_STMT as u32, None, null_mut()) }; return Ok(()); }; @@ -1241,7 +1282,7 @@ mod _sqlite { vm: &VirtualMachine, ) -> PyResult<()> { let db = self.db_lock(vm)?; - let Some(data )= CallbackData::new(callable, vm) else { + let Some(data) = CallbackData::new(callable, vm) else { unsafe { sqlite3_progress_handler(db.db, n, None, null_mut()) }; return Ok(()); }; @@ -1310,7 +1351,7 @@ mod _sqlite { if let Some(val) = &val { begin_statement_ptr_from_isolation_level(val, vm)?; } - unsafe { self.isolation_level.swap(val) }; + let _ = unsafe { self.isolation_level.swap(val) }; Ok(()) } @@ -1320,7 +1361,7 @@ mod _sqlite { } #[pygetset(setter)] fn set_text_factory(&self, val: PyObjectRef) { - unsafe { self.text_factory.swap(val) }; + let _ = unsafe { self.text_factory.swap(val) }; } #[pygetset] @@ -1329,7 +1370,7 @@ mod _sqlite { } #[pygetset(setter)] fn set_row_factory(&self, val: Option) { - unsafe { self.row_factory.swap(val) }; + let _ = unsafe { self.row_factory.swap(val) }; } fn check_thread(&self, vm: &VirtualMachine) -> PyResult<()> { @@ -1841,7 +1882,9 @@ mod _sqlite { } else if let Some(name) = needle.payload::() { for (obj, i) in self.description.iter().zip(0..) { let obj = &obj.payload::().unwrap().as_slice()[0]; - let Some(obj) = obj.payload::() else { break; }; + let Some(obj) = obj.payload::() else { + break; + }; let a_iter = name.as_str().chars().flat_map(|x| x.to_uppercase()); let b_iter = obj.as_str().chars().flat_map(|x| x.to_uppercase()); @@ -2153,7 +2196,10 @@ mod _sqlite { if let Some(index) = needle.try_index_opt(vm) { let Some(value) = value.payload::() else { - return Err(vm.new_type_error(format!("'{}' object cannot be interpreted as an integer", value.class()))); + return Err(vm.new_type_error(format!( + "'{}' object cannot be interpreted as an integer", + value.class() + ))); }; let value = value.try_to_primitive::(vm)?; let blob_len = inner.blob.bytes(); diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index a164fc11b..1a323b4c4 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -784,6 +784,7 @@ macro_rules! dict_view { &self.dict } fn item(vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef) -> PyObjectRef { + #[allow(clippy::redundant_closure_call)] $result_fn(vm, key, value) } fn reversed(&self) -> Self::ReverseIter { diff --git a/vm/src/builtins/range.rs b/vm/src/builtins/range.rs index 1543e2e56..348924a7a 100644 --- a/vm/src/builtins/range.rs +++ b/vm/src/builtins/range.rs @@ -287,7 +287,7 @@ impl PyRange { #[pymethod(magic)] fn reduce(&self, vm: &VirtualMachine) -> (PyTypeRef, PyTupleRef) { - let range_parameters: Vec = vec![&self.start, &self.stop, &self.step] + let range_parameters: Vec = [&self.start, &self.stop, &self.step] .iter() .map(|x| x.as_object().to_owned()) .collect(); diff --git a/vm/src/function/mod.rs b/vm/src/function/mod.rs index 409e4f1db..1c3babd80 100644 --- a/vm/src/function/mod.rs +++ b/vm/src/function/mod.rs @@ -15,7 +15,6 @@ pub use argument::{ }; pub use arithmetic::{PyArithmeticValue, PyComparisonValue}; pub use buffer::{ArgAsciiBuffer, ArgBytesLike, ArgMemoryBuffer, ArgStrOrBytesLike}; -pub(self) use builtin::{BorrowedParam, OwnedParam, RefParam}; pub use builtin::{IntoPyNativeFn, PyNativeFn}; pub use either::Either; pub use fspath::FsPath; @@ -26,6 +25,7 @@ pub use number::{ArgIndex, ArgIntoBool, ArgIntoComplex, ArgIntoFloat, ArgPrimiti pub use protocol::{ArgCallable, ArgIterable, ArgMapping, ArgSequence}; use crate::{builtins::PyStr, convert::TryFromBorrowedObject, PyObject, PyResult, VirtualMachine}; +use builtin::{BorrowedParam, OwnedParam, RefParam}; #[derive(Clone, Copy, PartialEq, Eq)] pub enum ArgByteOrder { diff --git a/vm/src/function/protocol.rs b/vm/src/function/protocol.rs index a70dd9e53..14aa5ad84 100644 --- a/vm/src/function/protocol.rs +++ b/vm/src/function/protocol.rs @@ -58,7 +58,9 @@ impl From for PyObjectRef { impl TryFromObject for ArgCallable { fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { let Some(callable) = obj.to_callable() else { - return Err(vm.new_type_error(format!("'{}' object is not callable", obj.class().name()))); + return Err( + vm.new_type_error(format!("'{}' object is not callable", obj.class().name())) + ); }; let call = callable.call; Ok(ArgCallable { obj, call }) diff --git a/vm/src/object/core.rs b/vm/src/object/core.rs index c5e42229d..bda8aaba2 100644 --- a/vm/src/object/core.rs +++ b/vm/src/object/core.rs @@ -1112,6 +1112,8 @@ macro_rules! partially_init { Uninit { $($uninit_field:ident),*$(,)? }$(,)? ) => {{ // check all the fields are there but *don't* actually run it + + #[allow(clippy::diverging_sub_expression)] // FIXME: better way than using `if false`? if false { #[allow(invalid_value, dead_code, unreachable_code)] let _ = {$ty { @@ -1185,10 +1187,8 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) { Uninit { typ }, ))); - let object_type_ptr = - object_type_ptr as *mut MaybeUninit> as *mut PyInner; - let type_type_ptr = - type_type_ptr as *mut MaybeUninit> as *mut PyInner; + let object_type_ptr = object_type_ptr as *mut PyInner; + let type_type_ptr = type_type_ptr as *mut PyInner; unsafe { (*type_type_ptr).ref_count.inc(); diff --git a/vm/src/protocol/callable.rs b/vm/src/protocol/callable.rs index e8c8b45c7..8a04e2021 100644 --- a/vm/src/protocol/callable.rs +++ b/vm/src/protocol/callable.rs @@ -26,10 +26,9 @@ impl PyObject { pub fn call_with_args(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult { vm_trace!("Invoke: {:?} {:?}", callable, args); let Some(callable) = self.to_callable() else { - return Err(vm.new_type_error(format!( - "'{}' object is not callable", - self.class().name() - ))); + return Err( + vm.new_type_error(format!("'{}' object is not callable", self.class().name())) + ); }; callable.invoke(args, vm) } diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index e9d705b4e..1adf0006e 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -1343,7 +1343,7 @@ pub mod module { .to_vec(); keys.into_iter() - .zip(values.into_iter()) + .zip(values) .map(|(k, v)| { let k = OsPath::try_from_object(vm, k)?.into_bytes(); let v = OsPath::try_from_object(vm, v)?.into_bytes(); From 03f954408a72a7948c4ee1bcb2b27e258a20a913 Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" Date: Sun, 27 Aug 2023 00:32:17 +0900 Subject: [PATCH 3/3] Fix windows SSL bug --- stdlib/src/ssl.rs | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/stdlib/src/ssl.rs b/stdlib/src/ssl.rs index a6ba30375..746bc5391 100644 --- a/stdlib/src/ssl.rs +++ b/stdlib/src/ssl.rs @@ -636,11 +636,19 @@ mod _ssl { ); } + #[cold] + fn invalid_cadata(vm: &VirtualMachine) -> PyBaseExceptionRef { + vm.new_type_error( + "cadata should be an ASCII string or a bytes-like object".to_owned(), + ) + } + + // validate cadata type and load cadata if let Some(cadata) = args.cadata { let certs = match cadata { Either::A(s) => { if !s.is_ascii() { - return Err(vm.new_type_error("Must be an ascii string".to_owned())); + return Err(invalid_cadata(vm)); } X509::stack_from_pem(s.as_str().as_bytes()) } @@ -1191,6 +1199,7 @@ mod _ssl { vm.new_exception_msg(cls, msg.to_owned()) } + // SSL_FILETYPE_ASN1 part of _add_ca_certs in CPython fn x509_stack_from_der(der: &[u8]) -> Result, ErrorStack> { unsafe { openssl::init(); @@ -1198,20 +1207,25 @@ mod _ssl { let mut certs = vec![]; loop { - let r = sys::d2i_X509_bio(bio.as_ptr(), std::ptr::null_mut()); - if r.is_null() { - let err = sys::ERR_peek_last_error(); - if sys::ERR_GET_LIB(err) == sys::ERR_LIB_ASN1 - && sys::ERR_GET_REASON(err) == sys::ASN1_R_HEADER_TOO_LONG - { - sys::ERR_clear_error(); - break; - } - - return Err(ErrorStack::get()); - } else { - certs.push(X509::from_ptr(r)); + let cert = sys::d2i_X509_bio(bio.as_ptr(), std::ptr::null_mut()); + if cert.is_null() { + break; } + certs.push(X509::from_ptr(cert)); + } + + let err = sys::ERR_peek_last_error(); + + if certs.is_empty() { + // let msg = if filetype == sys::SSL_FILETYPE_PEM { + // "no start line: cadata does not contain a certificate" + // } else { + // "not enough data: cadata does not contain a certificate" + // }; + return Err(ErrorStack::get()); + } + if err != 0 { + return Err(ErrorStack::get()); } Ok(certs)