Skip to content

Commit

Permalink
Fix user-defined warning testing in ABI3
Browse files Browse the repository at this point in the history
  • Loading branch information
FlickerSoul committed Jul 25, 2024
1 parent 33cc253 commit 49d530c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/tests/hygiene/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,11 @@ struct WarningDummy {
value: i32,
}

#[cfg(not(Py_LIMITED_API))]
#[crate::pyclass(crate = "crate", extends=crate::exceptions::PyWarning)]
pub struct UserDefinedWarning {}

#[cfg(not(Py_LIMITED_API))]
#[crate::pymethods(crate = "crate")]
impl UserDefinedWarning {
#[new]
Expand All @@ -454,6 +456,7 @@ impl WarningDummy {
#[pyo3(warn(message = "this method raises warning", category = crate::exceptions::PyFutureWarning))]
fn method_with_warning_and_custom_category(_slf: crate::PyRef<'_, Self>) {}

#[cfg(not(Py_LIMITED_API))]
#[pyo3(warn(message = "this method raises user-defined warning", category = UserDefinedWarning))]
fn method_with_warning_and_user_defined_category(&self) {}

Expand Down Expand Up @@ -523,6 +526,7 @@ impl WarningDummy2 {
#[pyo3(warn(message = "this class-method raises future warning", category = crate::exceptions::PyFutureWarning))]
fn multiple_warnings_fn(&self) {}

#[cfg(not(Py_LIMITED_API))]
#[pyo3(warn(message = "this class-method raises future warning", category = crate::exceptions::PyFutureWarning))]
#[pyo3(warn(message = "this class-method raises user-defined warning", category = UserDefinedWarning))]
fn multiple_warnings_fn_with_custom_category(&self) {}
Expand Down
10 changes: 9 additions & 1 deletion tests/test_methods.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![cfg(feature = "macros")]

use pyo3::exceptions::{PyDeprecationWarning, PyFutureWarning, PyUserWarning, PyWarning};
#[cfg(not(Py_LIMITED_API))]
use pyo3::exceptions::PyWarning;
use pyo3::exceptions::{PyDeprecationWarning, PyFutureWarning, PyUserWarning};
use pyo3::prelude::*;
use pyo3::py_run;
use pyo3::types::PySequence;
Expand Down Expand Up @@ -1164,9 +1166,11 @@ fn test_issue_2988() {
}
}

#[cfg(not(Py_LIMITED_API))]
#[pyclass(extends=PyWarning)]
pub struct UserDefinedWarning {}

#[cfg(not(Py_LIMITED_API))]
#[pymethods]
impl UserDefinedWarning {
#[new]
Expand Down Expand Up @@ -1200,6 +1204,7 @@ fn test_pymethods_warn() {
#[pyo3(warn(message = "this method raises warning", category = PyFutureWarning))]
fn method_with_warning_and_custom_category(_slf: PyRef<'_, Self>) {}

#[cfg(not(Py_LIMITED_API))]
#[pyo3(warn(message = "this method raises user-defined warning", category = UserDefinedWarning))]
fn method_with_warning_and_user_defined_category(&self) {}

Expand Down Expand Up @@ -1262,6 +1267,7 @@ fn test_pymethods_warn() {
);

// FnType::Fn, user-defined warning
#[cfg(not(Py_LIMITED_API))]
py_expect_warning!(
py,
obj,
Expand Down Expand Up @@ -1423,6 +1429,7 @@ fn test_py_methods_multiple_warn() {
#[pyo3(deprecated = "this method is deprecated")]
fn multiple_warn_deprecated_method(&self) {}

#[cfg(not(Py_LIMITED_API))]
#[pyo3(warn(message = "this method raises FutureWarning", category = pyo3::exceptions::PyFutureWarning))]
#[pyo3(warn(message = "this method raises UserDefinedWarning", category = UserDefinedWarning))]
fn multiple_warn_custom_category_method(&self) {}
Expand Down Expand Up @@ -1452,6 +1459,7 @@ fn test_py_methods_multiple_warn() {
]
);

#[cfg(not(Py_LIMITED_API))]
py_expect_warning!(
py,
obj,
Expand Down
10 changes: 9 additions & 1 deletion tests/test_pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::collections::HashMap;

#[cfg(not(Py_LIMITED_API))]
use pyo3::buffer::PyBuffer;
use pyo3::exceptions::{PyDeprecationWarning, PyFutureWarning, PyUserWarning, PyWarning};
#[cfg(not(Py_LIMITED_API))]
use pyo3::exceptions::PyWarning;
use pyo3::exceptions::{PyDeprecationWarning, PyFutureWarning, PyUserWarning};
use pyo3::ffi::c_str;
use pyo3::prelude::*;
#[cfg(not(Py_LIMITED_API))]
Expand Down Expand Up @@ -606,9 +608,11 @@ fn test_pyfunction_raw_ident() {
})
}

#[cfg(not(Py_LIMITED_API))]
#[pyclass(extends=PyWarning)]
pub struct UserDefinedWarning {}

#[cfg(not(Py_LIMITED_API))]
#[pymethods]
impl UserDefinedWarning {
#[new]
Expand Down Expand Up @@ -656,10 +660,12 @@ fn test_pyfunction_warn() {
)]
);

#[cfg(not(Py_LIMITED_API))]
#[pyfunction]
#[pyo3(warn(message = "this function raises user-defined warning", category = UserDefinedWarning))]
fn function_with_warning_and_user_defined_category() {}

#[cfg(not(Py_LIMITED_API))]
py_expect_warning_for_fn!(
function_with_warning_and_user_defined_category,
f,
Expand Down Expand Up @@ -699,11 +705,13 @@ fn test_pyfunction_multiple_warnings() {
]
);

#[cfg(not(Py_LIMITED_API))]
#[pyfunction]
#[pyo3(warn(message = "this function raises FutureWarning", category = PyFutureWarning))]
#[pyo3(warn(message = "this function raises user-defined warning", category = UserDefinedWarning))]
fn function_with_multiple_custom_warnings() {}

#[cfg(not(Py_LIMITED_API))]
py_expect_warning_for_fn!(
function_with_multiple_custom_warnings,
f,
Expand Down

0 comments on commit 49d530c

Please sign in to comment.