Thread: [pygccxml-development] [FT] recent changes - generated code
Brought to you by:
mbaas,
roman_yakovenko
From: Roman Y. <rom...@gm...> - 2006-09-20 18:50:43
|
Hi. Few comments on generated code: 1. Boost.Python object.hpp defines "len" function. This will impove next code: if (v.attr("__len__")()!=3) { PyErr_SetString(PyExc_ValueError, "Invalid sequence size (expected 3)"); boost::python::throw_error_already_set(); } 2. The next pattern is comming all the time: if( not condition ){ set python error throw boost::python exception } I know we are talking about code generator, but may be we can introduce few convinience functions? namespace pyplusplus{ namespace convinience{ void raise_error( exception, message ){ PyErr_SetString(exception, message); boost::python::throw_error_already_set(); } } } 3. py_v = pyresult; if (!PySequence_Check(py_v.ptr())) { PyErr_SetString(PyExc_ValueError, "fixed_output_array: sequence expected as return value for output array 'v'"); boost::python::throw_error_already_set(); } // Assert that the sequence has the correct size if (PySequence_Length(py_v.ptr())!=3) { PyErr_SetString(PyExc_ValueError, "fixed_output_array: sequence with 3 values expected as return value for output array 'v'"); boost::python::throw_error_already_set(); } It is obvious to me that we have to have hear "ensure_sequence" function template< class ItemType=any > void ensure_sequence( PyObject* x, Py_ssize_t expected_size=-1 ) { if not sequence: raise if does not have desired length raise if items in sequence are not from desired type raise } Something like this. I would like to see these changes. The generated code should not be too different from the one, that human would write. I don't write code like this. P.S. Matthias I hope you don't mind I do the review, right? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |
From: Matthias B. <ba...@ir...> - 2006-09-21 10:11:07
|
Roman Yakovenko wrote: > 1. Boost.Python object.hpp defines "len" function. > This will impove next code: > > if (v.attr("__len__")()!=3) Can you point me to the documentation of this function? At least in v1.33.1 it's not a member function of the object class. The following didn't work: if (v.len()!=3) > I know we are talking about code generator, but may be we can introduce few > convinience functions? > > namespace pyplusplus{ namespace convinience{ > void raise_error( exception, message ){ > PyErr_SetString(exception, message); > boost::python::throw_error_already_set(); > } > } } What shall I call the file in the code repository? convenience.py? > It is obvious to me that we have to have hear "ensure_sequence" function > > template< class ItemType=any > > void ensure_sequence( PyObject* x, Py_ssize_t expected_size=-1 ) { > if not sequence: raise > if does not have desired length raise > if items in sequence are not from desired type raise > } I suppose this is also a candidate for the pyplusplus::convenience namespace, right? Do you want me to put this into a separate file or can I add it to the above convenience file? > P.S. Matthias I hope you don't mind I do the review, right? Not at all. - Matthias - |
From: Roman Y. <rom...@gm...> - 2006-09-21 10:51:40
|
On 9/21/06, Matthias Baas <ba...@ir...> wrote: > Roman Yakovenko wrote: > > 1. Boost.Python object.hpp defines "len" function. > > This will impove next code: > > > > if (v.attr("__len__")()!=3) > > Can you point me to the documentation of this function? At least in > v1.33.1 it's not a member function of the object class. The following > didn't work: > > if (v.len()!=3) boost::python::len, it it defined in boost/python/object.hpp > > I know we are talking about code generator, but may be we can introduce few > > convinience functions? > > > > namespace pyplusplus{ namespace convinience{ > > void raise_error( exception, message ){ > > PyErr_SetString(exception, message); > > boost::python::throw_error_already_set(); > > } > > } } > > What shall I call the file in the code repository? convenience.py? I think yes. Although you can suggest another name > > It is obvious to me that we have to have hear "ensure_sequence" function > > > > template< class ItemType=any > > > void ensure_sequence( PyObject* x, Py_ssize_t expected_size=-1 ) { > > if not sequence: raise > > if does not have desired length raise > > if items in sequence are not from desired type raise > > } > > I suppose this is also a candidate for the pyplusplus::convenience > namespace, right? Do you want me to put this into a separate file or can > I add it to the above convenience file? I think we can put it in the same "convinience.py" file. Also in future we can move the functionality in another( may be better may be more specific ) namespace/file. > > P.S. Matthias I hope you don't mind I do the review, right? > > Not at all. Cool. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |