Deprecate handle::operator== in favor of object_api::is

This commit is contained in:
Dean Moldovan 2017-02-08 01:01:56 +01:00
parent 30d43c4992
commit 36f0a15a49
4 changed files with 12 additions and 8 deletions

View File

@ -551,7 +551,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
object matrix_type = sparse_module.attr( object matrix_type = sparse_module.attr(
rowMajor ? "csr_matrix" : "csc_matrix"); rowMajor ? "csr_matrix" : "csc_matrix");
if (obj.get_type() != matrix_type.ptr()) { if (!obj.get_type().is(matrix_type)) {
try { try {
obj = matrix_type(obj); obj = matrix_type(obj);
} catch (const error_already_set &) { } catch (const error_already_set &) {

View File

@ -278,7 +278,7 @@ protected:
chain = (detail::function_record *) rec_capsule; chain = (detail::function_record *) rec_capsule;
/* Never append a method to an overload chain of a parent class; /* Never append a method to an overload chain of a parent class;
instead, hide the parent's overloads in this case */ instead, hide the parent's overloads in this case */
if (chain->scope != rec->scope) if (!chain->scope.is(rec->scope))
chain = nullptr; chain = nullptr;
} }
// Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing // Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing
@ -1274,7 +1274,7 @@ template <typename... Args> struct init {
using Alias = typename Class::type_alias; using Alias = typename Class::type_alias;
handle cl_type = cl; handle cl_type = cl;
cl.def("__init__", [cl_type](handle self_, Args... args) { cl.def("__init__", [cl_type](handle self_, Args... args) {
if (self_.get_type() == cl_type) if (self_.get_type().is(cl_type))
new (self_.cast<Base *>()) Base(args...); new (self_.cast<Base *>()) Base(args...);
else else
new (self_.cast<Alias *>()) Alias(args...); new (self_.cast<Alias *>()) Alias(args...);
@ -1708,7 +1708,7 @@ inline function get_type_overload(const void *this_ptr, const detail::type_info
Py_file_input, d.ptr(), d.ptr()); Py_file_input, d.ptr(), d.ptr());
if (result == nullptr) if (result == nullptr)
throw error_already_set(); throw error_already_set();
if ((handle) d["self"] == Py_None) if (d["self"].is_none())
return function(); return function();
Py_DECREF(result); Py_DECREF(result);
#endif #endif

View File

@ -110,6 +110,8 @@ public:
PYBIND11_DEPRECATED("call(...) was deprecated in favor of operator()(...)") PYBIND11_DEPRECATED("call(...) was deprecated in favor of operator()(...)")
object call(Args&&... args) const; object call(Args&&... args) const;
/// Equivalent to ``obj is other`` in Python.
bool is(object_api const& other) const { return derived().ptr() == other.derived().ptr(); }
/// Equivalent to ``obj is None`` in Python. /// Equivalent to ``obj is None`` in Python.
bool is_none() const { return derived().ptr() == Py_None; } bool is_none() const { return derived().ptr() == Py_None; }
PYBIND11_DEPRECATED("Use py::str(obj) instead") PYBIND11_DEPRECATED("Use py::str(obj) instead")
@ -167,10 +169,12 @@ public:
/// Return ``true`` when the `handle` wraps a valid Python object /// Return ``true`` when the `handle` wraps a valid Python object
explicit operator bool() const { return m_ptr != nullptr; } explicit operator bool() const { return m_ptr != nullptr; }
/** \rst /** \rst
Check that the underlying pointers are the same. Deprecated: Check that the underlying pointers are the same.
Equivalent to ``obj1 is obj2`` in Python. Equivalent to ``obj1 is obj2`` in Python.
\endrst */ \endrst */
PYBIND11_DEPRECATED("Use obj1.is(obj2) instead")
bool operator==(const handle &h) const { return m_ptr == h.m_ptr; } bool operator==(const handle &h) const { return m_ptr == h.m_ptr; }
PYBIND11_DEPRECATED("Use !obj1.is(obj2) instead")
bool operator!=(const handle &h) const { return m_ptr != h.m_ptr; } bool operator!=(const handle &h) const { return m_ptr != h.m_ptr; }
PYBIND11_DEPRECATED("Use handle::operator bool() instead") PYBIND11_DEPRECATED("Use handle::operator bool() instead")
bool check() const { return m_ptr != nullptr; } bool check() const { return m_ptr != nullptr; }

View File

@ -37,7 +37,7 @@ test_initializer eval([](py::module &m) {
); );
auto x = local["x"].cast<int>(); auto x = local["x"].cast<int>();
return result == py::none() && x == 42; return result.is_none() && x == 42;
}); });
m.def("test_eval", [global]() { m.def("test_eval", [global]() {
@ -55,7 +55,7 @@ test_initializer eval([](py::module &m) {
auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local); auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local);
auto x = local["x"].cast<int>(); auto x = local["x"].cast<int>();
return result == py::none() && x == 42; return result.is_none() && x == 42;
}); });
m.def("test_eval_file", [global](py::str filename) { m.def("test_eval_file", [global](py::str filename) {
@ -66,7 +66,7 @@ test_initializer eval([](py::module &m) {
local["call_test2"] = py::cpp_function([&](int value) { val_out = value; }); local["call_test2"] = py::cpp_function([&](int value) { val_out = value; });
auto result = py::eval_file(filename, global, local); auto result = py::eval_file(filename, global, local);
return val_out == 43 && result == py::none(); return val_out == 43 && result.is_none();
}); });
m.def("test_eval_failure", []() { m.def("test_eval_failure", []() {