[Python-Dev] Detecting tp_compare / tp_richcompare from Python

Tres Seaver tseaver at palladion.com
Thu Nov 8 17:18:32 CET 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

While porting the BTrees package (split out from ZODB) to Python3., my
first step is to get the pure-Python reference implementation working:
in order to do that, I need a way to check that objects used as keys are
*not* using the comparison semantics inherited from the base 'object'
class, because those semantics rely on properties which are not stable
across the lifetime of the (persisted / restored) key.

The existing C code does something like::

 static int
 check_argument_cmp(PyObject *arg)
 {
    if (arg->ob_type->tp_richcompare == NULL
       && arg->ob_type->tp_compare ==
        ((PyTypeObject *)object_)->ob_type->tp_compare)
     {
       PyErr_SetString(PyExc_TypeError, "Object has default comparison");
       return 0;
     }
   return 1;
 }

Unless I'm mistaken, there is no way to do the equivalent from pure
Python.. I tried a couple of approximations which relied on non-API
attributes (I"m looking at out, methodwrapper.__objclass__), but without
much success (and I need the code to work on PyPy / Jython, too).

Am I missing something?



Tres.
- -- 
===================================================================
Tres Seaver          +1 540-429-0999          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iEYEARECAAYFAlCb21gACgkQ+gerLs4ltQ4jBACfV0lQaQ2eW2vhAtWunLUsPQWM
esEAoMYeeQvlJVnckaBg4HM19LoxPIWB
=+d+0
-----END PGP SIGNATURE-----



More information about the Python-Dev mailing list