24 #ifndef PXR_BASE_TF_PY_CLASS_METHOD_H
25 #define PXR_BASE_TF_PY_CLASS_METHOD_H
29 #include <boost/python/class.hpp>
30 #include <boost/python/dict.hpp>
31 #include <boost/python/object.hpp>
32 #include <boost/python/def_visitor.hpp>
34 PXR_NAMESPACE_OPEN_SCOPE
36 namespace Tf_PyClassMethod {
38 using namespace boost::python;
44 struct _TfPyClassMethod : def_visitor<_TfPyClassMethod>
46 friend class def_visitor_access;
48 _TfPyClassMethod(
const std::string &methodName) :
49 _methodName(methodName) {}
50 explicit _TfPyClassMethod(
const char *methodName) :
51 _methodName(methodName) {}
53 template <
typename CLS>
54 void visit(CLS &c)
const
56 PyTypeObject*
self = downcast<PyTypeObject>( c.ptr() );
57 dict d((handle<>(borrowed(self->tp_dict))));
59 object method(d[_methodName]);
61 c.attr(_methodName.c_str()) =
object(
62 handle<>( PyClassMethod_New((_CallableCheck)(method.ptr()) )));
67 PyObject* _CallableCheck(PyObject* callable)
const
69 if (PyCallable_Check(expect_non_null(callable)))
72 PyErr_Format( PyExc_TypeError,
73 "classmethod expects callable object; got an object of type %s, "
74 "which is not callable",
75 callable->ob_type->tp_name);
77 throw_error_already_set();
81 const std::string _methodName;
98 typedef Tf_PyClassMethod::_TfPyClassMethod TfPyClassMethod;
100 PXR_NAMESPACE_CLOSE_SCOPE
102 #endif // PXR_BASE_TF_PY_CLASS_METHOD_H