Skip to content

Commit 0f02d45

Browse files
authored
Merge pull request #2039 from davidhewitt/no-self-dev-dep
dev: remove self dev dependency
2 parents 2705a92 + 6433d88 commit 0f02d45

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+151
-69
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ jobs:
185185
# Run tests (except on PyPy, because no embedding API).
186186
- if: ${{ !startsWith(matrix.python-version, 'pypy') }}
187187
name: Test (no features)
188-
run: cargo test --no-default-features
188+
run: cargo test --no-default-features --lib --tests
189189

190190
# --no-default-features when used with `cargo build/test -p` doesn't seem to work!
191191
- name: Test pyo3-build-config (no features)
@@ -252,6 +252,10 @@ jobs:
252252
# TODO: this is a hack to workaround compile_error! warnings about auto-initialize on PyPy
253253
# Once cargo's `resolver = "2"` is stable (~ MSRV Rust 1.52), remove this.
254254
PYO3_CI: 1
255+
# This is a hack to make CARGO_PRIMARY_PACKAGE always set even for the
256+
# msrv job. MSRV is currently 1.48, but CARGO_PRIMARY_PACKAGE only came in
257+
# 1.49.
258+
CARGO_PRIMARY_PACKAGE: 1
255259

256260
coverage:
257261
needs: [fmt]

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ rustversion = "1.0"
4646
proptest = { version = "0.10.1", default-features = false, features = ["std"] }
4747
serde_json = "1.0.61"
4848

49-
# features needed to run the PyO3 test suite
50-
pyo3 = { path = ".", default-features = false, features = ["macros", "auto-initialize"] }
51-
5249
[build-dependencies]
5350
pyo3-build-config = { path = "pyo3-build-config", version = "0.15.1", features = ["resolve-config"] }
5451

