|
| 1 | +use crate::{ |
| 2 | + builtins::{PyDict, PyStrRef, PyTypeRef}, |
| 3 | + AsObject, PyObjectRef, PyResult, VirtualMachine, |
| 4 | +}; |
| 5 | + |
| 6 | +pub fn warn( |
| 7 | + message: PyStrRef, |
| 8 | + category: Option<PyTypeRef>, |
| 9 | + stack_level: isize, |
| 10 | + source: Option<PyObjectRef>, |
| 11 | + vm: &VirtualMachine, |
| 12 | +) -> PyResult<()> { |
| 13 | + let (filename, lineno, module, registry) = setup_context(stack_level, vm)?; |
| 14 | + warn_explicit( |
| 15 | + category, message, filename, lineno, module, registry, None, source, vm, |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +#[allow(clippy::too_many_arguments)] |
| 20 | +fn warn_explicit( |
| 21 | + category: Option<PyTypeRef>, |
| 22 | + message: PyStrRef, |
| 23 | + _filename: PyStrRef, |
| 24 | + _lineno: usize, |
| 25 | + _module: PyObjectRef, |
| 26 | + _registry: PyObjectRef, |
| 27 | + _source_line: Option<PyObjectRef>, |
| 28 | + _source: Option<PyObjectRef>, |
| 29 | + vm: &VirtualMachine, |
| 30 | +) -> PyResult<()> { |
| 31 | + // TODO: Implement correctly |
| 32 | + let category = if let Some(category) = category { |
| 33 | + if !category.fast_issubclass(&vm.ctx.exceptions.warning) { |
| 34 | + return Err(vm.new_type_error(format!( |
| 35 | + "category must be a Warning subclass, not '{}'", |
| 36 | + category.class().name() |
| 37 | + ))); |
| 38 | + } |
| 39 | + category |
| 40 | + } else { |
| 41 | + vm.ctx.exceptions.user_warning.clone() |
| 42 | + }; |
| 43 | + let stderr = crate::stdlib::sys::PyStderr(vm); |
| 44 | + writeln!(stderr, "{}: {}", category.name(), message.as_str(),); |
| 45 | + Ok(()) |
| 46 | +} |
| 47 | + |
| 48 | +// filename, module, and registry are new refs, globals is borrowed |
| 49 | +// Returns 0 on error (no new refs), 1 on success |
| 50 | +fn setup_context( |
| 51 | + _stack_level: isize, |
| 52 | + vm: &VirtualMachine, |
| 53 | +) -> PyResult< |
| 54 | + // filename, lineno, module, registry |
| 55 | + (PyStrRef, usize, PyObjectRef, PyObjectRef), |
| 56 | +> { |
| 57 | + let __warningregistry__ = "__warningregistry__"; |
| 58 | + let __name__ = "__name__"; |
| 59 | + |
| 60 | + // Setup globals, filename and lineno. |
| 61 | + let frame = vm.current_frame(); |
| 62 | + |
| 63 | + // PyThreadState *tstate = _PyThreadState_GET(); |
| 64 | + // PyFrameObject *f = PyThreadState_GetFrame(tstate); |
| 65 | + // // Stack level comparisons to Python code is off by one as there is no |
| 66 | + // // warnings-related stack level to avoid. |
| 67 | + // if (stack_level <= 0 || is_internal_frame(f)) { |
| 68 | + // while (--stack_level > 0 && f != NULL) { |
| 69 | + // PyFrameObject *back = PyFrame_GetBack(f); |
| 70 | + // Py_DECREF(f); |
| 71 | + // f = back; |
| 72 | + // } |
| 73 | + // } |
| 74 | + // else { |
| 75 | + // while (--stack_level > 0 && f != NULL) { |
| 76 | + // f = next_external_frame(f); |
| 77 | + // } |
| 78 | + // } |
| 79 | + |
| 80 | + let (globals, filename, lineno) = if let Some(f) = frame { |
| 81 | + // TODO: |
| 82 | + let lineno = 1; |
| 83 | + // *lineno = PyFrame_GetLineNumber(f); |
| 84 | + // *filename = code->co_filename; |
| 85 | + (f.globals.clone(), f.code.source_path.clone(), lineno) |
| 86 | + } else { |
| 87 | + (vm.current_globals().clone(), vm.ctx.new_str("sys"), 1) |
| 88 | + }; |
| 89 | + |
| 90 | + let registry = if let Ok(registry) = globals.get_item(__warningregistry__, vm) { |
| 91 | + registry |
| 92 | + } else { |
| 93 | + let registry = PyDict::new_ref(&vm.ctx); |
| 94 | + globals.set_item(__warningregistry__, registry.clone().into(), vm)?; |
| 95 | + registry.into() |
| 96 | + }; |
| 97 | + |
| 98 | + // Setup module. |
| 99 | + let module = globals |
| 100 | + .get_item(__name__, vm) |
| 101 | + .unwrap_or_else(|_| vm.new_pyobj("<string>")); |
| 102 | + Ok((filename, lineno, module, registry)) |
| 103 | +} |
0 commit comments