Skip to content

Enhance bytearray_repr with bytes_repr's logic #71679

Closed

Description

@zhangyangyu
BPO 27492
Nosy @serhiy-storchaka, @zhangyangyu
Files
  • bytearray_repr.patch
  • _Py_bytes_repr.patch
  • bytearray_repr_v2.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2016-07-12.07:09:41.672>
    labels = ['interpreter-core', 'type-feature']
    title = "Enhance bytearray_repr with bytes_repr's logic"
    updated_at = <Date 2016-08-15.16:28:03.851>
    user = 'https://github.com/zhangyangyu'

    bugs.python.org fields:

    activity = <Date 2016-08-15.16:28:03.851>
    actor = 'xiang.zhang'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2016-07-12.07:09:41.672>
    creator = 'xiang.zhang'
    dependencies = []
    files = ['43697', '43702', '44119']
    hgrepos = []
    issue_num = 27492
    keywords = ['patch']
    message_count = 6.0
    messages = ['270231', '270241', '270249', '270298', '272747', '272778']
    nosy_count = 2.0
    nosy_names = ['serhiy.storchaka', 'xiang.zhang']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue27492'
    versions = ['Python 3.6']

    Linked PRs

    Activity

    1. zhangyangyu commented on Jul 12, 2016

      @zhangyangyu
      MemberAuthor

      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.

    2. serhiy-storchaka commented on Jul 12, 2016

      @serhiy-storchaka
      Member

      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.

    3. zhangyangyu commented on Jul 12, 2016

      @zhangyangyu
      MemberAuthor

      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.

    4. zhangyangyu commented on Jul 13, 2016

      @zhangyangyu
      MemberAuthor

      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.

    5. serhiy-storchaka commented on Aug 15, 2016

      @serhiy-storchaka
      Member

      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.

    6. zhangyangyu commented on Aug 15, 2016

      @zhangyangyu
      MemberAuthor

      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.

    7. transferred this issue fromon Apr 10, 2022
    8. added
      triagedThe issue has been accepted as valid by a triager.
      3.15bugs and security fixes
      on Aug 19, 2025
    9. added 4 commits that reference this issue on Aug 27, 2025
    10. added 2 commits that reference this issue on Aug 27, 2025
    11. added a commit that references this issue on Aug 27, 2025
    12. added a commit that references this issue on Aug 27, 2025
    13. added a commit that references this issue on Sep 17, 2025
    14. added a commit that references this issue on Oct 8, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)triagedThe issue has been accepted as valid by a triager.type-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions