Skip to content

Commit 0e135c9

Browse files
yonmilkyouknowone
authored andcommitted
Fix warning module
1 parent 94819c5 commit 0e135c9

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

vm/src/warn.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ pub struct WarningsState {
1616

1717
impl WarningsState {
1818
fn create_filter(ctx: &Context) -> PyListRef {
19-
ctx.new_list(vec![
20-
ctx.new_str("__main__").into(),
21-
ctx.types.none_type.as_object().to_owned(),
22-
ctx.exceptions.warning.as_object().to_owned(),
23-
ctx.new_str("ACTION").into(),
24-
ctx.new_int(0).into(),
25-
])
19+
ctx.new_list(vec![ctx
20+
.new_tuple(vec![
21+
ctx.new_str("__main__").into(),
22+
ctx.types.none_type.as_object().to_owned(),
23+
ctx.exceptions.warning.as_object().to_owned(),
24+
ctx.new_str("ACTION").into(),
25+
ctx.new_int(0).into(),
26+
])
27+
.into()])
2628
}
2729

2830
pub fn init_state(ctx: &Context) -> WarningsState {
@@ -44,8 +46,8 @@ fn check_matched(obj: &PyObjectRef, arg: &PyObjectRef, vm: &VirtualMachine) -> P
4446
return Ok(false);
4547
}
4648

47-
let result = vm.invoke(obj, (arg.to_owned(),))?;
48-
result.is_true(vm)
49+
let result = vm.invoke(obj, (arg.to_owned(),));
50+
Ok(result.is_ok())
4951
}
5052

5153
pub fn py_warn(
@@ -95,9 +97,9 @@ fn get_filter(
9597
text: PyObjectRef,
9698
lineno: usize,
9799
module: PyObjectRef,
98-
mut _item: PyObjectRef,
100+
mut _item: PyTupleRef,
99101
vm: &VirtualMachine,
100-
) -> PyResult<PyObjectRef> {
102+
) -> PyResult {
101103
let filters = vm.state.warnings.filters.as_object().to_owned();
102104

103105
let filters: PyListRef = filters
@@ -109,55 +111,53 @@ fn get_filter(
109111
let tmp_item = filters.borrow_vec().get(i).cloned();
110112
let tmp_item = if let Some(tmp_item) = tmp_item {
111113
let tmp_item = PyTupleRef::try_from_object(vm, tmp_item)?;
112-
if tmp_item.len() != 5 {
113-
Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i)))
114-
} else {
114+
if tmp_item.len() == 5 {
115115
Ok(tmp_item)
116+
} else {
117+
Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i)))
116118
}
117119
} else {
118120
Err(vm.new_value_error(format!("_warnings.filters item {} isn't a 5-tuple", i)))
119121
}?;
120122

