]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-145831: email.quoprimime: `decode()` leaves stray `\r` when `eol='\r\n'` (#145832)
authorStefan Zetzsche <120379523+stefanzetzsche@users.noreply.github.com>
Thu, 9 Apr 2026 20:21:49 +0000 (21:21 +0100)
committerGitHub <noreply@github.com>
Thu, 9 Apr 2026 20:21:49 +0000 (16:21 -0400)
decoded[:-1] only strips one character, leaving a stray \r when eol
is two characters. Fix: decoded[:-len(eol)].

Lib/email/quoprimime.py
Lib/test/test_email/test_email.py
Misc/NEWS.d/next/Library/2026-03-11-15-09-52.gh-issue-145831._sW94w.rst [new file with mode: 0644]

index 27c7ea55c7871fd6e8ca43b723598579d207da07..bc53b3768213102eeb757ea771ac8a263e8db119 100644 (file)
@@ -272,7 +272,7 @@ def decode(encoded, eol=NL):
                 decoded += eol
     # Special case if original string did not end with eol
     if encoded[-1] not in '\r\n' and decoded.endswith(eol):
-        decoded = decoded[:-1]
+        decoded = decoded[:-len(eol)]
     return decoded
 
 
index 4e6c213510c74c3627deb542336f579bd37fd95c..7778566492d8f4479d0b9be2bb2d9ed2bb89568f 100644 (file)
@@ -4828,6 +4828,15 @@ class TestQuopri(unittest.TestCase):
     def test_decode_false_quoting(self):
         self._test_decode('A=1,B=A ==> A+B==2', 'A=1,B=A ==> A+B==2')
 
+    def test_decode_crlf_eol_no_trailing_newline(self):
+        self._test_decode('abc', 'abc', eol='\r\n')
+
+    def test_decode_crlf_eol_multiline_no_trailing_newline(self):
+        self._test_decode('a\r\nb', 'a\r\nb', eol='\r\n')
+
+    def test_decode_crlf_eol_with_trailing_newline(self):
+        self._test_decode('abc\r\n', 'abc\r\n', eol='\r\n')
+
     def _test_encode(self, body, expected_encoded_body, maxlinelen=None, eol=None):
         kwargs = {}
         if maxlinelen is None:
diff --git a/Misc/NEWS.d/next/Library/2026-03-11-15-09-52.gh-issue-145831._sW94w.rst b/Misc/NEWS.d/next/Library/2026-03-11-15-09-52.gh-issue-145831._sW94w.rst
new file mode 100644 (file)
index 0000000..454b62b
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :func:`!email.quoprimime.decode` leaving a stray ``\r`` when
+``eol='\r\n'`` by stripping the full *eol* string instead of one character.