John Hunter <jdhunter@...5...> writes:
>> You shouldn't need to with the ft2font extension code, because
>> it uses pycxx which has support for kwarg handling. Eg in the
>> _image.cpp src
>>
>> Py::Object resize(const Py::Tuple& args, const Py::Dict&
>> kwargs);
> [...]
>> args.verify_length(2);
>>
>> int norm = 1; if ( kwargs.hasKey("norm") ) norm = Py::Int(
>> kwargs["norm"] );
> This seems to mean that the function cannot be called using
> the normal Python convention:
>>>> img.resize(10,10,1)
> Traceback (most recent call last): File "<stdin>", line
> 1, in ? IndexError: Unexpected SeqBase<T> length.
The reason it raises is because I told it too
args.verify_length(2);
if you want normal python symantics, you could to something something
like (untested, freestyle code)
int norm(0);
if (args.length()==3)
norm = Py::Int( args[2] );
elif ( kwargs.hasKey("norm") )
norm = Py::Int( kwargs["norm"] );
> Instead you have to do img.resize(10,10,norm=1). This is
> handled transparently by PyArg_ParseTupleAndKeywords: if
> you set the format string to "ii|ii" and list the names of
> all parameters as keywords, you automatically get the
> normal Python convention where the last two args are
> optional and all args are specifiable with their names.
> But I guess this is not so important in extension code that
> only gets called by matplotlib internals and not end users,
> so I changed load_char to use the pycxx convention.
I do think it is useful in the pycxx extension code to stick where
possible to the cxx idioms -- for the most part the code is cleaner to
reads and helps with reference counting, etc.... You can check the
docs at
Writing Python Extensions in C++
there may be a cleaner way to handle kwargs than what I suggested.
JDH