10000 Implement __eq__ for template and interpolation · python/cpython@17014e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17014e7

Browse files
committed
Implement __eq__ for template and interpolation
1 parent 0dda3f9 commit 17014e7

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Objects/interpolationobject.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,40 @@ interpolation_repr(interpolationobject *self)
6666
self->conv, self->format_spec);
6767
}
6868

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+
69103
static PyMemberDef interpolation_members[] = {
70104
{"value", Py_T_OBJECT_EX, offsetof(interpolationobject, value), Py_READONLY, "Value"},
71105
{"expr", Py_T_OBJECT_EX, offsetof(interpolationobject, expr), Py_READONLY, "Expr"},
@@ -84,6 +118,7 @@ PyTypeObject _PyInterpolation_Type = {
84118
.tp_new = (newfunc) interpolation_new,
85119
.tp_dealloc = (destructor) interpolation_dealloc,
86120
.tp_repr = (reprfunc) interpolation_repr,
121+
.tp_richcompare = (richcmpfunc) interpolation_compare,
87122
.tp_members = interpolation_members,
88123
};
89124

Objects/templateobject.c

Lines 10000 changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ template_repr(templateobject *self)
5050
self->args);
5151
}
5252

53+
static PyObject *
54+
template_compare(templateobject *self, PyObject *other, int op)
55+
{
56+
if (op == Py_LT || op == Py_LE || op == Py_GT || op == Py_GE) {
57+
Py_RETURN_NOTIMPLEMENTED;
58+
}
59+
60+
if (!PyObject_TypeCheck(other, &_PyTemplate_Type)) {
61+
return (op == Py_EQ) ? Py_False : Py_True;
62+
}
63+
64+
return PyObject_RichCompare(self->args, ((templateobject *) other)->args, op);
65+
}
66+
5367
static PyObject *
5468
template_add_template_str(templateobject *template, PyUnicodeObject *str, int templateleft)
5569
{
@@ -125,7 +139,7 @@ static PyMemberDef template_members[] = {
125139
};
126140

127141
static PyNumberMethods template_as_number = {
128-
.nb_add = template_add
142+
.nb_add = template_add,
129143
};
130144

131145
PyTypeObject _PyTemplate_Type = {
@@ -139,6 +153,7 @@ PyTypeObject _PyTemplate_Type = {
139153
.tp_new = (newfunc) template_new,
140154
.tp_dealloc = (destructor) template_dealloc,
141155
.tp_repr = (reprfunc) template_repr,
156+
.tp_richcompare = (richcmpfunc) template_compare,
142157
.tp_members = template_members,
143158
};
144159

0 commit comments

Comments
 (0)
0