[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

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 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.

--
Added file: http://bugs.python.org/file43702/_Py_bytes_repr.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 preallocated buffer.

--
versions:  -Python 3.5

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 bytes_repr's logic which calculates the actual output 
length.

--
components: Interpreter Core
files: bytearray_repr.patch
keywords: patch
messages: 270231
nosy: serhiy.storchaka, xiang.zhang
priority: normal
severity: normal
status: open
title: Enhance bytearray_repr with bytes_repr's logic
type: enhancement
versions: Python 3.5, Python 3.6
Added file: http://bugs.python.org/file43697/bytearray_repr.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com