[issue27492] Enhance bytearray_repr with bytes_repr's logic

2016-08-15 Thread Xiang Zhang
Xiang Zhang added the comment: > 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. -- Added file: http://bugs.python.org/file44119/bytearray_repr_v2.patch

[issue27492] Enhance bytearray_repr with bytes_repr's logic

2016-08-15 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: 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

[issue27492] Enhance bytearray_repr with bytes_repr's logic

2016-07-13 Thread Xiang Zhang
Xiang Zhang added the comment: 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

[issue27492] Enhance bytearray_repr with bytes_repr's logic

2016-07-12 Thread Xiang Zhang
Xiang Zhang added the comment: 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. -- ___ Python tracker

[issue27492] Enhance bytearray_repr with bytes_repr's logic

2016-07-12 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: 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

[issue27492] Enhance bytearray_repr with bytes_repr's logic

2016-07-12 Thread Xiang Zhang
New submission from Xiang Zhang: 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