Commit 510cf8d1 authored by Kirill Smelkov's avatar Kirill Smelkov

golang_str: Fix bstr.tp_print(flags=print_repr)

On py2 objects are printed via their .tp_repr slot with flags=0
(contrary to Py_PRINT_RAW which requests to print str -
https://docs.python.org/2.7/c-api/object.html#c.PyObject_Print)

We were not handling repr'ing inside our tp_print implementation, and
as the result e.g. b('мир') was printed on interactive console as
'\xd0\xbc\xd0\xb8\xd1\x80' instead of b('мир').

Fix it.
parent 386844d3
......@@ -542,11 +542,22 @@ IF PY2:
ctypedef int (*printfunc)(PyObject *, FILE *, int) except -1
ctypedef struct _PyTypeObject_Print "PyTypeObject":
printfunc tp_print
int Py_PRINT_RAW
cdef int _pybstr_tp_print(PyObject *obj, FILE *f, int flags) except -1:
o = <object>obj
if flags & Py_PRINT_RAW:
# emit str of the object instead of repr
# https://docs.python.org/2.7/c-api/object.html#c.PyObject_Print
pass
else:
# emit repr
o = repr(o)
cdef int _pybstr_tp_print(PyObject *obj, FILE *f, int nesting) except -1:
o = <bytes>obj
assert isinstance(o, bytes)
o = <bytes>o
o = bytes(buffer(o)) # change tp_type to bytes instead of pybstr
return (<_PyTypeObject_Print*>Py_TYPE(o)) .tp_print(<PyObject*>o, f, nesting)
return (<_PyTypeObject_Print*>Py_TYPE(o)) .tp_print(<PyObject*>o, f, Py_PRINT_RAW)
(<_PyTypeObject_Print*>Py_TYPE(pybstr())) .tp_print = _pybstr_tp_print
......
......@@ -38,6 +38,11 @@ def main():
print("print(repr(b)):", repr(sb))
print("print(repr(u)):", repr(su))
# py2: print(dict) calls PyObject_Print(flags=0) for both keys and values,
# not with flags=Py_PRINT_RAW used by default almost everywhere else.
# this way we can verify whether bstr.tp_print handles flags correctly.
print("print({b: u}):", {sb: su})
if __name__ == '__main__':
main()
......@@ -4,3 +4,4 @@ print(qq(b)): "привет b"
print(qq(u)): "привет u"
print(repr(b)): b('привет b')
print(repr(u)): u('привет u')
print({b: u}): {b('привет b'): u('привет u')}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment