Boost logo

Boost :

From: nksauter_at_[hidden]
Date: 2001-05-22 02:58:15

As a follow up to my previous posting, I have implemented
Boost.Python support for Python 2.1 rich comparisons.
Initially I was only able to customize the comparison methods
(__lt__, __le__, etc.), but subsequently I overloaded the Python
operators themselves, so that a < b will give a result of
any desired type, rather than an integer 0 or 1.

The key to doing this is that a pointer to a user-defined rich
comparison function must be registered in the PyTypeObject of the
extension class; also a bit flag must be set indicating that rich
comparisons are available. Thus the following lines of code are
added to Boost.Python types.cpp:
  dest->tp_richcompare = &do_instance_richcompare;
  dest->tp_flags |= Py_TPFLAGS_HAVE_RICHCOMPARE;
I also added appropriate guards so that Boost.Python is still
backwards compatible with earlier Python versions.

I'm placing an overlay tarball with my source code and test case
on my web page at,
along with a makefile at The
overlay is meant to be used in conjunction with boost_1_21_2. I've
tested this on Compaq alpha/cxx, Linux/gcc, and Irix/CC; with Python
2.1 and Python 1.5.2.

Hopefully we can see something like this incorporated into a future
Boost release.

Nick Sauter, LBNL

Boost list run by bdawes at, gregod at, cpdaniel at, john at