Ticket #1316: call.patch

File call.patch, 3.5 KB (added by Stefan Seefeld, 15 years ago)

Enhanced object API with test case.

  • boost/python/object_core.hpp

     
    4242
    4343namespace boost { namespace python {
    4444
     45namespace detail {
     46
     47class kwds_proxy;
     48class args_proxy;
     49
     50}
     51
    4552namespace converter
    4653{
    4754  template <class T> struct arg_to_python;
     
    99106# define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_PYTHON_MAX_ARITY, <boost/python/object_call.hpp>))
    100107# include BOOST_PP_ITERATE()
    101108
     109      detail::args_proxy operator* () const;
     110
     111      object operator()(detail::args_proxy const &args) const;
     112      object operator()(detail::args_proxy const &args,
     113                        detail::kwds_proxy const &kwds) const;
     114
     115
    102116      // truth value testing
    103117      //
    104118      operator bool_type() const;
     
    410424// implementation
    411425//
    412426
     427namespace detail {
     428class call_proxy
     429{
     430public:
     431  call_proxy(object target) : m_target(target) {}
     432  operator object() const { return m_target;}
     433
     434 private:
     435    object m_target;
     436};
     437
     438class kwds_proxy : public call_proxy
     439{
     440public:
     441  kwds_proxy(object o = object()) : call_proxy(o) {}
     442};
     443class args_proxy : public call_proxy
     444{
     445public:
     446  args_proxy(object o) : call_proxy(o) {}
     447  kwds_proxy operator* () const { return kwds_proxy(*this);}
     448};
     449}
     450
     451template <typename U>
     452detail::args_proxy api::object_operators<U>::operator* () const
     453{
     454  object_cref2 x = *static_cast<U const*>(this);
     455  return detail::args_proxy(x);
     456}
     457
     458template <typename U>
     459object api::object_operators<U>::operator()(detail::args_proxy const &args) const
     460{
     461  U const& self = *static_cast<U const*>(this);
     462  PyObject *result = PyObject_Call(get_managed_object(self, tag),
     463                                   args.operator object().ptr(),
     464                                   0);
     465  return object(detail::new_reference(result));
     466
     467}
     468
     469template <typename U>
     470object api::object_operators<U>::operator()(detail::args_proxy const &args,
     471                                            detail::kwds_proxy const &kwds) const
     472{
     473  U const& self = *static_cast<U const*>(this);
     474  PyObject *result = PyObject_Call(get_managed_object(self, tag),
     475                                   args.operator object().ptr(),
     476                                   kwds.operator object().ptr());
     477  return object(detail::new_reference(result));
     478
     479}
     480
    413481inline object::object()
    414482    : object_base(python::incref(Py_None))
    415483{}
  • libs/python/test/object.cpp

     
    123123    return !y[key];
    124124}
    125125
     126object test_call(object c, object args, object kwds)
     127{
     128    return c(*args, **kwds);
     129}
     130
    126131bool check_string_slice()
    127132{
    128133    object s("hello, world");
     
    324329    def("test_item", test_item);
    325330    def("test_not_item", test_not_item);
    326331
     332    def("test_call", test_call);
     333
    327334    def("check_binary_operators", check_binary_operators);
    328335    def("check_inplace", check_inplace);
    329336    def("check_string_slice", check_string_slice);
  • libs/python/test/object.py

     
    1515'hello, world!'
    1616>>> number()
    171742
     18>>> def print_args(*args, **kwds):
     19...     print args, kwds
     20>>> test_call(print_args, (0, 1, 2, 3), {'a':'A'})
     21(0, 1, 2, 3) {'a': 'A'}
    1822
    1923>>> test('hi')
    20241