• 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
Name
Last commit
Last update
..
_patch Loading commit data...
cmd Loading commit data...
pyx Loading commit data...
runtime Loading commit data...
testdata/src/lab.nexedi.com/kirr Loading commit data...
testprog Loading commit data...
x Loading commit data...
.gitignore Loading commit data...
__init__.pxd Loading commit data...
__init__.py Loading commit data...
_context.pxd Loading commit data...
_context.pyx Loading commit data...
_cxx_test.pyx Loading commit data...
_errors.pxd Loading commit data...
_errors.pyx Loading commit data...
_errors_test.pyx Loading commit data...
_fmt.pxd Loading commit data...
_fmt.pyx Loading commit data...
_fmt_test.pyx Loading commit data...
_golang.pxd Loading commit data...
_golang.pyx Loading commit data...
_golang_test.pyx Loading commit data...
_gopath.py Loading commit data...
_gopath_test.py Loading commit data...
_io.pxd Loading commit data...
_io.pyx Loading commit data...
_strings_test.pyx Loading commit data...
_sync.pxd Loading commit data...
_sync.pyx Loading commit data...
_sync_test.pyx Loading commit data...
_testing.h Loading commit data...
_time.pxd Loading commit data...
_time.pyx Loading commit data...
context.cpp Loading commit data...
context.h Loading commit data...
context.pxd Loading commit data...
context.py Loading commit data...
context_test.py Loading commit data...
cxx.h Loading commit data...
cxx.pxd Loading commit data...
cxx_test.cpp Loading commit data...
cxx_test.py Loading commit data...
errors.cpp Loading commit data...
errors.h Loading commit data...
errors.pxd Loading commit data...
errors.py Loading commit data...
errors_test.cpp Loading commit data...
errors_test.py Loading commit data...
fmt.cpp Loading commit data...
fmt.h Loading commit data...
fmt.pxd Loading commit data...
fmt.py Loading commit data...
fmt_test.cpp Loading commit data...
fmt_test.py Loading commit data...
gcompat.py Loading commit data...
golang_test.py Loading commit data...
io.cpp Loading commit data...
io.h Loading commit data...
io.pxd Loading commit data...
io.py Loading commit data...
io_test.py Loading commit data...
libgolang.h Loading commit data...
strconv.py Loading commit data...
strconv_test.py Loading commit data...
strings.cpp Loading commit data...
strings.h Loading commit data...
strings.pxd Loading commit data...
strings_test.cpp Loading commit data...
strings_test.py Loading commit data...
sync.cpp Loading commit data...
sync.h Loading commit data...
sync.pxd Loading commit data...
sync.py Loading commit data...
sync_test.cpp Loading commit data...
sync_test.py Loading commit data...
testing.py Loading commit data...
time.cpp Loading commit data...
time.h Loading commit data...
time.pxd Loading commit data...
time.py Loading commit data...
time_test.py Loading commit data...