Skip to content

Commit 194296c

Browse files
authored
Merge pull request #466 from ManifoldFR/topic/add-deprecation-call-policy
Add deprecation call policy. Backport from pinocchio bindings.
2 parents 24c3628 + 42d683a commit 194296c

File tree

7 files changed

+127
-1
lines changed

7 files changed

+127
-1
lines changed

.github/workflows/linux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
echo $(g++ --version)
4343
- run: cmake . -DPYTHON_EXECUTABLE=$(which python${{ matrix.python }}) -DBUILD_TESTING_SCIPY=ON
4444
- run: make -j2
45-
- run: make test
45+
- run: ctest --output-on-failure
4646

4747
check:
4848
if: always()

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## [Unreleased]
88

9+
### Added
10+
- Added a deprecation call policy shortcut ([#466](https://github.com/stack-of-tasks/eigenpy/pull/466))
11+
912
## [3.5.1] - 2024-04-25
1013

1114
### Fixed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ set(${PROJECT_NAME}_HEADERS
220220
${${PROJECT_NAME}_DECOMPOSITIONS_HEADERS}
221221
include/eigenpy/alignment.hpp
222222
include/eigenpy/computation-info.hpp
223+
include/eigenpy/deprecation-policy.hpp
223224
include/eigenpy/eigenpy.hpp
224225
include/eigenpy/exception.hpp
225226
include/eigenpy/scalar-conversion.hpp
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// Copyright (C) 2020 INRIA
3+
// Copyright (C) 2024 LAAS-CNRS, INRIA
4+
//
5+
#ifndef __eigenpy_deprecation_hpp__
6+
#define __eigenpy_deprecation_hpp__
7+
8+
#include "eigenpy/fwd.hpp"
9+
10+
namespace eigenpy {
11+
12+
enum class DeprecationType { DEPRECATION, FUTURE };
13+
14+
namespace detail {
15+
16+
constexpr PyObject *deprecationTypeToPyObj(DeprecationType dep) {
17+
switch (dep) {
18+
case DeprecationType::DEPRECATION:
19+
return PyExc_DeprecationWarning;
20+
case DeprecationType::FUTURE:
21+
return PyExc_FutureWarning;
22+
}
23+
}
24+
25+
} // namespace detail
26+
27+
/// @brief A Boost.Python call policy which triggers a Python warning on
28+
/// precall.
29+
template <DeprecationType deprecation_type = DeprecationType::DEPRECATION,
30+
class BasePolicy = bp::default_call_policies>
31+
struct deprecation_warning_policy : BasePolicy {
32+
using result_converter = typename BasePolicy::result_converter;
33+
using argument_package = typename BasePolicy::argument_package;
34+
35+
deprecation_warning_policy(const std::string &warning_msg)
36+
: BasePolicy(), m_what(warning_msg) {}
37+
38+
std::string what() const { return m_what; }
39+
40+
const BasePolicy *derived() const {
41+
return static_cast<const BasePolicy *>(this);
42+
}
43+
44+
template <class ArgPackage>
45+
bool precall(const ArgPackage &args) const {
46+
PyErr_WarnEx(detail::deprecationTypeToPyObj(deprecation_type),
47+
m_what.c_str(), 1);
48+
return derived()->precall(args);
49+
}
50+
51+
protected:
52+
const std::string m_what;
53+
};
54+
55+
template <DeprecationType deprecation_type = DeprecationType::DEPRECATION,
56+
class BasePolicy = bp::default_call_policies>
57+
struct deprecated_function
58+
: deprecation_warning_policy<deprecation_type, BasePolicy> {
59+
deprecated_function(const std::string &msg =
60+
"This function has been marked as deprecated, and "
61+
"will be removed in the future.")
62+
: deprecation_warning_policy<deprecation_type, BasePolicy>(msg) {}
63+
};
64+
65+
template <DeprecationType deprecation_type = DeprecationType::DEPRECATION,
66+
class BasePolicy = bp::default_call_policies>
67+
struct deprecated_member
68+
: deprecation_warning_policy<deprecation_type, BasePolicy> {
69+
deprecated_member(const std::string &msg =
70+
"This attribute or method has been marked as "
71+
"deprecated, and will be removed in the future.")
72+
: deprecation_warning_policy<deprecation_type, BasePolicy>(msg) {}
73+
};
74+
75+
} // namespace eigenpy
76+
77+
#endif // ifndef __eigenpy_deprecation_hpp__

unittest/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ endif()
3737
add_lib_unit_test(tensor)
3838
add_lib_unit_test(geometry)
3939
add_lib_unit_test(complex)
40+
add_lib_unit_test(deprecation_policy)
4041
add_lib_unit_test(return_by_ref)
4142
add_lib_unit_test(include)
4243
if(NOT ${EIGEN3_VERSION} VERSION_LESS "3.2.0")
@@ -104,6 +105,8 @@ add_python_lib_unit_test("py-matrix" "unittest/python/test_matrix.py")
104105
add_python_lib_unit_test("py-tensor" "unittest/python/test_tensor.py")
105106
add_python_lib_unit_test("py-geometry" "unittest/python/test_geometry.py")
106107
add_python_lib_unit_test("py-complex" "unittest/python/test_complex.py")
108+
add_python_lib_unit_test("py-deprecation-policy"
109+
"unittest/python/test_deprecation_policy.py")
107110
add_python_lib_unit_test("py-return-by-ref"
108111
"unittest/python/test_return_by_ref.py")
109112
add_python_lib_unit_test("py-eigen-ref" "unittest/python/test_eigen_ref.py")

unittest/deprecation_policy.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "eigenpy/eigenpy.hpp"
2+
#include "eigenpy/deprecation-policy.hpp"
3+
4+
#include <iostream>
5+
6+
namespace bp = boost::python;
7+
using eigenpy::DeprecationType;
8+
9+
void some_deprecated_function() {
10+
std::cout << "Calling this should produce a warning" << std::endl;
11+
}
12+
13+
void some_future_deprecated_function() {
14+
std::cout
15+
<< "Calling this should produce a warning about a future deprecation"
16+
<< std::endl;
17+
}
18+
19+
class X {
20+
public:
21+
void deprecated_member_function() {}
22+
};
23+
24+
BOOST_PYTHON_MODULE(deprecation_policy) {
25+
bp::def("some_deprecated_function", some_deprecated_function,
26+
eigenpy::deprecated_function<DeprecationType::DEPRECATION>());
27+
bp::def("some_future_deprecated_function", some_future_deprecated_function,
28+
eigenpy::deprecated_function<DeprecationType::FUTURE>());
29+
30+
bp::class_<X>("X", bp::init<>(bp::args("self")))
31+
.def("deprecated_member_function", &X::deprecated_member_function,
32+
eigenpy::deprecated_member<>());
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from deprecation_policy import (
2+
X,
3+
some_deprecated_function,
4+
some_future_deprecated_function,
5+
)
6+
7+
some_deprecated_function()
8+
some_future_deprecated_function()
9+
X().deprecated_member_function()

0 commit comments

Comments
 (0)