Skip to content

Commit

Permalink
Add first pass at documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyFoote committed Apr 3, 2024
1 parent eec748d commit 0ff4744
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/instrumentation.rs
@@ -1,14 +1,26 @@
//! APIs wrapping the Python interpreter's instrumentation features.
use crate::ffi;
use crate::pyclass::boolean_struct::False;
use crate::types::PyFrame;
use crate::{Bound, PyAny, PyClass, PyObject, PyRefMut, PyResult, Python};
use std::ffi::c_int;

/// Represents a monitoring event used by the profiling API
pub enum ProfileEvent<'py> {
/// A python function or method was called or a generator was entered.
Call,
/// A python function or method returned or a generator yielded. The
/// contained data is the value returned to the caller or `None` if
/// caused by an exception.
Return(Option<Bound<'py, PyAny>>),
/// A C function is about to be called. The contained data is the
/// function object being called.
CCall(Bound<'py, PyAny>),
/// A C function has raised an exception. The contained data is the
/// function object being called.
CException(Bound<'py, PyAny>),
/// A C function has returned. The contained data is the function
/// object being called.
CReturn(Bound<'py, PyAny>),
}

Expand All @@ -25,14 +37,17 @@ impl<'py> ProfileEvent<'py> {
}
}

/// Trait for Rust structs that can be used with Python's profiling API.
pub trait Profiler: PyClass<Frozen = False> {
/// Callback for implementing custom profiling logic.
fn profile<'py>(
&mut self,
frame: Bound<'py, PyFrame>,
event: ProfileEvent<'py>,
) -> PyResult<()>;
}

/// Register a custom Profiler with the Python interpreter.
pub fn register_profiler<P: Profiler>(profiler: Bound<'_, P>) {
unsafe { ffi::PyEval_SetProfile(Some(profile_callback::<P>), profiler.into_ptr()) };
}
Expand Down

0 comments on commit 0ff4744

Please sign in to comment.