Fix fatal error when combining segmentlists with invalid values
An expression like this with a binary operation between a segmentlist on the right hand side and certain incompatible values on the left hand side would crash a Python 3 interpreter:
>>> from glue.segments import segmentlist
>>> None | segmentlist()
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: None
Fatal Python error: deallocating None
Current thread 0x00007fffa0e7e340 (most recent call first):
Abort trap: 6
Several such expressions are evaluated in
test_segmentlist.test_typesafety
in segments_verify.py
, so this
would cause the unit tests to fail.
The reason was that the C implementations of segmentlist.__and__
,
segmentlist.__or__
, and segmentlist.__sub__
assumed that the left hand
side argument was an instance of segmentlist
. However, this is not
necessarily the case as Python will try both v.__or__(v, w)
and
w.__or__(v, w)
. Since segmentlist.__and__
was trying to create a new
instance of the class of the left hand side operand, it was basically
trying to instantiate NoneType
, which is all kinds of bad.
The solution is to use the class of the left hand side for the class of
the return value if the left hand side is an instance of segmentlist
, or
the class of the right hand side otherwise.
Fixes #4 (closed).