From eb27d1e8d7b38f0cde32a782499da7f95c54f08a Mon Sep 17 00:00:00 2001 From: "Kim, YeonWoo" Date: Thu, 1 Sep 2022 00:30:35 +0900 Subject: [PATCH 1/5] Update warning warn_explicit --- vm/src/vm/mod.rs | 8 +- vm/src/warn.rs | 189 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 186 insertions(+), 11 deletions(-) diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs index 6a37864ec..e8091be1f 100644 --- a/vm/src/vm/mod.rs +++ b/vm/src/vm/mod.rs @@ -31,7 +31,9 @@ use crate::{ import, protocol::PyIterIter, scope::Scope, - signal, stdlib, AsObject, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, + signal, stdlib, + warn::WarningsState, + AsObject, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, }; use crossbeam_utils::atomic::AtomicCell; use std::sync::atomic::AtomicBool; @@ -88,6 +90,7 @@ pub struct PyGlobalState { pub atexit_funcs: PyMutex>, pub codec_registry: CodecsRegistry, pub finalizing: AtomicBool, + pub warnings: WarningsState, } pub fn process_hash_secret_seed() -> u32 { @@ -136,6 +139,8 @@ impl VirtualMachine { let codec_registry = CodecsRegistry::new(&ctx); + let warnings = WarningsState::init_state(&ctx); + let mut vm = VirtualMachine { builtins, sys_module, @@ -161,6 +166,7 @@ impl VirtualMachine { atexit_funcs: PyMutex::default(), codec_registry, finalizing: AtomicBool::new(false), + warnings, }), initialized: false, recursion_depth: Cell::new(0), diff --git a/vm/src/warn.rs b/vm/src/warn.rs index bb51d54b8..5bce2b3f5 100644 --- a/vm/src/warn.rs +++ b/vm/src/warn.rs @@ -1,8 +1,39 @@ +use itertools::Itertools; + use crate::{ - builtins::{PyDict, PyStrRef, PyType, PyTypeRef}, - AsObject, Py, PyObjectRef, PyResult, VirtualMachine, + builtins::{PyDict, PyDictRef, PyListRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef}, + convert::TryFromObject, + AsObject, Context, Py, PyObjectRef, PyResult, VirtualMachine, }; +pub struct WarningsState { + filters: PyListRef, + _once_registry: PyDictRef, + _default_action: PyStrRef, + filters_version: usize, +} + +impl WarningsState { + fn create_filter(ctx: &Context) -> PyListRef { + ctx.new_list(vec![ + ctx.new_str("__main__").into(), + ctx.types.none_type.as_object().to_owned(), + ctx.exceptions.warning.as_object().to_owned(), + ctx.new_str("ACTION").into(), + ctx.new_int(0).into(), + ]) + } + + pub fn init_state(ctx: &Context) -> WarningsState { + WarningsState { + filters: Self::create_filter(ctx), + _once_registry: PyDict::new_ref(ctx), + _default_action: ctx.new_str("default"), + filters_version: 0, + } + } +} + pub fn py_warn( category: &Py, message: String, @@ -31,19 +62,130 @@ pub fn warn( ) } +fn get_filter( + _category: PyObjectRef, + _text: PyObjectRef, + _lineno: usize, + _module: PyObjectRef, + mut _item: PyObjectRef, + vm: &VirtualMachine, +) -> PyResult<()> { + let filters = vm.state.warnings.filters.as_object().to_owned(); + + let filters: PyListRef = filters + .try_into_value(vm) + .map_err(|_| vm.new_value_error("_warnings.filters must be a list".to_string()))?; + + /* WarningsState.filters could change while we are iterating over it. */ + for i in 0..filters.borrow_vec().len() { + let tmp_item = filters.borrow_vec().get(i).cloned(); + let tmp_item = if let Some(tmp_item) = tmp_item { + let tmp_item = PyTupleRef::try_from_object(vm, tmp_item)?; + if tmp_item.len() != 5 { + Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i))) + } else { + Ok(tmp_item) + } + } else { + Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i))) + }?; + + /* Python code: action, msg, cat, mod, ln = item */ + let _action = tmp_item.get(0); + let _msg = tmp_item.get(1); + let _cat = tmp_item.get(2); + let _item_mod = tmp_item.get(3); + let _ln_obj = tmp_item.get(4); + } + + Ok(()) +} + +fn already_warned( + registry: PyObjectRef, + key: PyObjectRef, + should_set: usize, + vm: &VirtualMachine, +) -> bool { + let version_obj = registry.get_item("version", vm).ok(); + let filters_version = vm + .ctx + .new_int(vm.state.warnings.filters_version) + .as_object() + .to_owned(); + + if version_obj.is_none() + || version_obj.as_ref().unwrap().try_int(vm).is_err() + || !version_obj.unwrap().is(&filters_version) + { + let registry = registry.dict(); + registry.as_ref().map(|registry| { + registry.into_iter().collect_vec().clear(); + registry + }); + + if let Some(registry) = registry { + if registry.set_item("version", filters_version, vm).is_err() { + return false; + } + } + } else { + let already_warned = registry.get_item(key.as_ref(), vm); + return already_warned.is_ok(); + } + + /* This warning wasn't found in the registry, set it. */ + if should_set != 0 { + registry.set_item(key.as_ref(), vm.new_pyobj(""), vm).ok(); + } + + false +} + +fn normalize_module(filename: PyStrRef, vm: &VirtualMachine) -> Option { + let len = filename.char_len(); + + if len == 0 { + Some(vm.new_pyobj("")) + } else if len >= 3 && filename.as_str().contains(".py") { + Some(vm.new_pyobj(filename.as_str().replace(".py", ""))) + } else { + Some(filename.as_object().to_owned()) + } +} + #[allow(clippy::too_many_arguments)] fn warn_explicit( category: Option, message: PyStrRef, - _filename: PyStrRef, - _lineno: usize, - _module: PyObjectRef, - _registry: PyObjectRef, + filename: PyStrRef, + lineno: usize, + module: Option, + registry: PyObjectRef, _source_line: Option, _source: Option, vm: &VirtualMachine, ) -> PyResult<()> { - // TODO: Implement correctly + if registry.clone().is_true(vm)? + && !registry.class().is(vm.ctx.types.dict_type) + && !registry.class().is(vm.ctx.types.none_type) + { + return Err(vm.new_type_error("'registry' must be a dict or None".to_string())); + } + + // Normalize module. + let module = module.and(normalize_module(filename, vm)); + let module = if let Some(module) = module { + module + } else { + return Ok(()); + }; + + // Normalize message. + let text = message.as_str(); + if text.is_empty() { + return Ok(()); + } let category = if let Some(category) = category { if !category.fast_issubclass(vm.ctx.exceptions.warning) { return Err(vm.new_type_error(format!( @@ -55,8 +197,35 @@ fn warn_explicit( } else { vm.ctx.exceptions.user_warning.to_owned() }; + + // Create key. + let key = PyTuple::new_ref( + vec![ + vm.ctx.new_int(3).into(), + vm.ctx.new_str(text).into(), + category.as_object().to_owned(), + vm.ctx.new_int(lineno).into(), + ], + &vm.ctx, + ); + + if already_warned(registry, key.as_object().to_owned(), 0, vm) { + return Ok(()); + } + // Else this warning hasn't been generated before. + + let item = vm.ctx.new_tuple(vec![]).into(); + let _action = get_filter( + category.as_object().to_owned(), + vm.ctx.new_str(text).into(), + lineno, + module, + item, + vm, + ); + let stderr = crate::stdlib::sys::PyStderr(vm); - writeln!(stderr, "{}: {}", category.name(), message.as_str(),); + writeln!(stderr, "{}: {}", category.name(), text,); Ok(()) } @@ -67,7 +236,7 @@ fn setup_context( vm: &VirtualMachine, ) -> PyResult< // filename, lineno, module, registry - (PyStrRef, usize, PyObjectRef, PyObjectRef), + (PyStrRef, usize, Option, PyObjectRef), > { let __warningregistry__ = "__warningregistry__"; let __name__ = "__name__"; @@ -120,5 +289,5 @@ fn setup_context( let module = globals .get_item(__name__, vm) .unwrap_or_else(|_| vm.new_pyobj("")); - Ok((filename.to_owned(), lineno, module, registry)) + Ok((filename.to_owned(), lineno, Some(module), registry)) } From 49af908a3e71f194895d797f0ef97f0f515100be Mon Sep 17 00:00:00 2001 From: "Kim, YeonWoo" Date: Tue, 13 Sep 2022 23:54:18 +0900 Subject: [PATCH 2/5] Fix warning warn_explicit --- vm/src/builtins/dict.rs | 2 +- vm/src/vm/context.rs | 1 + vm/src/warn.rs | 101 +++++++++++++++++++--------------------- 3 files changed, 51 insertions(+), 53 deletions(-) diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 7f8d26066..cccaed000 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -288,7 +288,7 @@ impl PyDict { } #[pymethod] - fn clear(&self) { + pub fn clear(&self) { self.entries.clear() } diff --git a/vm/src/vm/context.rs b/vm/src/vm/context.rs index 8482e5f9f..cde7271bc 100644 --- a/vm/src/vm/context.rs +++ b/vm/src/vm/context.rs @@ -213,6 +213,7 @@ declare_const_name! { keys, items, values, + version, update, copy, flush, diff --git a/vm/src/warn.rs b/vm/src/warn.rs index 5bce2b3f5..a9c1949c8 100644 --- a/vm/src/warn.rs +++ b/vm/src/warn.rs @@ -1,8 +1,6 @@ -use itertools::Itertools; - use crate::{ builtins::{PyDict, PyDictRef, PyListRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef}, - convert::TryFromObject, + convert::{IntoObject, TryFromObject}, AsObject, Context, Py, PyObjectRef, PyResult, VirtualMachine, }; @@ -104,42 +102,43 @@ fn get_filter( fn already_warned( registry: PyObjectRef, key: PyObjectRef, - should_set: usize, + should_set: bool, vm: &VirtualMachine, -) -> bool { - let version_obj = registry.get_item("version", vm).ok(); - let filters_version = vm - .ctx - .new_int(vm.state.warnings.filters_version) - .as_object() - .to_owned(); +) -> PyResult { + let version_obj = registry.get_item(identifier!(&vm.ctx, version), vm).ok(); + let filters_version = vm.ctx.new_int(vm.state.warnings.filters_version).into(); - if version_obj.is_none() - || version_obj.as_ref().unwrap().try_int(vm).is_err() - || !version_obj.unwrap().is(&filters_version) - { - let registry = registry.dict(); - registry.as_ref().map(|registry| { - registry.into_iter().collect_vec().clear(); - registry - }); - - if let Some(registry) = registry { - if registry.set_item("version", filters_version, vm).is_err() { - return false; + match version_obj { + Some(version_obj) + if version_obj.try_int(vm).is_ok() || version_obj.is(&filters_version) => + { + let already_warned = registry.get_item(key.as_ref(), vm)?; + if already_warned.is_true(vm)? { + return Ok(true); + } + } + _ => { + let registry = registry.dict(); + registry.as_ref().map(|registry| { + registry.clear(); + registry + }); + + if let Some(registry) = registry { + if registry.set_item("version", filters_version, vm).is_err() { + return Ok(false); + } } } - } else { - let already_warned = registry.get_item(key.as_ref(), vm); - return already_warned.is_ok(); } /* This warning wasn't found in the registry, set it. */ - if should_set != 0 { - registry.set_item(key.as_ref(), vm.new_pyobj(""), vm).ok(); - } - - false + Ok(if should_set { + let item = vm.ctx.true_value.clone().into(); + registry.set_item(key.as_ref(), item, vm).map(|_| true)? + } else { + false + }) } fn normalize_module(filename: PyStrRef, vm: &VirtualMachine) -> Option { @@ -148,7 +147,7 @@ fn normalize_module(filename: PyStrRef, vm: &VirtualMachine) -> Option")) } else if len >= 3 && filename.as_str().contains(".py") { - Some(vm.new_pyobj(filename.as_str().replace(".py", ""))) + Some(vm.new_pyobj(&filename.as_str()[..len - 3])) } else { Some(filename.as_object().to_owned()) } @@ -166,26 +165,16 @@ fn warn_explicit( _source: Option, vm: &VirtualMachine, ) -> PyResult<()> { - if registry.clone().is_true(vm)? - && !registry.class().is(vm.ctx.types.dict_type) - && !registry.class().is(vm.ctx.types.none_type) - { - return Err(vm.new_type_error("'registry' must be a dict or None".to_string())); - } + let registry: PyObjectRef = registry + .try_into_value(vm) + .map_err(|_| vm.new_type_error("'registry' must be a dict or None".to_owned()))?; // Normalize module. - let module = module.and(normalize_module(filename, vm)); - let module = if let Some(module) = module { - module - } else { - return Ok(()); + let module = match module.or_else(|| normalize_module(filename, vm)) { + Some(module) => module, + None => return Ok(()), }; - // Normalize message. - let text = message.as_str(); - if text.is_empty() { - return Ok(()); - } let category = if let Some(category) = category { if !category.fast_issubclass(vm.ctx.exceptions.warning) { return Err(vm.new_type_error(format!( @@ -198,18 +187,26 @@ fn warn_explicit( vm.ctx.exceptions.user_warning.to_owned() }; + // Normalize message. + let (category, text) = if message.fast_isinstance(vm.ctx.exceptions.warning) { + (message.class().into_owned(), message.as_object().str(vm)?) + } else { + (category, message) + }; + // Create key. let key = PyTuple::new_ref( vec![ vm.ctx.new_int(3).into(), - vm.ctx.new_str(text).into(), + vm.ctx.new_str(text.as_str()).into(), category.as_object().to_owned(), vm.ctx.new_int(lineno).into(), ], &vm.ctx, ); - if already_warned(registry, key.as_object().to_owned(), 0, vm) { + if !vm.is_none(registry.as_object()) && already_warned(registry, key.into_object(), false, vm)? + { return Ok(()); } // Else this warning hasn't been generated before. @@ -217,7 +214,7 @@ fn warn_explicit( let item = vm.ctx.new_tuple(vec![]).into(); let _action = get_filter( category.as_object().to_owned(), - vm.ctx.new_str(text).into(), + vm.ctx.new_str(text.as_str()).into(), lineno, module, item, From 94819c54d312f2ce07c14607707236bc9b85b570 Mon Sep 17 00:00:00 2001 From: "Kim, YeonWoo" Date: Sat, 17 Sep 2022 16:46:58 +0900 Subject: [PATCH 3/5] Update get_filter --- vm/src/warn.rs | 101 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 13 deletions(-) diff --git a/vm/src/warn.rs b/vm/src/warn.rs index a9c1949c8..97cc70f2f 100644 --- a/vm/src/warn.rs +++ b/vm/src/warn.rs @@ -1,13 +1,16 @@ +use std::convert::TryInto; + use crate::{ builtins::{PyDict, PyDictRef, PyListRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef}, convert::{IntoObject, TryFromObject}, + types::PyComparisonOp, AsObject, Context, Py, PyObjectRef, PyResult, VirtualMachine, }; pub struct WarningsState { filters: PyListRef, _once_registry: PyDictRef, - _default_action: PyStrRef, + default_action: PyStrRef, filters_version: usize, } @@ -26,12 +29,25 @@ impl WarningsState { WarningsState { filters: Self::create_filter(ctx), _once_registry: PyDict::new_ref(ctx), - _default_action: ctx.new_str("default"), + default_action: ctx.new_str("default"), filters_version: 0, } } } +fn check_matched(obj: &PyObjectRef, arg: &PyObjectRef, vm: &VirtualMachine) -> PyResult { + if obj.class().is(vm.ctx.types.none_type) { + return Ok(true); + } + + if obj.rich_compare_bool(arg, PyComparisonOp::Eq, vm)? { + return Ok(false); + } + + let result = vm.invoke(obj, (arg.to_owned(),))?; + result.is_true(vm) +} + pub fn py_warn( category: &Py, message: String, @@ -60,14 +76,28 @@ pub fn warn( ) } +fn get_default_action(vm: &VirtualMachine) -> PyResult { + vm.state + .warnings + .default_action + .clone() + .try_into() + .map_err(|_| { + vm.new_value_error(format!( + "_warnings.defaultaction must be a string, not '{}'", + vm.state.warnings.default_action + )) + }) +} + fn get_filter( - _category: PyObjectRef, - _text: PyObjectRef, - _lineno: usize, - _module: PyObjectRef, + category: PyObjectRef, + text: PyObjectRef, + lineno: usize, + module: PyObjectRef, mut _item: PyObjectRef, vm: &VirtualMachine, -) -> PyResult<()> { +) -> PyResult { let filters = vm.state.warnings.filters.as_object().to_owned(); let filters: PyListRef = filters @@ -89,14 +119,50 @@ fn get_filter( }?; /* Python code: action, msg, cat, mod, ln = item */ - let _action = tmp_item.get(0); - let _msg = tmp_item.get(1); - let _cat = tmp_item.get(2); - let _item_mod = tmp_item.get(3); - let _ln_obj = tmp_item.get(4); + let action = tmp_item.get(0); + let msg = tmp_item.get(1); + let cat = tmp_item.get(2); + let item_mod = tmp_item.get(3); + let ln_obj = tmp_item.get(4); + + let action = if let Some(action) = action { + action.str(vm).map(|action| action.into_object()) + } else { + Err(vm.new_type_error("action must be a string".to_string())) + }; + + let good_msg = if let Some(msg) = msg { + check_matched(msg, &text, vm)? + } else { + false + }; + + let good_mod = if let Some(item_mod) = item_mod { + check_matched(item_mod, &module, vm)? + } else { + false + }; + + let is_subclass = if let Some(cat) = cat { + category.fast_isinstance(&cat.class()) + } else { + false + }; + + // I would like some help on how to deal with it + let ln = if let Some(ln_obj) = ln_obj { + ln_obj.length(vm)? + } else { + 0 + }; + + if good_msg && good_mod && is_subclass && (ln == 0 || lineno == ln) { + _item = tmp_item.into_object(); + return action; + } } - Ok(()) + get_default_action(vm) } fn already_warned( @@ -192,6 +258,7 @@ fn warn_explicit( (message.class().into_owned(), message.as_object().str(vm)?) } else { (category, message) + // (category, message.to_owned()) }; // Create key. @@ -221,6 +288,14 @@ fn warn_explicit( vm, ); + // if action.str(vm)?.as_str().eq("error") { + // return Err(vm.new_type_error(message.to_string())); + // } + + // if action.str(vm)?.as_str().eq("ignore") { + // return Ok(()); + // } + let stderr = crate::stdlib::sys::PyStderr(vm); writeln!(stderr, "{}: {}", category.name(), text,); Ok(()) From 0e135c9bfb45136dc6123c4e2aef0b74a16cd8c1 Mon Sep 17 00:00:00 2001 From: "Kim, YeonWoo" Date: Sun, 18 Sep 2022 17:03:35 +0900 Subject: [PATCH 4/5] Fix warning module --- vm/src/warn.rs | 104 ++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/vm/src/warn.rs b/vm/src/warn.rs index 97cc70f2f..008c9b12e 100644 --- a/vm/src/warn.rs +++ b/vm/src/warn.rs @@ -16,13 +16,15 @@ pub struct WarningsState { impl WarningsState { fn create_filter(ctx: &Context) -> PyListRef { - ctx.new_list(vec![ - ctx.new_str("__main__").into(), - ctx.types.none_type.as_object().to_owned(), - ctx.exceptions.warning.as_object().to_owned(), - ctx.new_str("ACTION").into(), - ctx.new_int(0).into(), - ]) + ctx.new_list(vec![ctx + .new_tuple(vec![ + ctx.new_str("__main__").into(), + ctx.types.none_type.as_object().to_owned(), + ctx.exceptions.warning.as_object().to_owned(), + ctx.new_str("ACTION").into(), + ctx.new_int(0).into(), + ]) + .into()]) } pub fn init_state(ctx: &Context) -> WarningsState { @@ -44,8 +46,8 @@ fn check_matched(obj: &PyObjectRef, arg: &PyObjectRef, vm: &VirtualMachine) -> P return Ok(false); } - let result = vm.invoke(obj, (arg.to_owned(),))?; - result.is_true(vm) + let result = vm.invoke(obj, (arg.to_owned(),)); + Ok(result.is_ok()) } pub fn py_warn( @@ -95,9 +97,9 @@ fn get_filter( text: PyObjectRef, lineno: usize, module: PyObjectRef, - mut _item: PyObjectRef, + mut _item: PyTupleRef, vm: &VirtualMachine, -) -> PyResult { +) -> PyResult { let filters = vm.state.warnings.filters.as_object().to_owned(); let filters: PyListRef = filters @@ -109,55 +111,53 @@ fn get_filter( let tmp_item = filters.borrow_vec().get(i).cloned(); let tmp_item = if let Some(tmp_item) = tmp_item { let tmp_item = PyTupleRef::try_from_object(vm, tmp_item)?; - if tmp_item.len() != 5 { - Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i))) - } else { + if tmp_item.len() == 5 { Ok(tmp_item) + } else { + Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i))) } } else { Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i))) }?; /* Python code: action, msg, cat, mod, ln = item */ - let action = tmp_item.get(0); - let msg = tmp_item.get(1); - let cat = tmp_item.get(2); - let item_mod = tmp_item.get(3); - let ln_obj = tmp_item.get(4); - - let action = if let Some(action) = action { + let action = if let Some(action) = tmp_item.get(0) { action.str(vm).map(|action| action.into_object()) } else { Err(vm.new_type_error("action must be a string".to_string())) }; - let good_msg = if let Some(msg) = msg { + let good_msg = if let Some(msg) = tmp_item.get(1) { check_matched(msg, &text, vm)? } else { false }; - let good_mod = if let Some(item_mod) = item_mod { - check_matched(item_mod, &module, vm)? - } else { - false - }; - - let is_subclass = if let Some(cat) = cat { + let is_subclass = if let Some(cat) = tmp_item.get(2) { category.fast_isinstance(&cat.class()) } else { false }; - // I would like some help on how to deal with it - let ln = if let Some(ln_obj) = ln_obj { - ln_obj.length(vm)? + let good_mod = if let Some(item_mod) = tmp_item.get(3) { + check_matched(item_mod, &module, vm)? } else { - 0 + false }; + let ln = tmp_item.get(4).map_or_else( + || 0, + |ln_obj| { + if let Ok(ln) = ln_obj.try_int(vm) { + ln.as_u32_mask() as usize + } else { + 0 + } + }, + ); + if good_msg && good_mod && is_subclass && (ln == 0 || lineno == ln) { - _item = tmp_item.into_object(); + _item = tmp_item; return action; } } @@ -212,7 +212,7 @@ fn normalize_module(filename: PyStrRef, vm: &VirtualMachine) -> Option")) - } else if len >= 3 && filename.as_str().contains(".py") { + } else if len >= 3 && filename.as_str().ends_with(".py") { Some(vm.new_pyobj(&filename.as_str()[..len - 3])) } else { Some(filename.as_object().to_owned()) @@ -241,6 +241,9 @@ fn warn_explicit( None => return Ok(()), }; + // Normalize message. + let text = message.as_str(); + let category = if let Some(category) = category { if !category.fast_issubclass(vm.ctx.exceptions.warning) { return Err(vm.new_type_error(format!( @@ -253,19 +256,17 @@ fn warn_explicit( vm.ctx.exceptions.user_warning.to_owned() }; - // Normalize message. - let (category, text) = if message.fast_isinstance(vm.ctx.exceptions.warning) { - (message.class().into_owned(), message.as_object().str(vm)?) + let category = if message.fast_isinstance(vm.ctx.exceptions.warning) { + message.class().into_owned() } else { - (category, message) - // (category, message.to_owned()) + category }; // Create key. let key = PyTuple::new_ref( vec![ vm.ctx.new_int(3).into(), - vm.ctx.new_str(text.as_str()).into(), + vm.ctx.new_str(text).into(), category.as_object().to_owned(), vm.ctx.new_int(lineno).into(), ], @@ -276,25 +277,24 @@ fn warn_explicit( { return Ok(()); } - // Else this warning hasn't been generated before. - let item = vm.ctx.new_tuple(vec![]).into(); - let _action = get_filter( + let item = vm.ctx.new_tuple(vec![]); + let action = get_filter( category.as_object().to_owned(), - vm.ctx.new_str(text.as_str()).into(), + vm.ctx.new_str(text).into(), lineno, module, item, vm, - ); + )?; - // if action.str(vm)?.as_str().eq("error") { - // return Err(vm.new_type_error(message.to_string())); - // } + if action.str(vm)?.as_str().eq("error") { + return Err(vm.new_type_error(message.to_string())); + } - // if action.str(vm)?.as_str().eq("ignore") { - // return Ok(()); - // } + if action.str(vm)?.as_str().eq("ignore") { + return Ok(()); + } let stderr = crate::stdlib::sys::PyStderr(vm); writeln!(stderr, "{}: {}", category.name(), text,); From ac28407e25e92f549909262d22459d8a8dbf384f Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sat, 24 Sep 2022 13:10:09 +0900 Subject: [PATCH 5/5] fix styles --- vm/src/warn.rs | 68 +++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/vm/src/warn.rs b/vm/src/warn.rs index 008c9b12e..2005419ba 100644 --- a/vm/src/warn.rs +++ b/vm/src/warn.rs @@ -1,5 +1,3 @@ -use std::convert::TryInto; - use crate::{ builtins::{PyDict, PyDictRef, PyListRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef}, convert::{IntoObject, TryFromObject}, @@ -108,17 +106,19 @@ fn get_filter( /* WarningsState.filters could change while we are iterating over it. */ for i in 0..filters.borrow_vec().len() { - let tmp_item = filters.borrow_vec().get(i).cloned(); - let tmp_item = if let Some(tmp_item) = tmp_item { + let tmp_item = if let Some(tmp_item) = filters.borrow_vec().get(i).cloned() { let tmp_item = PyTupleRef::try_from_object(vm, tmp_item)?; if tmp_item.len() == 5 { - Ok(tmp_item) + Some(tmp_item) } else { - Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i))) + None } } else { - Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i))) - }?; + None + }; + let tmp_item = tmp_item.ok_or_else(|| { + vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i)) + })?; /* Python code: action, msg, cat, mod, ln = item */ let action = if let Some(action) = tmp_item.get(0) { @@ -145,16 +145,9 @@ fn get_filter( false }; - let ln = tmp_item.get(4).map_or_else( - || 0, - |ln_obj| { - if let Ok(ln) = ln_obj.try_int(vm) { - ln.as_u32_mask() as usize - } else { - 0 - } - }, - ); + let ln = tmp_item.get(4).map_or(0, |ln_obj| { + ln_obj.try_int(vm).map_or(0, |ln| ln.as_u32_mask() as _) + }); if good_msg && good_mod && is_subclass && (ln == 0 || lineno == ln) { _item = tmp_item; @@ -185,13 +178,10 @@ fn already_warned( } _ => { let registry = registry.dict(); - registry.as_ref().map(|registry| { + if let Some(registry) = registry.as_ref() { registry.clear(); - registry - }); - - if let Some(registry) = registry { - if registry.set_item("version", filters_version, vm).is_err() { + let r = registry.set_item("version", filters_version, vm); + if r.is_err() { return Ok(false); } } @@ -199,24 +189,24 @@ fn already_warned( } /* This warning wasn't found in the registry, set it. */ - Ok(if should_set { - let item = vm.ctx.true_value.clone().into(); - registry.set_item(key.as_ref(), item, vm).map(|_| true)? - } else { - false - }) + if !should_set { + return Ok(false); + } + + let item = vm.ctx.true_value.clone().into(); + let _ = registry.set_item(key.as_ref(), item, vm); // ignore set error + Ok(true) } fn normalize_module(filename: PyStrRef, vm: &VirtualMachine) -> Option { - let len = filename.char_len(); - - if len == 0 { - Some(vm.new_pyobj("")) - } else if len >= 3 && filename.as_str().ends_with(".py") { - Some(vm.new_pyobj(&filename.as_str()[..len - 3])) - } else { - Some(filename.as_object().to_owned()) - } + let obj = match filename.char_len() { + 0 => vm.new_pyobj(""), + len if len >= 3 && filename.as_str().ends_with(".py") => { + vm.new_pyobj(&filename.as_str()[..len - 3]) + } + _ => filename.as_object().to_owned(), + }; + Some(obj) } #[allow(clippy::too_many_arguments)]