This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Enhance bytearray_repr with bytes_repr's logic
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: serhiy.storchaka, xiang.zhang
Priority: normal Keywords: patch

Created on 2016-07-12 07:09 by xiang.zhang, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
bytearray_repr.patch xiang.zhang, 2016-07-12 07:09 review
_Py_bytes_repr.patch xiang.zhang, 2016-07-13 06:44 review
bytearray_repr_v2.patch xiang.zhang, 2016-08-15 16:28 review
Messages (6)
msg270231 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-07-12 07:09
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.
msg270241 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-07-12 12:59
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.
msg270249 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-07-12 15:25
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.
msg270298 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-07-13 06:44
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.
msg272747 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-15 10:44
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

    def __repr__(self):
        return 'bytearray(%r)' % bytes(self)

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.
msg272778 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-08-15 16:28
> It can be more efficient is make bytes.__repr__ accepting not only bytes, but objects supporting the buffer protocol.

I like this idea. v2 implements this.
History
Date User Action Args
2022-04-11 14:58:33adminsetgithub: 71679
2016-08-15 16:28:03xiang.zhangsetfiles: + bytearray_repr_v2.patch

messages: + msg272778
2016-08-15 10:44:51serhiy.storchakasetmessages: + msg272747
2016-07-13 06:44:35xiang.zhangsetfiles: + _Py_bytes_repr.patch

messages: + msg270298
2016-07-12 15:25:57xiang.zhangsetmessages: + msg270249
2016-07-12 12:59:00serhiy.storchakasetmessages: + msg270241
versions: - Python 3.5
2016-07-12 07:09:41xiang.zhangcreate