id summary reporter owner description type status priority milestone component resolution keywords cc merged author reviewer upstream work_issues branch commit dependencies stopgaps
16537 "Python 3 preparation: Use ""rich comparison"" - std function cmp() is gone, method __cmp__ is ignored" wluebbe "Since Py2.1 there are the //rich comparison// special methods ({{{__eq__, __ne__, __lt__, __le__, __gt__, __ge__}}}) to implement comparison operators ({{{==, !=, <, <=, >, >=}}} respectively) for custom classes.
This is more flexible (but somewhat more tedious) than defining the special method {{{__cmp__}}}.
In Py3 the special method {{{__cmp__}}} is ignored. The standard function {{{cmp()}}} (which is mostly used to implement {{{__cmp__}}} methods) is gone.
One may define a replacement function like
{{{
def cmp(a, b):
return (a > b) - (a < b)
}}}
But this does not improved performance. And it does not seem forward looking ...
Unfortunately {{{__cmp__}}} and {{{cmp()}}} are used a LOT in Sage. And the migration to rich comparison can not be done purely mechanical :-(
Since Py2.7 there is a class decorator [[https://docs.python.org/2/library/functools.html?highlight=total_ordering#functools.total_ordering|functools.total_ordering]] to help to create the full set of special method {{{__lt__, __le__, __gt__, __ge__}}} when given one of those.
[[br]]
See Lennart Regebro's [[http://python3porting.com/preparing.html|Chapter: Use rich comparison operators]] for a more detailed discussion.
Useful tool to see the size of the remaining problem:
{{{
git grep -c ""^[^#]*[^a-z\._]cmp("" *.py
git grep -c ""^[^#]*[^a-z\._]cmp("" *.pyx
}}}
This ticket is tracked as a dependency of meta-ticket ticket:15980.
----
== Progress ==
Several tickets have already been submitted and merged for fixing `__cmp__` -> `__richcmp__` issues in Python 3. The remaining issues will be tracked here:
=== Open ===
[[TicketQuery(keywords~=richcmp&status=new)]]
=== In Progress ===
[[TicketQuery(keywords~=richcmp&status!=new&status!=closed)]]
=== Modules needing work ===
The following modules still contain `__cmp__` either in user-visible classes or in tests that may need to be addressed for Python 3.
||= '''Ticket''' =||= '''Module''' =||= Notes =||
||#24831||src/sage/categories/functor.pyx ||||
||#24930||src/sage/crypto/boolean_function.pyx ||||
||#25180||src/sage/dynamics/flat_surfaces/strata.py ||deprecated in #20695||
||#25180||src/sage/dynamics/interval_exchanges/labelled.py ||deprecated in #20695||
||#25180||src/sage/dynamics/interval_exchanges/reduced.py ||deprecated in #20695||
||#24930||src/sage/finance/time_series.pyx ||||
||#25063 ||src/sage/interfaces/axiom.py ||||
||#25063 ||src/sage/interfaces/giac.py ||||
||#25063 ||src/sage/interfaces/interface.py ||||
||#25063 ||src/sage/interfaces/lisp.py ||||
||#25063 ||src/sage/interfaces/maple.py ||||
||#25063 ||src/sage/interfaces/mathematica.py ||||
||#25063 ||src/sage/interfaces/polymake.py ||||
||#25063 ||src/sage/interfaces/r.py ||||
||#24793||src/sage/misc/fast_methods.pyx ||||
||#25778||src/sage/misc/lazy_import.pyx ||The `__cmp__` here returns an error since #21247 but is not removed. Full removal in #25778||
||#24899||src/sage/modular/modsym/p1list.pyx ||||
||#25412||src/sage/modules/module.pyx ||Here `__cmp__`is only in the doc.||
||#25053||src/sage/monoids/free_monoid_element.py ||||
||#25059||src/sage/monoids/string_monoid.py ||||
||#25419||src/sage/numerical/linear_functions.pyx ||Strange distinct uses of both old cmp and new richcmp||
||#25623||src/sage/numerical/linear_tensor_element.pyx ||Strange distinct uses of both old cmp and new richcmp||
||#25060||src/sage/rings/polynomial/multi_polynomial_element.py ||||
||#24931||src/sage/rings/real_mpfr.pyx ||||
||#24980||src/sage/schemes/generic/ambient_space.py ||||
||#24930||src/sage/stats/intlist.pyx ||||
||#24791||src/sage/structure/unique_representation.py ||{{{__cmp__}}} only in the doc||
||#24980||src/sage/tensor/modules/free_module_morphism.py ||||
||#?????||src/sage/structure/element.pyx||remaining calls to cmp() inside the documentation||" enhancement new major sage-8.3 python3 python3, richcmp jpflori N/A