[C++-sig] Re: (return_self_policy / return_arg): Keywors

Nikolay Mladenov nickm at sitius.com
Sun Jun 29 22:56:16 CEST 2003


Dave,

That is what I have as diffs now. The diffs are from somewhat old cvs
state (1.29.0).
I've been using it for quite some time for init<> and it seems to be
working nicely, but I have no tests, nor docs.
If you approve this though, I will write docs and tests.

Thanks,

Nikolay

David Abrahams wrote:
> 
> Nikolay Mladenov <nickm at sitius.com> writes:
> 
> > PS. How can I get you to commit the pathes I posted some time ago about
> > keywords?
> 
> Nikolay,
> 
> If they got lost in the shuffle, I sincerely apologize.  Please post
> a pointer to the post containing the patches.  I'll try to look them
> over this week during my "vacation" ;-)
> 
> --
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com
-------------- next part --------------
44a45,51
>       keywords<size+1> operator , (const keywords<1> &k) const
>       {
>           python::detail::keywords<size+1> res;
>           std::copy(elements, elements+size, res.elements);
>           res.elements[size] = k.elements[0];
>           return res;
>       }
46a54,56
>       
> 
> 
98c108,120
< #  define BOOST_PYTHON_ASSIGN_NAME(z, n, _) result.elements[n].name = name##n;
---
> struct arg : detail::keywords<1>
> {
>   template <class T>
>       arg &operator = (T const &value)
>   {
>       elements[0].default_value = handle<>(python::borrowed(object(value).ptr()));
>       return *this;
>   }
>   arg (char const *name){elements[0].name = name;}
>   operator detail::keyword const &()const {return elements[0];}
> };
> 
> #  define BOOST_PYTHON_ASSIGN_NAME(z, n, _) result.elements[n] = kwd##n;
100c122
< inline detail::keywords<n> args(BOOST_PP_ENUM_PARAMS_Z(1, n, char const* name)) \
---
> inline detail::keywords<n> args(BOOST_PP_ENUM_PARAMS_Z(1, n, detail::keyword const& kwd)) \
-------------- next part --------------
21a22
>       keyword(char const* n=0):name(n){}
-------------- next part --------------
57a58
>     unsigned m_nkeyword_values;
-------------- next part --------------
35a36
>       , m_nkeyword_values(0)
42d42
<             
48a49,50
>             object tpl (handle<>(PyTuple_New(names_and_defaults[i].default_value?2:1)));
> 
50,51c52,53
<                 m_arg_names.ptr()
<                 , i + keyword_offset
---
>                 tpl.ptr()
>                 , 0
55a58,72
>             if(names_and_defaults[i].default_value){
>                 PyTuple_SET_ITEM(
>                     tpl.ptr()
>                     , 1
>                     , incref(names_and_defaults[i].default_value.get())
>                     );
>                 ++m_nkeyword_values;
>             }
> 
> 
>             PyTuple_SET_ITEM(
>                 m_arg_names.ptr()
>                 , i + keyword_offset
>                 , incref(tpl.ptr())
>                 );
82c99
<         if (total_args >= f->m_min_arity && total_args <= f->m_max_arity)
---
>         if (total_args+f->m_nkeyword_values >= f->m_min_arity && total_args <= f->m_max_arity)
85c102
<             if (nkeywords > 0)
---
>             if (nkeywords > 0 || total_args < f->m_min_arity)
94,95c111,112
<                     // build a new arg tuple
<                     args2 = handle<>(PyTuple_New(total_args));
---
>                     // build a new arg tuple, will adjust its size later
>                     args2 = handle<>(PyTuple_New(f->m_max_arity));
102c119,120
<                     for (std::size_t j = nargs; j < total_args; ++j)
---
>                     std::size_t j = nargs, k = nargs, size=PyTuple_GET_SIZE(f->m_arg_names.ptr());
>                     for (; j < f->m_max_arity && j<size ; ++j)
104,105c122,125
<                         PyObject* value = PyDict_GetItem(
<                             keywords, PyTuple_GET_ITEM(f->m_arg_names.ptr(), j));
---
>                         PyObject* kwd=PyTuple_GET_ITEM(f->m_arg_names.ptr(), j);
> 
>                         PyObject* value = nkeywords?PyDict_GetItem(
>                             keywords, PyTuple_GET_ITEM(kwd, 0)) : 0;
108a129,132
>                             if(PyTuple_GET_SIZE(kwd)>1)
>                                 value = PyTuple_GET_ITEM(kwd, 1);
>                             if (!value)
>                             {
112a137
>                         }else ++k;
114a140,156
> 
>                     if(args2.get()){
>                         //check if we proccessed all the arguments
>                         if(k < total_args)
>                             args2 = handle<>();
> 
>                         //adjust the parameter tuple size
>                         if(j<f->m_max_arity){
>                             handle<> args3( PyTuple_New(j) );
>                             for(size_t l=0; l!=j; ++l)
>                             {
>                                 PyTuple_SET_ITEM(args3.get(), l, PyTuple_GET_ITEM(args3.get(), l));
>                                 PyTuple_SET_ITEM(args2.get(), l, 0);
>                             }
>                             args2 = args3;
>                         }
>                     }


More information about the Cplusplus-sig mailing list