Skip to content

Commit 7dbbf71

Browse files
authored
Fix the main branch (#2046)
1 parent 0f02d45 commit 7dbbf71

File tree

3 files changed

+57
-46
lines changed

3 files changed

+57
-46
lines changed

benches/bench_call.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@ use pyo3::prelude::*;
44

55
macro_rules! test_module {
66
($py:ident, $code:literal) => {
7-
PyModule::from_code($py, indoc::indoc!($code), file!(), "test_module")
8-
.expect("module creation failed")
7+
PyModule::from_code($py, $code, file!(), "test_module").expect("module creation failed")
98
};
109
}
1110

1211
fn bench_call_0(b: &mut Bencher) {
1312
Python::with_gil(|py| {
14-
let module = test_module!(
15-
py,
16-
r#"
17-
def foo(): pass
18-
"#
19-
);
13+
let module = test_module!(py, "def foo(): pass");
2014

2115
let foo_module = module.getattr("foo").unwrap();
2216

@@ -32,10 +26,11 @@ fn bench_call_method_0(b: &mut Bencher) {
3226
Python::with_gil(|py| {
3327
let module = test_module!(
3428
py,
35-
r#"
36-
class Foo:
37-
def foo(self): pass
38-
"#
29+
"
30+
class Foo:
31+
def foo(self):
32+
pass
33+
"
3934
);
4035

4136
let foo_module = module.getattr("Foo").unwrap().call0().unwrap();

benches/bench_pyclass.rs

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,64 @@
1-
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
1+
#[cfg(feature = "macros")]
2+
use criterion::{criterion_group, criterion_main, Criterion};
23

3-
use pyo3::{class::PyObjectProtocol, prelude::*, type_object::LazyStaticType};
4+
#[cfg(feature = "macros")]
5+
mod m {
6+
use pyo3::{class::PyObjectProtocol, prelude::*, type_object::LazyStaticType};
47

5-
/// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle.
6-
#[pyclass]
7-
struct MyClass {
8-
#[pyo3(get, set)]
9-
elements: Vec<i32>,
10-
}
11-
12-
#[pymethods]
13-
impl MyClass {
14-
#[new]
15-
fn new(elements: Vec<i32>) -> Self {
16-
Self { elements }
8+
/// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle.
9+
#[pyclass]
10+
struct MyClass {
11+
#[pyo3(get, set)]
12+
elements: Vec<i32>,
1713
}
1814

19-
fn __call__(&mut self, new_element: i32) -> usize {
20-
self.elements.push(new_element);
21-
self.elements.len()
15+
#[pymethods]
16+
impl MyClass {
17+
#[new]
18+
fn new(elements: Vec<i32>) -> Self {
19+
Self { elements }
20+
}
21+
22+
fn __call__(&mut self, new_element: i32) -> usize {
23+
self.elements.push(new_element);
24+
self.elements.len()
25+
}
2226
}
23-
}
2427

25-
#[pyproto]
26-
impl PyObjectProtocol for MyClass {
27-
/// A basic __str__ implementation.
28-
fn __str__(&self) -> &'static str {
29-
"MyClass"
28+
#[pyproto]
29+
impl PyObjectProtocol for MyClass {
30+
/// A basic __str__ implementation.
31+
fn __str__(&self) -> &'static str {
32+
"MyClass"
33+
}
3034
}
31-
}
3235

33-
fn first_time_init(b: &mut Bencher) {
34-
let gil = Python::acquire_gil();
35-
let py = gil.python();
36-
b.iter(|| {
37-
// This is using an undocumented internal PyO3 API to measure pyclass performance; please
38-
// don't use this in your own code!
39-
let ty = LazyStaticType::new();
40-
ty.get_or_init::<MyClass>(py);
41-
});
36+
pub fn first_time_init(b: &mut criterion::Bencher) {
37+
let gil = Python::acquire_gil();
38+
let py = gil.python();
39+
b.iter(|| {
40+
// This is using an undocumented internal PyO3 API to measure pyclass performance; please
41+
// don't use this in your own code!
42+
let ty = LazyStaticType::new();
43+
ty.get_or_init::<MyClass>(py);
44+
});
45+
}
4246
}
4347

48+
#[cfg(feature = "macros")]
4449
fn criterion_benchmark(c: &mut Criterion) {
45-
c.bench_function("first_time_init", first_time_init);
50+
c.bench_function("first_time_init", m::first_time_init);
4651
}
4752

53+
#[cfg(feature = "macros")]
4854
criterion_group!(benches, criterion_benchmark);
55+
56+
#[cfg(feature = "macros")]
4957
criterion_main!(benches);
58+
59+
#[cfg(not(feature = "macros"))]
60+
fn main() {
61+
unimplemented!(
62+
"benchmarking `bench_pyclass` is only available with the `macros` feature enabled"
63+
);
64+
}

src/gil.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ impl GILGuard {
186186
// all depend on the auto-initialize feature for conciseness but Cargo does not
187187
// provide a mechanism to specify required features for tests.
188188
if option_env!("CARGO_PRIMARY_PACKAGE").is_some() {
189+
#[cfg(not(PyPy))]
189190
prepare_freethreaded_python();
190191
}
191192

0 commit comments

Comments
 (0)