py_to_c_template----------------------------------------------------------------------------
speed note
the convert_to_int macro below takes about 25 ns per conversion on my
850 MHz PIII. A slightly more sophisticated macro version can trim this
to 20 ns, but this savings is dang near useless because the other
overhead swamps it...
----------------------------------------------------------------------------
py_to_c_template = """
class %(type_name)s_handler
{
public:
%(return_type)s convert_to_%(type_name)s(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
%(inc_ref_count)s
if (!py_obj || !%(check_func)s(py_obj))
handle_conversion_error(py_obj,"%(type_name)s", name);
return %(to_c_return)s;
}
%(return_type)s py_to_%(type_name)s(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !%(check_func)s(py_obj))
handle_bad_type(py_obj,"%(type_name)s", name);
%(inc_ref_count)s
return %(to_c_return)s;
}
};
%(type_name)s_handler x__%(type_name)s_handler = %(type_name)s_handler();
#define convert_to_%(type_name)s(py_obj,name) \
x__%(type_name)s_handler.convert_to_%(type_name)s(py_obj,name)
#define py_to_%(type_name)s(py_obj,name) \
x__%(type_name)s_handler.py_to_%(type_name)s(py_obj,name)
"""