You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
More correct string truncating in PyUnicode_FromFormat() #70278
The C code often uses %.<number><format> in PyUnicode_FromFormat(). %.200s protects from unlimited output when broken pointer points on random non-null-terminated data. %.200R is used to limit the size of human-readable messages.
In all these case formatted string can look well-formed with short data, but mis-formed (not closed quote, truncated backslash escaping or � decoded from truncated UTF-8 sequence) with long data.
I propose to make truncating in PyUnicode_FromFormat() more smart.
Truncated %R should keep at least one end character (the quote or ">").
Truncated output should include "..." or "[...]" as truncating sign.
\c, \OOO, \xXX, \uXXXX, and \UXXXXXXXX should not be truncated. It is better to omit these sequences at all (cut the string before them) that output them truncated.
Doesn't truncate UTF-8 sequence inside a character for %s.
I think we can make this feature available with classic formatting '%.100r', but with new formatting '{0:.100!r}' (especially with f-strings) this can be not so easy.
Activity
serhiy-storchaka commentedon Jan 12, 2016
The C code often uses %.<number><format> in PyUnicode_FromFormat(). %.200s protects from unlimited output when broken pointer points on random non-null-terminated data. %.200R is used to limit the size of human-readable messages.
In all these case formatted string can look well-formed with short data, but mis-formed (not closed quote, truncated backslash escaping or � decoded from truncated UTF-8 sequence) with long data.
I propose to make truncating in PyUnicode_FromFormat() more smart.
vstinner commentedon Jan 12, 2016
See my old issue bpo-10833 which proposed to *remove* the arbitrary limit
on strings. It was rejected.
gvanrossum commentedon Jan 12, 2016
Could we make this feature available at the Python level too? It sounds
really useful.
--Guido (mobile)
On Jan 12, 2016 2:01 AM, "STINNER Victor" <report@bugs.python.org> wrote:
serhiy-storchaka commentedon Jan 12, 2016
I think we can make this feature available with classic formatting '%.100r', but with new formatting '{0:.100!r}' (especially with f-strings) this can be not so easy.
gvanrossum commentedon Jan 12, 2016
Well it seems a little odd to spend effort on a corner case of the C-level
error messages if we can't even replicate it in pure Python.
pythongh-70278: Fix PyUnicode_FromFormat() with precision for %s and %V
serhiy-storchaka commentedon Jun 11, 2024
#120365 solves item 4. It is the simplest of the mentioned problems. It only exists at the C level.
vstinner commentedon Jun 12, 2024
I suggest adding
(...)to indicate that the string is truncated.gh-70278: Fix PyUnicode_FromFormat() with precision for %s and %V (GH…
pythongh-70278: Fix PyUnicode_FromFormat() with precision for %s and …
pythongh-70278: Fix PyUnicode_FromFormat() with precision for %s and …
pythongh-70278: Fix PyUnicode_FromFormat() with precision for %s and …