Support rpmver-py comparison operator inheritance

Although we were able to write python rpm.ver subclasses, we were not
actually able to inherit the comparison operators of the base class:
Trying to compare instances of the subclass would raise a
NotImplementedError.

The reason behind that was the verObject_Check macro checked that an
instance's type was matching rpmver_Type, rather than running a wider
check. Then, that check is applied to each of the operands of a
comparison by the function implementing the tp_richcompare slot
function.

This patch turns the type check into a PyObject_TypeCheck call, which
will also permit subclasses to go through. Then, it adds another test to
cover such behavior.

Signed-off-by: Beñat Gartzia <bgartzia@redhat.com>
This commit is contained in:
Beñat Gartzia 2024-01-11 13:38:39 +01:00 committed by Florian Festi
parent 0583808692
commit 2721a845d3
2 changed files with 28 additions and 1 deletions

View File

@ -8,7 +8,7 @@ typedef struct rpmverObject_s rpmverObject;
extern PyTypeObject* rpmver_Type;
extern PyType_Spec rpmver_Type_Spec;
#define verObject_Check(v) (((PyObject*)v)->ob_type == rpmver_Type)
#define verObject_Check(v) PyObject_TypeCheck(v, rpmver_Type)
int verFromPyObject(PyObject *item, rpmver *ver);
PyObject * rpmver_Wrap(PyTypeObject *subtype, rpmver ver);

View File

@ -748,3 +748,30 @@ for vt in [ (None, "1.0", None), (None, "1.0", "2"), ("1", "1.0", "3") ]:
1:1.0-3
],
[])
RPMPY_TEST([version objects 3],[
class Foo(rpm.ver):
pass
f1 = Foo('v1.0')
v1 = rpm.ver('v1.0')
f2 = Foo('v2.0')
myprint('%s > %s: %s' % (f1, f2, f1 > f2))
myprint('%s < %s: %s' % (f1, f2, f1 < f2))
myprint('%s < %s: %s' % (f1, v1, f1 < v1))
myprint('%s > %s: %s' % (f2, v1, f2 > v1))
myprint('%s > %s: %s' % (v1, f1, v1 > f1))
myprint('%s < %s: %s' % (v1, f2, v1 < f2))
myprint('%s == %s: %s' % (v1, f1, v1 == f1))
myprint('%s == %s: %s' % (f1, f2, f1 == f2))
myprint('%s != %s: %s' % (f1, f2, f1 != f2))
],
[v1.0 > v2.0: False
v1.0 < v2.0: True
v1.0 < v1.0: False
v2.0 > v1.0: True
v1.0 > v1.0: False
v1.0 < v2.0: True
v1.0 == v1.0: True
v1.0 == v2.0: False
v1.0 != v2.0: True
])