From: John H. <jdh...@ac...> - 2006-04-26 13:40:05
|
>>>>> "Jouni" == Jouni K Seppanen <jk...@ik...> writes: Jouni> John Hunter <jdh...@ac...> 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); Jouni> [...] >> args.verify_length(2); >> >> int norm = 1; if ( kwargs.hasKey("norm") ) norm = Py::Int( >> kwargs["norm"] ); Jouni> This seems to mean that the function cannot be called using Jouni> the normal Python convention: >>>> img.resize(10,10,1) Jouni> Traceback (most recent call last): File "<stdin>", line Jouni> 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"] ); Jouni> Instead you have to do img.resize(10,10,norm=1). This is Jouni> handled transparently by PyArg_ParseTupleAndKeywords: if Jouni> you set the format string to "ii|ii" and list the names of Jouni> all parameters as keywords, you automatically get the Jouni> normal Python convention where the last two args are Jouni> optional and all args are specifiable with their names. Jouni> But I guess this is not so important in extension code that Jouni> only gets called by matplotlib internals and not end users, Jouni> 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 http://cxx.sourceforge.net/PyCXX.html there may be a cleaner way to handle kwargs than what I suggested. JDH |