• Kirill Smelkov's avatar
    golang: Teach qq to be usable with both bytes and str format whatever type qq argument is · edc7aaab
    Kirill Smelkov authored
    qq is used to quote strings or byte-strings. The following example
    illustrates the problem we are currently hitting in zodbtools with
    Python3:
    
        >>> "hello %s" % qq("мир")
        'hello "мир"'
    
        >>> b"hello %s" % qq("мир")
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        TypeError: %b requires a bytes-like object, or an object that implements __bytes__, not 'str'
    
        >>> "hello %s" % qq(b("мир"))
        'hello "мир"'
    
        >>> b"hello %s" % qq(b("мир"))
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        TypeError: %b requires a bytes-like object, or an object that implements __bytes__, not 'str'
    
    i.e. one way or another if type of format string and what qq returns do not
    match it creates a TypeError.
    
    We want qq(obj) to be useable with both string and bytestring format.
    
    For that let's teach qq to return special str- and bytes- derived types that
    know how to automatically convert to str->bytes and bytes->str via b/u
    correspondingly. This way formatting works whatever types combination it was
    for format and for qq, and the whole result has the same type as format.
    
    For now we teach only qq to use new types and don't generally expose
    _str and _unicode to be returned by b and u yet. However we might do so
    in the future after incrementally gaining a bit more experience.
    
    /proposed-for-review-on: !1
    edc7aaab
_golang.pyx 32.7 KB