-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #466 from ManifoldFR/topic/add-deprecation-call-po…
…licy Add deprecation call policy. Backport from pinocchio bindings.
- Loading branch information
Showing
7 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// Copyright (C) 2020 INRIA | ||
// Copyright (C) 2024 LAAS-CNRS, INRIA | ||
// | ||
#ifndef __eigenpy_deprecation_hpp__ | ||
#define __eigenpy_deprecation_hpp__ | ||
|
||
#include "eigenpy/fwd.hpp" | ||
|
||
namespace eigenpy { | ||
|
||
enum class DeprecationType { DEPRECATION, FUTURE }; | ||
|
||
namespace detail { | ||
|
||
constexpr PyObject *deprecationTypeToPyObj(DeprecationType dep) { | ||
switch (dep) { | ||
case DeprecationType::DEPRECATION: | ||
return PyExc_DeprecationWarning; | ||
case DeprecationType::FUTURE: | ||
return PyExc_FutureWarning; | ||
} | ||
} | ||
|
||
} // namespace detail | ||
|
||
/// @brief A Boost.Python call policy which triggers a Python warning on | ||
/// precall. | ||
template <DeprecationType deprecation_type = DeprecationType::DEPRECATION, | ||
class BasePolicy = bp::default_call_policies> | ||
struct deprecation_warning_policy : BasePolicy { | ||
using result_converter = typename BasePolicy::result_converter; | ||
using argument_package = typename BasePolicy::argument_package; | ||
|
||
deprecation_warning_policy(const std::string &warning_msg) | ||
: BasePolicy(), m_what(warning_msg) {} | ||
|
||
std::string what() const { return m_what; } | ||
|
||
const BasePolicy *derived() const { | ||
return static_cast<const BasePolicy *>(this); | ||
} | ||
|
||
template <class ArgPackage> | ||
bool precall(const ArgPackage &args) const { | ||
PyErr_WarnEx(detail::deprecationTypeToPyObj(deprecation_type), | ||
m_what.c_str(), 1); | ||
return derived()->precall(args); | ||
} | ||
|
||
protected: | ||
const std::string m_what; | ||
}; | ||
|
||
template <DeprecationType deprecation_type = DeprecationType::DEPRECATION, | ||
class BasePolicy = bp::default_call_policies> | ||
struct deprecated_function | ||
: deprecation_warning_policy<deprecation_type, BasePolicy> { | ||
deprecated_function(const std::string &msg = | ||
"This function has been marked as deprecated, and " | ||
"will be removed in the future.") | ||
: deprecation_warning_policy<deprecation_type, BasePolicy>(msg) {} | ||
}; | ||
|
||
template <DeprecationType deprecation_type = DeprecationType::DEPRECATION, | ||
class BasePolicy = bp::default_call_policies> | ||
struct deprecated_member | ||
: deprecation_warning_policy<deprecation_type, BasePolicy> { | ||
deprecated_member(const std::string &msg = | ||
"This attribute or method has been marked as " | ||
"deprecated, and will be removed in the future.") | ||
: deprecation_warning_policy<deprecation_type, BasePolicy>(msg) {} | ||
}; | ||
|
||
} // namespace eigenpy | ||
|
||
#endif // ifndef __eigenpy_deprecation_hpp__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "eigenpy/eigenpy.hpp" | ||
#include "eigenpy/deprecation-policy.hpp" | ||
|
||
#include <iostream> | ||
|
||
namespace bp = boost::python; | ||
using eigenpy::DeprecationType; | ||
|
||
void some_deprecated_function() { | ||
std::cout << "Calling this should produce a warning" << std::endl; | ||
} | ||
|
||
void some_future_deprecated_function() { | ||
std::cout | ||
<< "Calling this should produce a warning about a future deprecation" | ||
<< std::endl; | ||
} | ||
|
||
class X { | ||
public: | ||
void deprecated_member_function() {} | ||
}; | ||
|
||
BOOST_PYTHON_MODULE(deprecation_policy) { | ||
bp::def("some_deprecated_function", some_deprecated_function, | ||
eigenpy::deprecated_function<DeprecationType::DEPRECATION>()); | ||
bp::def("some_future_deprecated_function", some_future_deprecated_function, | ||
eigenpy::deprecated_function<DeprecationType::FUTURE>()); | ||
|
||
bp::class_<X>("X", bp::init<>(bp::args("self"))) | ||
.def("deprecated_member_function", &X::deprecated_member_function, | ||
eigenpy::deprecated_member<>()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from deprecation_policy import ( | ||
X, | ||
some_deprecated_function, | ||
some_future_deprecated_function, | ||
) | ||
|
||
some_deprecated_function() | ||
some_future_deprecated_function() | ||
X().deprecated_member_function() |