From 0ece9871247625ed3541b66529ca654039a5d8b5 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Wed, 30 Aug 2023 19:26:11 +0100 Subject: [PATCH] Fix interoperability of rfc822_escape with stblib's email library --- distutils/util.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/distutils/util.py b/distutils/util.py index 7ef47176..4f94e587 100644 --- a/distutils/util.py +++ b/distutils/util.py @@ -508,6 +508,12 @@ def rfc822_escape(header): """Return a version of the string escaped for inclusion in an RFC-822 header, by ensuring there are 8 spaces space after each newline. """ - lines = header.split('\n') - sep = '\n' + 8 * ' ' - return sep.join(lines) + indent = 8 * " " + lines = header.splitlines(keepends=True) + + # Emulate the behaviour of `str.split` + # (the terminal line break in `splitlines` does not result in an extra line): + ends_in_newline = lines and lines[-1].splitlines()[0] != lines[-1] + suffix = indent if ends_in_newline else "" + + return indent.join(lines) + suffix