forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WrapperHelpers.cpp
48 lines (43 loc) · 2.06 KB
/
WrapperHelpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "MantidPythonInterface/core/WrapperHelpers.h"
using namespace boost::python;
namespace Mantid::PythonInterface {
/** Checks whether the given object's type dictionary contains the named
*attribute.
*
* Usually boost::python::get_override is used for this check but if the
* object's overridden function is declared on a superclass of the wrapped
* class then get_override always returns true, regardless of whether the
* method has been overridden in Python.
*
* An example is the algorithm hierachy. We export the IAlgorithm interface
* with the name method attach to it. If a class in Python does not
* override the name method then get_override still claims that the
* override exists because it has found the IAlgorithm one
* @param obj :: A pointer to a PyObject
* @param attr :: A string containin the attribute name
* @returns True if the type dictionary contains the attribute
*/
bool typeHasAttribute(PyObject *obj, const char *attr) {
PyObject *cls_dict = obj->ob_type->tp_dict;
object key(handle<>(to_python_value<char const *&>()(attr)));
return PyDict_Contains(cls_dict, key.ptr()) > 0;
}
/** Same as above but taking a wrapper reference instead
* @param wrapper :: A reference to a Wrapper object
* @param attr :: A string containin the attribute name
* @returns True if the type dictionary contains the attribute
*/
bool typeHasAttribute(const boost::python::detail::wrapper_base &wrapper, const char *attr) {
using namespace boost::python::detail;
return typeHasAttribute(wrapper_base_::get_owner(wrapper), attr);
}
} // namespace Mantid::PythonInterface