Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2330,6 +2330,8 @@ expression support in the :mod:`re` module).
Number, Punctuation, or Symbol (L, M, N, P, or S); plus the ASCII space 0x20.
Nonprintable characters are those in group Separator or Other (Z or C),
except the ASCII space.
Additionally, strong right-to-left characters which have a bidirectional
class R or AL considered non-printable.

For example:

Expand All @@ -2339,6 +2341,11 @@ expression support in the :mod:`re` module).
(True, True)
>>> '\t'.isprintable(), '\n'.isprintable()
(False, False)
>>> '\u05be'.isprintable(), '\u0608'.isprintable()
(False, False)

.. versionchanged:: next
Strong right-to-left characters considered non-printable.


.. method:: str.isspace()
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,14 @@ Other language changes
controlled by :ref:`environment variables <using-on-controlling-color>`.
(Contributed by Peter Bierma in :gh:`134170`.)

* The :meth:`~object.__repr__` of :class:`str` now hex-escapes strong
right-to-left characters (bidirectional class R and AL).
Previously, non-escaped right-to-left characters caused misleading
output on Unicode-aware terminals and browsers.
Correspondingly, :meth:`str.isprintable` now returns ``False`` for such
characters.
(Contributed by Serhiy Storchaka in :gh:`89268`.)

* The :meth:`~object.__repr__` of :class:`ImportError` and :class:`ModuleNotFoundError`
now shows "name" and "path" as ``name=<name>`` and ``path=<path>`` if they were given
as keyword arguments at construction time.
Expand Down
16 changes: 15 additions & 1 deletion Lib/test/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ def test_repr(self):
self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096),
repr("\U00010000" * 39 + "\uffff" * 4096))

# strong right-to-left character
self.assertEqual(repr('12\u05be34'), r"'12\u05be34'")
self.assertEqual(repr('12\u060834'), r"'12\u060834'")
self.assertEqual(repr('12\U0001080034'), r"'12\U0001080034'")
self.assertEqual(repr('12\U00010d0034'), r"'12\U00010d0034'")

self.assertTypedEqual(repr('\U0001f40d'), "'\U0001f40d'")
self.assertTypedEqual(repr(StrSubclass('abc')), "'abc'")
self.assertTypedEqual(repr(WithRepr('<abc>')), '<abc>')
Expand Down Expand Up @@ -853,14 +859,22 @@ def test_isprintable(self):
self.assertTrue('\U0001F46F'.isprintable())
self.assertFalse('\U000E0020'.isprintable())

# strong right-to-left character
self.assertFalse("\u05be".isprintable())
self.assertFalse("\u0608".isprintable())
self.assertFalse("\U00010800".isprintable())
self.assertFalse("\U00010d00".isprintable())

@support.requires_resource('cpu')
def test_isprintable_invariant(self):
for codepoint in range(sys.maxunicode + 1):
char = chr(codepoint)
category = unicodedata.category(char)
bidirectional = unicodedata.bidirectional(char)
self.assertEqual(char.isprintable(),
category[0] not in ('C', 'Z')
(category[0] not in ('C', 'Z')
or char == ' ')
and bidirectional not in ('R', 'AL'))

def test_surrogates(self):
for s in ('a\uD800b\uDFFF', 'a\uDFFFb\uD800',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The :meth:`~object.__repr__` of :class:`str` now hex-escapes strong
right-to-left characters (bidirectional class R and AL). Previously,
non-escaped right-to-left characters caused misleading output on
Unicode-aware terminals and browsers. Correspondingly,
:meth:`str.isprintable` now returns ``False`` for such characters.
Loading
Loading