Skip to content

Commit

Permalink
dev: remove self dev dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 8, 2021
1 parent 2705a92 commit 6433d88
Show file tree
Hide file tree
Showing 44 changed files with 151 additions and 69 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ jobs:
# Run tests (except on PyPy, because no embedding API).
- if: ${{ !startsWith(matrix.python-version, 'pypy') }}
name: Test (no features)
run: cargo test --no-default-features
run: cargo test --no-default-features --lib --tests

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

coverage:
needs: [fmt]
Expand Down
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ rustversion = "1.0"
proptest = { version = "0.10.1", default-features = false, features = ["std"] }
serde_json = "1.0.61"

# features needed to run the PyO3 test suite
pyo3 = { path = ".", default-features = false, features = ["macros", "auto-initialize"] }

[build-dependencies]
pyo3-build-config = { path = "pyo3-build-config", version = "0.15.1", features = ["resolve-config"] }

Expand Down
4 changes: 2 additions & 2 deletions src/conversions/anyhow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ impl From<anyhow::Error> for PyErr {

#[cfg(test)]
mod test_anyhow {
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
use crate::prelude::*;
use crate::types::IntoPyDict;

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

Expand Down
4 changes: 2 additions & 2 deletions src/conversions/eyre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ impl From<eyre::Report> for PyErr {

#[cfg(test)]
mod tests {
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
use crate::prelude::*;
use crate::types::IntoPyDict;

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

Expand Down
41 changes: 25 additions & 16 deletions src/ffi/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,19 +630,22 @@ impl Deref for _PyDateTime_TimeZone_UTC_impl {
#[cfg(test)]
mod tests {
use super::*;
use crate::{py_run, AsPyPointer, IntoPy, Py, PyAny, Python};
use crate::{types::PyDict, AsPyPointer, IntoPy, Py, PyAny, Python};

#[test]
fn test_datetime_fromtimestamp() {
Python::with_gil(|py| {
let args: Py<PyAny> = (100,).into_py(py);
unsafe { PyDateTime_IMPORT() };
let dt: &PyAny = unsafe { py.from_owned_ptr(PyDateTime_FromTimestamp(args.as_ptr())) };
py_run!(
py,
dt,
"import datetime; assert dt == datetime.datetime.fromtimestamp(100)"
);
let locals = PyDict::new(py);
locals.set_item("dt", dt).unwrap();
py.run(
"import datetime; assert dt == datetime.datetime.fromtimestamp(100)",
None,
Some(locals),
)
.unwrap();
})
}

Expand All @@ -652,11 +655,14 @@ mod tests {
let args: Py<PyAny> = (100,).into_py(py);
unsafe { PyDateTime_IMPORT() };
let dt: &PyAny = unsafe { py.from_owned_ptr(PyDate_FromTimestamp(args.as_ptr())) };
py_run!(
py,
dt,
"import datetime; assert dt == datetime.date.fromtimestamp(100)"
);
let locals = PyDict::new(py);
locals.set_item("dt", dt).unwrap();
py.run(
"import datetime; assert dt == datetime.date.fromtimestamp(100)",
None,
Some(locals),
)
.unwrap();
})
}

Expand All @@ -665,11 +671,14 @@ mod tests {
fn test_utc_timezone() {
Python::with_gil(|py| {
let utc_timezone = PyDateTime_TimeZone_UTC.as_ref(py);
py_run!(
py,
utc_timezone,
"import datetime; assert utc_timezone is datetime.timezone.utc"
);
let locals = PyDict::new(py);
locals.set_item("utc_timezone", utc_timezone).unwrap();
py.run(
"import datetime; assert utc_timezone is datetime.timezone.utc",
None,
Some(locals),
)
.unwrap();
})
}
}
8 changes: 8 additions & 0 deletions src/gil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ impl GILGuard {
if #[cfg(all(feature = "auto-initialize", not(PyPy)))] {
prepare_freethreaded_python();
} else {
// This is a "hack" to make running `cargo test` for PyO3 convenient (i.e. no need
// to specify `--features auto-initialize` manually. Tests within the crate itself
// all depend on the auto-initialize feature for conciseness but Cargo does not
// provide a mechanism to specify required features for tests.
if option_env!("CARGO_PRIMARY_PACKAGE").is_some() {
prepare_freethreaded_python();
}

START.call_once_force(|_| unsafe {
// Use call_once_force because if there is a panic because the interpreter is
// not initialized, it's fine for the user to initialize the interpreter and
Expand Down
22 changes: 9 additions & 13 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,13 +697,6 @@ mod tests {
Python, ToPyObject,
};

macro_rules! test_module {
($py:ident, $code:literal) => {
PyModule::from_code($py, indoc::indoc!($code), file!(), "test_module")
.expect("module creation failed")
};
}

#[test]
fn test_call_for_non_existing_method() {
Python::with_gil(|py| {
Expand All @@ -728,14 +721,17 @@ mod tests {
#[test]
fn test_call_method0() {
Python::with_gil(|py| {
let module = test_module!(
let module = PyModule::from_code(
py,
r#"
class SimpleClass:
def foo(self):
return 42
"#
);
class SimpleClass:
def foo(self):
return 42
"#,
file!(),
"test_module",
)
.expect("module creation failed");

let simple_class = module.getattr("SimpleClass").unwrap().call0().unwrap();
assert_eq!(
Expand Down
19 changes: 8 additions & 11 deletions src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ mod tests {
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]
use crate::PyTryFrom;
use crate::{Py, PyAny, Python, ToPyObject};
use indoc::indoc;

#[test]
fn vec_iter() {
Expand Down Expand Up @@ -177,16 +176,14 @@ mod tests {

#[test]
fn fibonacci_generator() {
let fibonacci_generator = indoc!(
r#"
def fibonacci(target):
a = 1
b = 1
for _ in range(target):
yield a
a, b = b, a + b
"#
);
let fibonacci_generator = r#"
def fibonacci(target):
a = 1
b = 1
for _ in range(target):
yield a
a, b = b, a + b
"#;

Python::with_gil(|py| {
let context = PyDict::new(py);
Expand Down
9 changes: 7 additions & 2 deletions src/types/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ fn err_if_invalid_value<T: PartialEq>(
#[cfg(test)]
mod test_128bit_intergers {
use super::*;
use crate::types::PyDict;

#[cfg(not(target_arch = "wasm32"))]
use proptest::prelude::*;
Expand All @@ -288,7 +289,9 @@ mod test_128bit_intergers {
fn test_i128_roundtrip(x: i128) {
Python::with_gil(|py| {
let x_py = x.into_py(py);
crate::py_run!(py, x_py, &format!("assert x_py == {}", x));
let locals = PyDict::new(py);
locals.set_item("x_py", x_py.clone_ref(py)).unwrap();
py.run(&format!("assert x_py == {}", x), None, Some(locals)).unwrap();
let roundtripped: i128 = x_py.extract(py).unwrap();
assert_eq!(x, roundtripped);
})
Expand All @@ -301,7 +304,9 @@ mod test_128bit_intergers {
fn test_u128_roundtrip(x: u128) {
Python::with_gil(|py| {
let x_py = x.into_py(py);
crate::py_run!(py, x_py, &format!("assert x_py == {}", x));
let locals = PyDict::new(py);
locals.set_item("x_py", x_py.clone_ref(py)).unwrap();
py.run(&format!("assert x_py == {}", x), None, Some(locals)).unwrap();
let roundtripped: u128 = x_py.extract(py).unwrap();
assert_eq!(x, roundtripped);
})
Expand Down
2 changes: 2 additions & 0 deletions tests/test_arithmetics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

use pyo3::class::basic::CompareOp;
use pyo3::prelude::*;
use pyo3::py_run;
Expand Down
1 change: 1 addition & 0 deletions tests/test_arithmetics_protos.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(deprecated)] // for deprecated protocol methods
#![cfg(feature = "macros")]

use pyo3::class::basic::CompareOp;
use pyo3::class::*;
Expand Down
1 change: 1 addition & 0 deletions tests/test_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "macros")]
#![cfg(not(Py_LIMITED_API))]

use pyo3::{
Expand Down
1 change: 1 addition & 0 deletions tests/test_buffer_protocol.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "macros")]
#![cfg(not(Py_LIMITED_API))]

use pyo3::buffer::PyBuffer;
Expand Down
2 changes: 2 additions & 0 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

use pyo3::prelude::*;
use pyo3::types::PyBytes;

Expand Down
2 changes: 2 additions & 0 deletions tests/test_class_attributes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

use pyo3::prelude::*;

mod common;
Expand Down
2 changes: 2 additions & 0 deletions tests/test_class_basics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

use pyo3::prelude::*;
use pyo3::types::PyType;
use pyo3::{py_run, PyClass};
Expand Down
2 changes: 2 additions & 0 deletions tests/test_class_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

use pyo3::prelude::*;
use pyo3::ToPyObject;

Expand Down
2 changes: 2 additions & 0 deletions tests/test_class_new.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;

Expand Down
2 changes: 2 additions & 0 deletions tests/test_compile_error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

#[rustversion::stable]
#[test]
fn test_compile_errors() {
Expand Down
38 changes: 19 additions & 19 deletions tests/test_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ fn _get_subclasses<'p>(
}

macro_rules! assert_check_exact {
($check_func:ident, $obj: expr) => {
($check_func:ident, $check_func_exact:ident, $obj: expr) => {
unsafe {
use pyo3::{AsPyPointer, ffi::*};
use pyo3::{ffi::*, AsPyPointer};
assert!($check_func(($obj).as_ptr()) != 0);
assert!(pyo3::paste::expr!([<$check_func Exact>])(($obj).as_ptr()) != 0);
assert!($check_func_exact(($obj).as_ptr()) != 0);
}
};
}

macro_rules! assert_check_only {
($check_func:ident, $obj: expr) => {
($check_func:ident, $check_func_exact:ident, $obj: expr) => {
unsafe {
use pyo3::{AsPyPointer, ffi::*};
use pyo3::{ffi::*, AsPyPointer};
assert!($check_func(($obj).as_ptr()) != 0);
assert!(pyo3::paste::expr!([<$check_func Exact>])(($obj).as_ptr()) == 0);
assert!($check_func_exact(($obj).as_ptr()) == 0);
}
};
}
Expand All @@ -58,9 +58,9 @@ fn test_date_check() {
let py = gil.python();
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "date", "2018, 1, 1").unwrap();

assert_check_exact!(PyDate_Check, obj);
assert_check_only!(PyDate_Check, sub_obj);
assert_check_only!(PyDate_Check, sub_sub_obj);
assert_check_exact!(PyDate_Check, PyDate_CheckExact, obj);
assert_check_only!(PyDate_Check, PyDate_CheckExact, sub_obj);
assert_check_only!(PyDate_Check, PyDate_CheckExact, sub_sub_obj);
}

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

assert_check_exact!(PyTime_Check, obj);
assert_check_only!(PyTime_Check, sub_obj);
assert_check_only!(PyTime_Check, sub_sub_obj);
assert_check_exact!(PyTime_Check, PyTime_CheckExact, obj);
assert_check_only!(PyTime_Check, PyTime_CheckExact, sub_obj);
assert_check_only!(PyTime_Check, PyTime_CheckExact, sub_sub_obj);
}

#[test]
Expand All @@ -82,10 +82,10 @@ fn test_datetime_check() {
.map_err(|e| e.print(py))
.unwrap();

assert_check_only!(PyDate_Check, obj);
assert_check_exact!(PyDateTime_Check, obj);
assert_check_only!(PyDateTime_Check, sub_obj);
assert_check_only!(PyDateTime_Check, sub_sub_obj);
assert_check_only!(PyDate_Check, PyDate_CheckExact, obj);
assert_check_exact!(PyDateTime_Check, PyDateTime_CheckExact, obj);
assert_check_only!(PyDateTime_Check, PyDateTime_CheckExact, sub_obj);
assert_check_only!(PyDateTime_Check, PyDateTime_CheckExact, sub_sub_obj);
}

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

assert_check_exact!(PyDelta_Check, obj);
assert_check_only!(PyDelta_Check, sub_obj);
assert_check_only!(PyDelta_Check, sub_sub_obj);
assert_check_exact!(PyDelta_Check, PyDelta_CheckExact, obj);
assert_check_only!(PyDelta_Check, PyDelta_CheckExact, sub_obj);
assert_check_only!(PyDelta_Check, PyDelta_CheckExact, sub_sub_obj);
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions tests/test_default_impls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

use pyo3::prelude::*;

mod common;
Expand Down
2 changes: 2 additions & 0 deletions tests/test_enum.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

use pyo3::prelude::*;
use pyo3::{py_run, wrap_pyfunction};

Expand Down
2 changes: 2 additions & 0 deletions tests/test_exceptions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "macros")]

use pyo3::prelude::*;
use pyo3::{exceptions, py_run, PyErr, PyResult};
use std::error::Error;
Expand Down
Loading

0 comments on commit 6433d88

Please sign in to comment.