Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking whether a converter has already been registered

I have several modules which define converters for some trivial types (such as list of ints as std::vector<int>); they are parts of independent modules, but they are sometimes both used in one script, which leads to

RuntimeWarning: to-Python converter for std::vector<int, std::allocator<int> > already registered; second conversion method ignored.

How can I check that converter for some type is already define and skip the second registration?

like image 254
eudoxos Avatar asked Mar 27 '12 11:03

eudoxos


1 Answers

boost::python::type_info info = boost::python::type_id<YourType>(); 
const boost::python::converter::registration* reg = boost::python::converter::registry::query(info); 
if (reg == NULL)  {
  //register YourType 
} else if ((*reg).m_to_python == NULL) {
  //register YourType 
}

Note that you need to check also for ((*reg).m_to_python == NULL) otherwise you risk, in some architectures, that the registration does not happen as a default constructor because registration has been called assigning a NULL converter to YourType. In this case query(info) does return the address of the empty registration.

like image 81
Dario Izzo Avatar answered Oct 23 '22 19:10

Dario Izzo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!