121123
/* Python code: action, msg, cat, mod, ln = item */
122-
let action = tmp_item.get(0);
123-
let msg = tmp_item.get(1);
124-
let cat = tmp_item.get(2);
125-
let item_mod = tmp_item.get(3);
126-
let ln_obj = tmp_item.get(4);
127-
128-
let action = if let Some(action) = action {
124+
let action = if let Some(action) = tmp_item.get(0) {
129125
action.str(vm).map(|action| action.into_object())
130126
} else {
131127
Err(vm.new_type_error("action must be a string".to_string()))
132128
};
133129

134-
let good_msg = if let Some(msg) = msg {
130+
let good_msg = if let Some(msg) = tmp_item.get(1) {
135131
check_matched(msg, &text, vm)?
136132
} else {
137133
false
138134
};
139135

140-
let good_mod = if let Some(item_mod) = item_mod {
141-
check_matched(item_mod, &module, vm)?
136+
let is_subclass = if let Some(cat) = tmp_item.get(2) {
137+
category.fast_isinstance(&cat.class())
142138
} else {
143139
false
144140
};
145141

146-
let is_subclass = if let Some(cat) = cat {
147-
category.fast_isinstance(&cat.class())
142+
let good_mod = if let Some(item_mod) = tmp_item.get(3) {
143+
check_matched(item_mod, &module, vm)?
148144
} else {
149145
false
150146
};
151147

152-
// I would like some help on how to deal with it
153-
let ln = if let Some(ln_obj) = ln_obj {
154-
ln_obj.length(vm)?
155-
} else {
156-
0
157-
};
148+
let ln = tmp_item.get(4).map_or_else(
149+
|| 0,
150+
|ln_obj| {
151+
if let Ok(ln) = ln_obj.try_int(vm) {
152+
ln.as_u32_mask() as usize
153+
} else {
154+
0
155+
}
156+
},
157+
);
158158

159159
if good_msg && good_mod && is_subclass && (ln == 0 || lineno == ln) {
160-
_item = tmp_item.into_object();
160+
_item = tmp_item;
161161
return action;
162162
}
163163
}
@@ -212,7 +212,7 @@ fn normalize_module(filename: PyStrRef, vm: &VirtualMachine) -> Option<PyObjectR
212212

213213
if len == 0 {
214214
Some(vm.new_pyobj("<unknown>"))
215-
} else if len >= 3 && filename.as_str().contains(".py") {
215+
} else if len >= 3 && filename.as_str().ends_with(".py") {
216216
Some(vm.new_pyobj(&filename.as_str()[..len - 3]))
217217
} else {
218218
Some(filename.as_object().to_owned())
@@ -241,6 +241,9 @@ fn warn_explicit(
241241
None => return Ok(()),
242242
};
243243

244+
// Normalize message.
245+
let text = message.as_str();
246+
244247
let category = if let Some(category) = category {
245248
if !category.fast_issubclass(vm.ctx.exceptions.warning) {
246249
return Err(vm.new_type_error(format!(
@@ -253,19 +256,17 @@ fn warn_explicit(
253256
vm.ctx.exceptions.user_warning.to_owned()
254257
};
255258

256-
// Normalize message.
257-
let (category, text) = if message.fast_isinstance(vm.ctx.exceptions.warning) {
258-
(message.class().into_owned(), message.as_object().str(vm)?)
259+
let category = if message.fast_isinstance(vm.ctx.exceptions.warning) {
260+
message.class().into_owned()
259261
} else {
260-
(category, message)
261-
// (category, message.to_owned())
262+
category
262263
};
263264

264265
// Create key.
265266
let key = PyTuple::new_ref(
266267
vec![
267268
vm.ctx.new_int(3).into(),
268-
vm.ctx.new_str(text.as_str()).into(),
269+
vm.ctx.new_str(text).into(),
269270
category.as_object().to_owned(),
270271
vm.ctx.new_int(lineno).into(),
271272
],
@@ -276,25 +277,24 @@ fn warn_explicit(
276277
{
277278
return Ok(());
278279
}
279-
// Else this warning hasn't been generated before.
280280

281-
let item = vm.ctx.new_tuple(vec![]).into();
282-
let _action = get_filter(
281+
let item = vm.ctx.new_tuple(vec![]);
282+
let action = get_filter(
283283
category.as_object().to_owned(),
284-
vm.ctx.new_str(text.as_str()).into(),
284+
vm.ctx.new_str(text).into(),
285285
lineno,
286286
module,
287287
item,
288288
vm,
289-
);
289+
)?;
290290

291-
// if action.str(vm)?.as_str().eq("error") {
292-
// return Err(vm.new_type_error(message.to_string()));
293-
// }
291+
if action.str(vm)?.as_str().eq("error") {
292+
return Err(vm.new_type_error(message.to_string()));
293+
}
294294

295-
// if action.str(vm)?.as_str().eq("ignore") {
296-
// return Ok(());
297-
// }
295+
if action.str(vm)?.as_str().eq("ignore") {
296+
return Ok(());
297+
}
298298

299299
let stderr = crate::stdlib::sys::PyStderr(vm);
300300
writeln!(stderr, "{}: {}", category.name(), text,);

0 commit comments

Comments
 (0)