ivoflipse / pydicom

Automatically exported from code.google.com/p/pydicom
0 stars 1 forks source link

UID == x evaluates to true for non-str values of x #96

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Python 2.7 (r27:82508, Jul  3 2010, 21:12:11) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dicom
>>> u = dicom.UID.UID('1.2.840.10008.1.2.4.50')
>>> u
'1.2.840.10008.1.2.4.50'
>>> u == 3
True
>>> u == None
True

This is due to how the UID.__eq__ function uses str.__eq__:

>>> str.__eq__(u, None)
NotImplemented
>>> print type(str.__eq__(u, None))
<type 'NotImplementedType'>

>>> bool(NotImplemented)
True

Using str(u) == None gives the expected answer of False.  I think that using 
'is True' in the comparison function will work:

    def __eq__(self, other):
        """Override string equality so either name or UID number match passes"""
        if str.__eq__(self, other) is True:
            return True
        if str.__eq__(self.name, other) is True:
            return True
        return False

That could be shortened to:

        return str.__eq__(self, other) is True or str.__eq__(self.name, other) is True

I'm not 100% certain that either of those handle all cases, though.

Original issue reported on code.google.com by wickedg...@gmail.com on 15 Jan 2011 at 1:48

GoogleCodeExporter commented 9 years ago
This issue was closed by revision 156044badd.

Original comment by darcymason@gmail.com on 18 Jan 2011 at 5:40