[Python-Dev] Detecting tp_compare / tp_richcompare from Python

Guido van Rossum guido at python.org
Thu Nov 8 18:38:46 CET 2012


Well, the default behavior has changed to raise an exception when
using <, <=, >, >=; i.e., inequalities do not have a default
implementation at all. Perhaps that is enough for your purpose? == and
!= still compare by pointer, but you're primarily interested in
ordering her, right?

On Thu, Nov 8, 2012 at 8:18 AM, Tres Seaver <tseaver at palladion.com> wrote:
> -----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-----
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org



-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list