@@ -66,6 +66,40 @@ interpolation_repr(interpolationobject *self)
66
66
self -> conv , self -> format_spec );
67
67
}
68
68
69
+ static PyObject *
70
+ interpolation_compare (interpolationobject * self , PyObject * other , int op )
71
+ {
72
+ if (op == Py_LT || op == Py_LE || op == Py_GT || op == Py_GE ) {
73
+ Py_RETURN_NOTIMPLEMENTED ;
74
+ }
75
+
76
+ if (!PyObject_TypeCheck (other , & _PyInterpolation_Type )) {
77
+ return (op == Py_EQ ) ? Py_False : Py_True ;
78
+ }
79
+
80
+ interpolationobject * other_i = (interpolationobject * ) other ;
81
+
82
+ int valueeq = PyObject_RichCompareBool (self -> value , other_i -> value , Py_EQ );
83
+ if (valueeq == -1 ) {
84
+ return NULL ;
85
+ }
86
+ int expreq = PyUnicode_Compare (self -> expr , other_i -> expr );
87
+ if (expreq == -1 && PyErr_Occurred ()) {
88
+ return NULL ;
89
+ }
90
+ int conveq = PyObject_RichCompareBool (self -> conv , other_i -> conv , Py_EQ ); // conv might be Py_None
91
+ if (conveq == -1 ) {
92
+ return NULL ;
93
+ }
94
+ int formatspeceq = PyUnicode_Compare (self -> format_spec , other_i -> format_spec );
95
+ if (formatspeceq == -1 && PyErr_Occurred ()) {
96
+ return NULL ;
97
+ }
98
+
99
+ int eq = valueeq && expreq == 0 && conveq && formatspeceq == 0 ;
100
+ return PyBool_FromLong (op == Py_EQ ? eq : !eq );
101
+ }
102
+
69
103
static PyMemberDef interpolation_members [] = {
70
104
{"value" , Py_T_OBJECT_EX , offsetof(interpolationobject , value ), Py_READONLY , "Value" },
71
105
{"expr" , Py_T_OBJECT_EX , offsetof(interpolationobject , expr ), Py_READONLY , "Expr" },
@@ -84,6 +118,7 @@ PyTypeObject _PyInterpolation_Type = {
84
118
.tp_new = (newfunc ) interpolation_new ,
85
119
.tp_dealloc = (destructor ) interpolation_dealloc ,
86
120
.tp_repr = (reprfunc ) interpolation_repr ,
121
+ .tp_richcompare = (richcmpfunc ) interpolation_compare ,
87
122
.tp_members = interpolation_members ,
88
123
};
89
124
0 commit comments