Skip to content

Implement @total_ordering for cdef classes #2090

@scoder

Description

@scoder

We now support the normal Python comparison methods like __eq__() etc., but it would simplify their usage to auto-generate the obvious mappings when the @functools.total_ordering decorator is used. At the C level, this could probably be done without a performance penalty.

See https://docs.python.org/3/library/functools.html#functools.total_ordering

Since this only applies to cdef classes, it seems better to use a safe and dedicated separate decorator @cython.total_ordering than to rely on the generic Python decorator.

Approach:

  • Support the new decorator as a new directive in Options.py, similar to @no_gc_clear.
  • In generate_richcmp_function() (in ModuleNode.py), add default implementations of the missing comparison functions to the C switch statement if the directive is set.
  • Add a new test file tests/run/exttype_total_ordering.pyx, implement different cdef class configurations in it, and test their comparisons with Python doctests. You can also implement normal Python classes in these doctests for direct comparison of Python's own behaviour.
  • Run the tests with python[2|3] runtests.py -vv --debug exttype_total_ordering. The generated code can be found in the TEST_TMP/run/c/ directory.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions