Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/6261.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
During rust panic, all error output is printed to `sys.stderr`. Previously, some was printed to rust's stderr.
11 changes: 10 additions & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,16 @@ impl Drop for PyUntypedBuffer {
if Python::try_attach(|_| unsafe { self.0.release() }).is_none()
&& crate::internal::state::is_in_gc_traversal()
{
eprintln!("Warning: PyBuffer dropped while in GC traversal, this is a bug and will leak memory.");
// NOTE: write using libc because we can't call python APIs during gc traversal
let message = "Warning: PyBuffer dropped while in GC traversal, this is a bug and will leak memory.";
unsafe {
libc::write(
2,
message.as_ptr().cast(),
#[allow(clippy::useless_conversion, reason = "platform specific")]
message.len().try_into().unwrap(),
);
}
}
// If `try_attach` failed and `is_in_gc_traversal()` is false, then probably the interpreter has
// already finalized and we can just assume that the underlying memory has already been freed.
Expand Down
4 changes: 0 additions & 4 deletions src/conversions/jiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,10 +781,6 @@ mod tests {
|name: &'static str, year, month, day, hour, minute, second, ms, py_ms| {
let offset = Offset::from_seconds(3600).unwrap();
let datetime = DateTime::new(year, month, day, hour, minute, second, ms * 1000)
.map_err(|e| {
Comment thread
Person-93 marked this conversation as resolved.
eprintln!("{name}: {e}");
e
})
.unwrap()
.to_zoned(offset.to_time_zone())
.unwrap();
Expand Down
8 changes: 6 additions & 2 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::inspect::PyStaticExpr;
use crate::instance::Bound;
#[cfg(Py_3_11)]
use crate::intern;
use crate::internal::stdio::write_py_stderr;
use crate::panic::PanicException;
use crate::platform::prelude::*;
use crate::py_result_ext::PyResultExt;
Expand Down Expand Up @@ -302,8 +303,11 @@ impl PyErr {
.map(|py_str| py_str.to_string_lossy().into())
.unwrap_or_else(|_| String::from("Unwrapped panic from Python code"));

eprintln!("--- PyO3 is resuming a panic after fetching a PanicException from Python. ---");
eprintln!("Python stack trace below:");
write_py_stderr(
py,
c"--- PyO3 is resuming a panic after fetching a PanicException from Python. ---",
);
write_py_stderr(py, c"Python stack trace below:");

PyErrState::normalized(state).restore(py);

Expand Down
1 change: 1 addition & 0 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub(crate) mod macros;
pub(crate) mod get_slot;
pub(crate) mod pyclass_init;
pub(crate) mod state;
pub(crate) mod stdio;
9 changes: 9 additions & 0 deletions src/internal/stdio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use core::ffi::CStr;

use crate::ffi::PySys_WriteStderr;
use crate::prelude::Python;

pub(crate) fn write_py_stderr(_py: Python<'_>, message: &CStr) {
// SAFETY: message is a valid c string
unsafe { PySys_WriteStderr(message.as_ptr()) };
}
3 changes: 0 additions & 3 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ pub(crate) mod prelude {
string::{String, ToString},
vec::Vec,
};

// TODO find a `no_std` replacement for eprintln
pub use std::eprintln;
}

#[cfg(feature = "hashbrown")]
Expand Down