|  | 
| 13 | 13 | 
 | 
| 14 | 14 | #include <utility> | 
| 15 | 15 | 
 | 
| 16 |  | -namespace warning_helpers { | 
| 17 |  | -void warn_function(py::module &m, const char *name, py::handle category, const char *message) { | 
| 18 |  | -    m.def(name, [category, message]() { py::warnings::warn(message, category); }); | 
| 19 |  | -} | 
| 20 |  | -} // namespace warning_helpers | 
| 21 |  | - | 
| 22 |  | -class CustomWarning {}; | 
| 23 |  | - | 
| 24 | 16 | TEST_SUBMODULE(warnings_, m) { | 
| 25 | 17 | 
 | 
| 26 | 18 |     // Test warning mechanism base | 
| 27 |  | -    m.def("raise_and_return", []() { | 
| 28 |  | -        std::string message = "Warning was raised!"; | 
|  | 19 | +    m.def("warn_and_return_value", []() { | 
|  | 20 | +        std::string message = "This is simple warning"; | 
| 29 | 21 |         py::warnings::warn(message.c_str(), PyExc_Warning); | 
| 30 | 22 |         return 21; | 
| 31 | 23 |     }); | 
| 32 | 24 | 
 | 
| 33 |  | -    m.def("raise_default", []() { py::warnings::warn("RuntimeWarning is raised!"); }); | 
|  | 25 | +    m.def("warn_with_default_category", []() { py::warnings::warn("This is RuntimeWarning"); }); | 
| 34 | 26 | 
 | 
| 35 |  | -    m.def("raise_from_cpython", | 
| 36 |  | -          []() { py::warnings::warn("UnicodeWarning is raised!", PyExc_UnicodeWarning); }); | 
|  | 27 | +    m.def("warn_with_different_category", | 
|  | 28 | +          []() { py::warnings::warn("This is FutureWarning", PyExc_FutureWarning); }); | 
| 37 | 29 | 
 | 
| 38 |  | -    m.def("raise_and_fail", | 
| 39 |  | -          []() { py::warnings::warn("RuntimeError should be raised!", PyExc_Exception); }); | 
|  | 30 | +    m.def("warn_with_invalid_category", | 
|  | 31 | +          []() { py::warnings::warn("Invalid category", PyExc_Exception); }); | 
| 40 | 32 | 
 | 
| 41 | 33 |     // Test custom warnings | 
| 42 | 34 |     PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object> ex_storage; | 
| 43 | 35 |     ex_storage.call_once_and_store_result([&]() { | 
| 44 | 36 |         return py::warnings::new_warning_type(m, "CustomWarning", PyExc_DeprecationWarning); | 
| 45 | 37 |     }); | 
| 46 | 38 | 
 | 
| 47 |  | -    m.def("raise_custom", []() { | 
| 48 |  | -        py::warnings::warn("CustomWarning was raised!", ex_storage.get_stored()); | 
|  | 39 | +    m.def("warn_with_custom_type", []() { | 
|  | 40 | +        py::warnings::warn("This is CustomWarning", ex_storage.get_stored()); | 
| 49 | 41 |         return 37; | 
| 50 | 42 |     }); | 
| 51 | 43 | 
 | 
| 52 | 44 |     m.def("register_duplicate_warning", | 
| 53 | 45 |           [m]() { py::warnings::new_warning_type(m, "CustomWarning", PyExc_RuntimeWarning); }); | 
| 54 |  | - | 
| 55 |  | -    // Bind warning categories | 
| 56 |  | -    warning_helpers::warn_function(m, "raise_base_warning", PyExc_Warning, "This is Warning!"); | 
| 57 |  | -    warning_helpers::warn_function( | 
| 58 |  | -        m, "raise_bytes_warning", PyExc_BytesWarning, "This is BytesWarning!"); | 
| 59 |  | -    warning_helpers::warn_function( | 
| 60 |  | -        m, "raise_deprecation_warning", PyExc_DeprecationWarning, "This is DeprecationWarning!"); | 
| 61 |  | -    warning_helpers::warn_function( | 
| 62 |  | -        m, "raise_future_warning", PyExc_FutureWarning, "This is FutureWarning!"); | 
| 63 |  | -    warning_helpers::warn_function( | 
| 64 |  | -        m, "raise_import_warning", PyExc_ImportWarning, "This is ImportWarning!"); | 
| 65 |  | -    warning_helpers::warn_function(m, | 
| 66 |  | -                                   "raise_pending_deprecation_warning", | 
| 67 |  | -                                   PyExc_PendingDeprecationWarning, | 
| 68 |  | -                                   "This is PendingDeprecationWarning!"); | 
| 69 |  | -    warning_helpers::warn_function( | 
| 70 |  | -        m, "raise_resource_warning", PyExc_ResourceWarning, "This is ResourceWarning!"); | 
| 71 |  | -    warning_helpers::warn_function( | 
| 72 |  | -        m, "raise_runtime_warning", PyExc_RuntimeWarning, "This is RuntimeWarning!"); | 
| 73 |  | -    warning_helpers::warn_function( | 
| 74 |  | -        m, "raise_syntax_warning", PyExc_SyntaxWarning, "This is SyntaxWarning!"); | 
| 75 |  | -    warning_helpers::warn_function( | 
| 76 |  | -        m, "raise_unicode_warning", PyExc_UnicodeWarning, "This is UnicodeWarning!"); | 
| 77 |  | -    warning_helpers::warn_function( | 
| 78 |  | -        m, "raise_user_warning", PyExc_UserWarning, "This is UserWarning!"); | 
| 79 | 46 | } | 
0 commit comments