Skip to content

Commit b5bd480

Browse files
committed
Lower-impact solution
1 parent 5129959 commit b5bd480

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Objects/unionobject.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,13 @@ is_unionable(PyObject *obj)
256256
PyObject *
257257
_Py_union_type_or(PyObject* self, PyObject* other)
258258
{
259+
if (!is_unionable(self) || !is_unionable(other)) {
260+
Py_RETURN_NOTIMPLEMENTED;
261+
}
262+
259263
unionbuilder ub;
260-
if (!unionbuilder_init(&ub, true)) {
264+
// unchecked because we already checked is_unionable()
265+
if (!unionbuilder_init(&ub, false)) {
261266
return NULL;
262267
}
263268
if (!unionbuilder_add_single(&ub, self) ||
@@ -388,8 +393,23 @@ static PyGetSetDef union_properties[] = {
388393
{0}
389394
};
390395

396+
static PyObject *
397+
union_nb_or(PyObject *a, PyObject *b)
398+
{
399+
unionbuilder ub;
400+
if (!unionbuilder_init(&ub, true)) {
401+
return NULL;
402+
}
403+
if (!unionbuilder_add_single(&ub, a) ||
404+
!unionbuilder_add_single(&ub, b)) {
405+
unionbuilder_finalize(&ub);
406+
return NULL;
407+
}
408+
return make_union(&ub);
409+
}
410+
391411
static PyNumberMethods union_as_number = {
392-
.nb_or = _Py_union_type_or, // Add __or__ function
412+
.nb_or = union_nb_or, // Add __or__ function
393413
};
394414

395415
static const char* const cls_attrs[] = {

0 commit comments

Comments
 (0)