src/conversions/anyhow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ impl From<anyhow::Error> for PyErr {
117117

118118
#[cfg(test)]
119119
mod test_anyhow {
120-
use pyo3::prelude::*;
121-
use pyo3::types::IntoPyDict;
120+
use crate::prelude::*;
121+
use crate::types::IntoPyDict;
122122

123123
use anyhow::{anyhow, bail, Context, Result};
124124

src/conversions/eyre.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ impl From<eyre::Report> for PyErr {
118118

119119
#[cfg(test)]
120120
mod tests {
121-
use pyo3::prelude::*;
122-
use pyo3::types::IntoPyDict;
121+
use crate::prelude::*;
122+
use crate::types::IntoPyDict;
123123

124124
use eyre::{bail, Result, WrapErr};
125125

src/ffi/datetime.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -630,19 +630,22 @@ impl Deref for _PyDateTime_TimeZone_UTC_impl {
630630
#[cfg(test)]
631631
mod tests {
632632
use super::*;
633-
use crate::{py_run, AsPyPointer, IntoPy, Py, PyAny, Python};
633+
use crate::{types::PyDict, AsPyPointer, IntoPy, Py, PyAny, Python};
634634

635635
#[test]
636636
fn test_datetime_fromtimestamp() {
637637
Python::with_gil(|py| {
638638
let args: Py<PyAny> = (100,).into_py(py);
639639
unsafe { PyDateTime_IMPORT() };
640640
let dt: &PyAny = unsafe { py.from_owned_ptr(PyDateTime_FromTimestamp(args.as_ptr())) };
641-
py_run!(
642-
py,
643-
dt,
644-
"import datetime; assert dt == datetime.datetime.fromtimestamp(100)"
645-
);
641+
let locals = PyDict::new(py);
642+
locals.set_item("dt", dt).unwrap();
643+
py.run(
644+
"import datetime; assert dt == datetime.datetime.fromtimestamp(100)",
645+
None,
646+
Some(locals),
647+
)
648+
.unwrap();
646649
})
647650
}
648651

@@ -652,11 +655,14 @@ mod tests {
652655
let args: Py<PyAny> = (100,).into_py(py);
653656
unsafe { PyDateTime_IMPORT() };
654657
let dt: &PyAny = unsafe { py.from_owned_ptr(PyDate_FromTimestamp(args.as_ptr())) };
655-
py_run!(
656-
py,
657-
dt,
658-
"import datetime; assert dt == datetime.date.fromtimestamp(100)"
659-
);
658+
let locals = PyDict::new(py);
659+
locals.set_item("dt", dt).unwrap();
660+
py.run(
661+
"import datetime; assert dt == datetime.date.fromtimestamp(100)",
662+
None,
663+
Some(locals),
664+
)
665+
.unwrap();
660666
})
661667
}
662668

@@ -665,11 +671,14 @@ mod tests {
665671
fn test_utc_timezone() {
666672
Python::with_gil(|py| {
667673
let utc_timezone = PyDateTime_TimeZone_UTC.as_ref(py);
668-
py_run!(
669-
py,
670-
utc_timezone,
671-
"import datetime; assert utc_timezone is datetime.timezone.utc"
672-
);
674+
let locals = PyDict::new(py);
675+
locals.set_item("utc_timezone", utc_timezone).unwrap();
676+
py.run(
677+
"import datetime; assert utc_timezone is datetime.timezone.utc",
678+
None,
679+
Some(locals),
680+
)
681+
.unwrap();
673682
})
674683
}
675684
}

src/gil.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ impl GILGuard {
181181
if #[cfg(all(feature = "auto-initialize", not(PyPy)))] {
182182
prepare_freethreaded_python();
183183
} else {
184+
// This is a "hack" to make running `cargo test` for PyO3 convenient (i.e. no need
185+
// to specify `--features auto-initialize` manually. Tests within the crate itself
186+
// all depend on the auto-initialize feature for conciseness but Cargo does not
187+
// provide a mechanism to specify required features for tests.
188+
if option_env!("CARGO_PRIMARY_PACKAGE").is_some() {
189+
prepare_freethreaded_python();
190+
}
191+
184192
START.call_once_force(|_| unsafe {
185193
// Use call_once_force because if there is a panic because the interpreter is
186194
// not initialized, it's fine for the user to initialize the interpreter and

src/types/any.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -697,13 +697,6 @@ mod tests {
697697
Python, ToPyObject,
698698
};
699699

700-
macro_rules! test_module {
701-
($py:ident, $code:literal) => {
702-
PyModule::from_code($py, indoc::indoc!($code), file!(), "test_module")
703-
.expect("module creation failed")
704-
};
705-
}
706-
707700
#[test]
708701
fn test_call_for_non_existing_method() {
709702
Python::with_gil(|py| {
@@ -728,14 +721,17 @@ mod tests {
728721
#[test]
729722
fn test_call_method0() {
730723
Python::with_gil(|py| {
731-
let module = test_module!(
724+
let module = PyModule::from_code(
732725
py,
733726
r#"
734-
class SimpleClass:
735-
def foo(self):
736-
return 42
737-
"#
738-
);
727+
class SimpleClass:
728+
def foo(self):
729+
return 42
730+
"#,
731+
file!(),
732+
"test_module",
733+
)
734+
.expect("module creation failed");
739735

740736
let simple_class = module.getattr("SimpleClass").unwrap().call0().unwrap();
741737
assert_eq!(

src/types/iterator.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ mod tests {
113113
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]
114114
use crate::PyTryFrom;
115115
use crate::{Py, PyAny, Python, ToPyObject};
116-
use indoc::indoc;
117116

118117
#[test]
119118
fn vec_iter() {
@@ -177,16 +176,14 @@ mod tests {
177176

178177
#[test]
179178
fn fibonacci_generator() {
180-
let fibonacci_generator = indoc!(
181-
r#"
182-
def fibonacci(target):
183-
a = 1
184-
b = 1
185-
for _ in range(target):
186-
yield a
187-
a, b = b, a + b
188-
"#
189-
);
179+
let fibonacci_generator = r#"
180+
def fibonacci(target):
181+
a = 1
182+
b = 1
183+
for _ in range(target):
184+
yield a
185+
a, b = b, a + b
186+
"#;
190187

191188
Python::with_gil(|py| {
192189
let context = PyDict::new(py);

src/types/num.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ fn err_if_invalid_value<T: PartialEq>(
278278
#[cfg(test)]
279279
mod test_128bit_intergers {
280280
use super::*;
281+
use crate::types::PyDict;
281282

282283
#[cfg(not(target_arch = "wasm32"))]
283284
use proptest::prelude::*;
@@ -288,7 +289,9 @@ mod test_128bit_intergers {
288289
fn test_i128_roundtrip(x: i128) {
289290
Python::with_gil(|py| {
290291
let x_py = x.into_py(py);
291-
crate::py_run!(py, x_py, &format!("assert x_py == {}", x));
292+
let locals = PyDict::new(py);
293+
locals.set_item("x_py", x_py.clone_ref(py)).unwrap();
294+
py.run(&format!("assert x_py == {}", x), None, Some(locals)).unwrap();
292295
let roundtripped: i128 = x_py.extract(py).unwrap();
293296
assert_eq!(x, roundtripped);
294297
})
@@ -301,7 +304,9 @@ mod test_128bit_intergers {
301304
fn test_u128_roundtrip(x: u128) {
302305
Python::with_gil(|py| {
303306
let x_py = x.into_py(py);
304-
crate::py_run!(py, x_py, &format!("assert x_py == {}", x));
307+
let locals = PyDict::new(py);
308+
locals.set_item("x_py", x_py.clone_ref(py)).unwrap();
309+
py.run(&format!("assert x_py == {}", x), None, Some(locals)).unwrap();
305310
let roundtripped: u128 = x_py.extract(py).unwrap();
306311
assert_eq!(x, roundtripped);
307312
})

tests/test_arithmetics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
use pyo3::class::basic::CompareOp;
24
use pyo3::prelude::*;
35
use pyo3::py_run;

tests/test_arithmetics_protos.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(deprecated)] // for deprecated protocol methods
2+
#![cfg(feature = "macros")]
23

34
use pyo3::class::basic::CompareOp;
45
use pyo3::class::*;

tests/test_buffer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg(feature = "macros")]
12
#![cfg(not(Py_LIMITED_API))]
23

34
use pyo3::{

tests/test_buffer_protocol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg(feature = "macros")]
12
#![cfg(not(Py_LIMITED_API))]
23

34
use pyo3::buffer::PyBuffer;

tests/test_bytes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
use pyo3::prelude::*;
24
use pyo3::types::PyBytes;
35

tests/test_class_attributes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
use pyo3::prelude::*;
24

35
mod common;

tests/test_class_basics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
use pyo3::prelude::*;
24
use pyo3::types::PyType;
35
use pyo3::{py_run, PyClass};

tests/test_class_conversion.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
use pyo3::prelude::*;
24
use pyo3::ToPyObject;
35

tests/test_class_new.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
use pyo3::exceptions::PyValueError;
24
use pyo3::prelude::*;
35

tests/test_compile_error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
#[rustversion::stable]
24
#[test]
35
fn test_compile_errors() {

tests/test_datetime.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ fn _get_subclasses<'p>(
3333
}
3434

3535
macro_rules! assert_check_exact {
36-
($check_func:ident, $obj: expr) => {
36+
($check_func:ident, $check_func_exact:ident, $obj: expr) => {
3737
unsafe {
38-
use pyo3::{AsPyPointer, ffi::*};
38+
use pyo3::{ffi::*, AsPyPointer};
3939
assert!($check_func(($obj).as_ptr()) != 0);
40-
assert!(pyo3::paste::expr!([<$check_func Exact>])(($obj).as_ptr()) != 0);
40+
assert!($check_func_exact(($obj).as_ptr()) != 0);
4141
}
4242
};
4343
}
4444

4545
macro_rules! assert_check_only {
46-
($check_func:ident, $obj: expr) => {
46+
($check_func:ident, $check_func_exact:ident, $obj: expr) => {
4747
unsafe {
48-
use pyo3::{AsPyPointer, ffi::*};
48+
use pyo3::{ffi::*, AsPyPointer};
4949
assert!($check_func(($obj).as_ptr()) != 0);
50-
assert!(pyo3::paste::expr!([<$check_func Exact>])(($obj).as_ptr()) == 0);
50+
assert!($check_func_exact(($obj).as_ptr()) == 0);
5151
}
5252
};
5353
}
@@ -58,9 +58,9 @@ fn test_date_check() {
5858
let py = gil.python();
5959
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "date", "2018, 1, 1").unwrap();
6060

61-
assert_check_exact!(PyDate_Check, obj);
62-
assert_check_only!(PyDate_Check, sub_obj);
63-
assert_check_only!(PyDate_Check, sub_sub_obj);
61+
assert_check_exact!(PyDate_Check, PyDate_CheckExact, obj);
62+
assert_check_only!(PyDate_Check, PyDate_CheckExact, sub_obj);
63+
assert_check_only!(PyDate_Check, PyDate_CheckExact, sub_sub_obj);
6464
}
6565

6666
#[test]
@@ -69,9 +69,9 @@ fn test_time_check() {
6969
let py = gil.python();
7070
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "time", "12, 30, 15").unwrap();
7171

72-
assert_check_exact!(PyTime_Check, obj);
73-
assert_check_only!(PyTime_Check, sub_obj);
74-
assert_check_only!(PyTime_Check, sub_sub_obj);
72+
assert_check_exact!(PyTime_Check, PyTime_CheckExact, obj);
73+
assert_check_only!(PyTime_Check, PyTime_CheckExact, sub_obj);
74+
assert_check_only!(PyTime_Check, PyTime_CheckExact, sub_sub_obj);
7575
}
7676

7777
#[test]
@@ -82,10 +82,10 @@ fn test_datetime_check() {
8282
.map_err(|e| e.print(py))
8383
.unwrap();
8484

85-
assert_check_only!(PyDate_Check, obj);
86-
assert_check_exact!(PyDateTime_Check, obj);
87-
assert_check_only!(PyDateTime_Check, sub_obj);
88-
assert_check_only!(PyDateTime_Check, sub_sub_obj);
85+
assert_check_only!(PyDate_Check, PyDate_CheckExact, obj);
86+
assert_check_exact!(PyDateTime_Check, PyDateTime_CheckExact, obj);
87+
assert_check_only!(PyDateTime_Check, PyDateTime_CheckExact, sub_obj);
88+
assert_check_only!(PyDateTime_Check, PyDateTime_CheckExact, sub_sub_obj);
8989
}
9090

9191
#[test]
@@ -94,9 +94,9 @@ fn test_delta_check() {
9494
let py = gil.python();
9595
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "timedelta", "1, -3").unwrap();
9696

97-
assert_check_exact!(PyDelta_Check, obj);
98-
assert_check_only!(PyDelta_Check, sub_obj);
99-
assert_check_only!(PyDelta_Check, sub_sub_obj);
97+
assert_check_exact!(PyDelta_Check, PyDelta_CheckExact, obj);
98+
assert_check_only!(PyDelta_Check, PyDelta_CheckExact, sub_obj);
99+
assert_check_only!(PyDelta_Check, PyDelta_CheckExact, sub_sub_obj);
100100
}
101101

102102
#[test]

tests/test_default_impls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
use pyo3::prelude::*;
24

35
mod common;

tests/test_enum.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
use pyo3::prelude::*;
24
use pyo3::{py_run, wrap_pyfunction};
35

tests/test_exceptions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "macros")]
2+
13
use pyo3::prelude::*;
24
use pyo3::{exceptions, py_run, PyErr, PyResult};
35
use std::error::Error;

0 commit comments

Comments
 (0)