Skip to content

reprlib.Repr truncation ignores the length of a custom fillvalue #161289

Open

Description

@andresdelfino

Bug report

Bug description:

Since Python 3.11, reprlib.Repr.fillvalue can be set to any string, but Repr.repr_str(), Repr.repr_int() and Repr.repr_instance() still compute how many characters to keep around it as if it were always three characters long (the length of the default '...'):

i = max(0, (self.maxstring-3)//2)
j = max(0, self.maxstring-3-i)
s = s[:i] + self.fillvalue + s[len(s)-j:]

As a result, when the fill value has a different length, a truncated representation no longer fits maxstring, maxlong or maxother:

>>> import reprlib
>>> r = reprlib.Repr(maxstring=10, maxlong=10, maxother=10, fillvalue='<snip>')
>>> r.repr(12345678901234567890)  # 13 characters, maxlong is 10
'123<snip>7890'
>>> r.repr('abcdefghijklmnopqrstuvwxyz')  # 13 characters, maxstring is 10
"'ab<snip>xyz'"
>>> r = reprlib.Repr(maxstring=10, fillvalue='…')
>>> r.repr('abcdefghijklmnopqrstuvwxyz')  # 8 characters, 2 left unused
"'ab…xyz'"

The documentation describes maxlong as the "Maximum number of characters in the representation for an integer". Using len(self.fillvalue) instead of 3 makes the result fit the limit, and leaves the output with the default '...' unchanged.

CPython versions tested on:

3.12, 3.15, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 4, 2026
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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions