• Kirill Smelkov's avatar
    golang_str: Fix bstr/ustr slice access on py2 · 300d7dfa
    Kirill Smelkov authored
    In the patch "golang_str: bstr/ustr index access" we added __getitem__
    implementation for bstr/ustr and thorough corresponding tests to cover
    all access cases: [i], [i:j] and [i:j:k].
    
    The tests, however, are run via pytest which does AST rewriting, and, as
    it turned out, always invokes __getitem__ even for [i:j] case even on py2.
    Which differs from plain python2 behaviour to invoke __getslice__ for
    [i:j] case if __getslice__ slot is present.
    
    Since on py2 both str and unicode provide __getslice__ implementation,
    and bstr/ustr inherit from those types, they also inherit __getslice__.
    And oops, then on py2 e.g. bstr[i:j] was returning str instead of bstr:
    
        In [1]: bs = b('αβγ')
    
        In [2]: bs
        Out[2]: b('αβγ')
    
        In [3]: bs[0]
        Out[3]: b(b'\xce')
    
        In [4]: bs[0:1]
        Out[4]: '\xce'              <-- NOTE not b(...)
    
        In [5]: type(_)
        Out[5]: str                 <-- NOTE not bstr
    
    -> Fix it by explicitly whiting out __getslice__ slot for bstr and ustr.
    300d7dfa
golang_test_str_index2.py 1.79 KB