Skip to content

Commit

Permalink
remove coverage holder from subinterpreter and use directly
Browse files Browse the repository at this point in the history
  • Loading branch information
brownben committed Nov 27, 2024
1 parent dc78a93 commit 19d7cc2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 44 deletions.
21 changes: 8 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,19 @@ fn main() -> ExitCode {
.tests
.par_iter()
.map(|test| {
let mut subinterpreter = python::SubInterpreter::new(&interpreter);

if settings.coverage.enabled {
subinterpreter.enable_coverage();
}

let outcome = subinterpreter.with_gil(|python| {
python::SubInterpreter::new(&interpreter).with_gil(|python| {
python.add_parent_module_to_path(test.file());
if !settings.no_output_capture {
python.capture_output();
}
let tracer = (settings.coverage.enabled).then(|| coverage::enable_collection(python));

python.add_parent_module_to_path(test.file());

run::test(python, test)
});
let coverage = subinterpreter.get_coverage();
let test_outcome = run::test(python, test);
let coverage =
tracer.map(|tracer_object| coverage::get_executed_lines(python, &tracer_object));

(outcome, coverage)
(test_outcome, coverage)
})
})
.inspect(|(outcome, _coverage)| {
reporter.result(outcome);
Expand Down
33 changes: 2 additions & 31 deletions src/python/interpreters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use pyo3_ffi::{self as ffi};
use std::{env, mem, ptr};
use widestring::WideCString;

use super::{ActiveInterpreter, PyObject};
use crate::coverage;
use super::ActiveInterpreter;

/// Interface implemented by both [`MainInterpreter`] and [`SubInterpreter`]
///
Expand Down Expand Up @@ -82,7 +81,6 @@ unsafe impl Sync for MainInterpreter {}
/// Represents a Python Subinterpreter (An interpreter for a specific thread)
pub struct SubInterpreter {
interpreter_state: *mut ffi::PyThreadState,
coverage_trace_object: Option<PyObject>,
}
impl SubInterpreter {
/// The default configuration to create a Subinterpreter with it's own global interpreter lock
Expand All @@ -109,34 +107,7 @@ impl SubInterpreter {
// the main GIL is released during creation
};

Self {
interpreter_state,
coverage_trace_object: None,
}
}

pub fn enable_coverage(&mut self) {
unsafe { ffi::PyEval_RestoreThread(self.interpreter_state) };

// We have just got the GIL
let interpreter = unsafe { &ActiveInterpreter::new() };
self.coverage_trace_object = Some(coverage::enable_collection(interpreter));

self.interpreter_state = unsafe { ffi::PyEval_SaveThread() };
}

pub fn get_coverage(&mut self) -> Option<coverage::Lines> {
unsafe { ffi::PyEval_RestoreThread(self.interpreter_state) };

// We have just got the GIL
let interpreter = unsafe { &ActiveInterpreter::new() };
let lines = mem::take(&mut self.coverage_trace_object)
.as_ref()
.map(|trace_object| coverage::get_executed_lines(interpreter, trace_object));

self.interpreter_state = unsafe { ffi::PyEval_SaveThread() };

lines
Self { interpreter_state }
}
}
impl Interpreter for SubInterpreter {
Expand Down

0 comments on commit 19d7cc2

Please sign in to comment.