You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enhance bytearray_repr with bytes_repr's logic #71679
Right now bytearray_repr's implementation mimics old string_repr, but it has a drawback: It arbitrarily checks overflow using four times the bytearray object's length, but the representation length of the value can range from 1 to 4. I think we can use bytes_repr's logic which calculates the actual output length.
Bytes and bytearray share a much of code. I think it is possible to share the implementation of reprs. Just add two functions in bytes_methods.c: one counts the size of the repr and determines the quote, and other writes the content of the repr in preallocated buffer.
I considered that too. But I was not sure what code could go into bytes_methods.c then, Python level methods, all common parts or only tp->methods? I'll try to factor the common part out later.
Hi, Serhiy. I've tried one version factoring the common parts out. I make them one function but it seems not very elegant. I also think about passing the object and use it to get the type and then determine how to get the string, what the pre/postfix are, and the exception message, but that seems not elegant either. And I don't figure out what the two function's advantage. Let me know your considerations.
BTW, I am still not sure repr could be put into bytes_methods.c. The functions in it are all tp->methods.
The two function's advantage is in the ability to reuse this code for other purposes. For example in _codecs.escape_encode(). But since this is the only place where the same algorithm is used and this functions is not documented and I presume it is not much used, this advantage is pretty small.
The simplest implementation of bytearray.__repr__ is
It is less efficient than the current implementation or proposed patch, but is much simpler. This approach is used in reprs of set, frozenset, deque, array, BaseException, itemgetter, attrgetter, etc.
It can be more efficient is make bytes.__repr__ accepting not only bytes, but objects supporting the buffer protocol.
Activity
zhangyangyu commentedon Jul 12, 2016
Right now bytearray_repr's implementation mimics old string_repr, but it has a drawback: It arbitrarily checks overflow using four times the bytearray object's length, but the representation length of the value can range from 1 to 4. I think we can use bytes_repr's logic which calculates the actual output length.
serhiy-storchaka commentedon Jul 12, 2016
Bytes and bytearray share a much of code. I think it is possible to share the implementation of reprs. Just add two functions in bytes_methods.c: one counts the size of the repr and determines the quote, and other writes the content of the repr in preallocated buffer.
zhangyangyu commentedon Jul 12, 2016
I considered that too. But I was not sure what code could go into bytes_methods.c then, Python level methods, all common parts or only tp->methods? I'll try to factor the common part out later.
zhangyangyu commentedon Jul 13, 2016
Hi, Serhiy. I've tried one version factoring the common parts out. I make them one function but it seems not very elegant. I also think about passing the object and use it to get the type and then determine how to get the string, what the pre/postfix are, and the exception message, but that seems not elegant either. And I don't figure out what the two function's advantage. Let me know your considerations.
BTW, I am still not sure repr could be put into bytes_methods.c. The functions in it are all tp->methods.
serhiy-storchaka commentedon Aug 15, 2016
The two function's advantage is in the ability to reuse this code for other purposes. For example in _codecs.escape_encode(). But since this is the only place where the same algorithm is used and this functions is not documented and I presume it is not much used, this advantage is pretty small.
The simplest implementation of bytearray.__repr__ is
It is less efficient than the current implementation or proposed patch, but is much simpler. This approach is used in reprs of set, frozenset, deque, array, BaseException, itemgetter, attrgetter, etc.
It can be more efficient is make bytes.__repr__ accepting not only bytes, but objects supporting the buffer protocol.
zhangyangyu commentedon Aug 15, 2016
I like this idea. v2 implements this.
pythongh-71679: Improve tests for repr() of bytes and bytearray
pythongh-71679: Improve tests for repr() of bytes and bytearray
pythongh-71679: Share the repr implementation between bytes and bytea…
gh-71679: Improve tests for repr() of bytes and bytearray (GH-138180)
pythongh-71679: Improve tests for repr() of bytes and bytearray (pyth…
pythongh-71679: Improve tests for repr() of bytes and bytearray (pyth…
pythongh-71679: Share the repr implementation between bytes and bytea…
bytearray()shouldn't convert'to\'for readability and consistency #137190 as a duplicate of this issue[3.13] gh-71679: Improve tests for repr() of bytes and bytearray (GH-…
gh-71679: Share the repr implementation between bytes and bytearray (G…
[3.14] gh-71679: Improve tests for repr() of bytes and bytearray (GH-…