• Kirill Smelkov's avatar
    golang_str: Teach bstr/ustr to compare wrt any string with automatic coercion · 54c2a3cf
    Kirill Smelkov authored
    So that e.g. `bstr == <any string type>` works. We want `bstr == ustr`
    to work because we intend those types to be interoperable. We also want
    e.g. `bstr == "a_string"` to work because we want bstr to be
    interoperable with standard strings. In general we want to have full
    automatic interoperability with all string types, so that e.g. `bstr == X`
    works for X being all bstr, ustr, unicode, bytes (and later bytearray).
    
    For now we add support only for comparison operators. But later, we
    will be adding support for e.g. +, string methods, etc - and in all
    those operations we will be following the same approach: to have
    automatic interoperability with all string types out of the box.
    
    The text added to README reflects this.
    
    The patch to unicode.tp_richcompare on py2 illustrates our approach to
    adjust builtin types when absolutely needed. In this particular case
    original builtin unicode.__eq__(unicode, bstr) is always returning False
    for non-ASCII bstr even despite bstr having .__unicode__() method. Our
    adjustment is non-intrusive - we adjust unicode behaviour only wrt bstr
    and it stays exactly the same as before wrt all other types.
    
    We anyway do that with care and add a test that verifies that behaviour
    of what we patched stays unaffected when used outside of bstr/ustr
    context.
    54c2a3cf
_golang_str.pyx 18.6 KB