From 1a547d608a6422b4677e8b690bcfd8467b97c219 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 27 Jan 2022 13:30:39 +0300 Subject: [PATCH 1/2] bpo-46564: do not leak `x` and `uspace` in `textwrap` --- Lib/textwrap.py | 1 + .../next/Library/2022-01-27-13-30-02.bpo-46544.oFDVWj.rst | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-01-27-13-30-02.bpo-46544.oFDVWj.rst diff --git a/Lib/textwrap.py b/Lib/textwrap.py index 841de9baecf5d8..bd1af9e1d9d75a 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -67,6 +67,7 @@ class TextWrapper: uspace = ord(' ') for x in _whitespace: unicode_whitespace_trans[ord(x)] = uspace + del x, uspace # This funky little regex is just the trick for splitting # text up into word-wrappable chunks. E.g. diff --git a/Misc/NEWS.d/next/Library/2022-01-27-13-30-02.bpo-46544.oFDVWj.rst b/Misc/NEWS.d/next/Library/2022-01-27-13-30-02.bpo-46544.oFDVWj.rst new file mode 100644 index 00000000000000..63b47e55f1b180 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-27-13-30-02.bpo-46544.oFDVWj.rst @@ -0,0 +1,2 @@ +Don't leak ``x`` & ``uspace`` intermediate vars in +:class:`textwrap.TextWrapper`. From 4940bc97f362a57934dfd7fe09885543f91c0737 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 27 Jan 2022 14:16:07 +0300 Subject: [PATCH 2/2] Address review --- Lib/textwrap.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Lib/textwrap.py b/Lib/textwrap.py index bd1af9e1d9d75a..98bedd27ea3a11 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -63,11 +63,7 @@ class TextWrapper: Append to the last line of truncated text. """ - unicode_whitespace_trans = {} - uspace = ord(' ') - for x in _whitespace: - unicode_whitespace_trans[ord(x)] = uspace - del x, uspace + unicode_whitespace_trans = dict.fromkeys(map(ord, _whitespace), ord(' ')) # This funky little regex is just the trick for splitting # text up into word-wrappable chunks. E.g.