[Python-checkins] bpo-35062: Fix parsing _io.IncrementalNewlineDecoder's *translate* argument. (GH-10217)

Miss Islington (bot) webhook-mailer at python.org
Wed Oct 31 08:36:25 EDT 2018


https://github.com/python/cpython/commit/71b6c1af727fbe13525fb734568057d78cea33f3
commit: 71b6c1af727fbe13525fb734568057d78cea33f3
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-10-31T05:36:20-07:00
summary:

bpo-35062: Fix parsing _io.IncrementalNewlineDecoder's *translate* argument. (GH-10217)


_io.IncrementalNewlineDecoder's initializer possibly assigns out-of-range
value to the bitwise struct field.
(cherry picked from commit b08746bfdf64e55ce33516f2065fa2aa4f51be95)

Co-authored-by: Xiang Zhang <angwerzx at 126.com>

files:
A Misc/NEWS.d/next/Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst
M Lib/test/test_io.py
M Modules/_io/textio.c

diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index c68b2fea858b..b41141e28adb 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -3748,6 +3748,16 @@ def _check(dec):
         dec = self.IncrementalNewlineDecoder(None, translate=True)
         _check(dec)
 
+    def test_translate(self):
+        # issue 35062
+        for translate in (-2, -1, 1, 2):
+            decoder = codecs.getincrementaldecoder("utf-8")()
+            decoder = self.IncrementalNewlineDecoder(decoder, translate)
+            self.check_newline_decoding_utf8(decoder)
+        decoder = codecs.getincrementaldecoder("utf-8")()
+        decoder = self.IncrementalNewlineDecoder(decoder, translate=0)
+        self.assertEqual(decoder.decode(b"\r\r\n"), "\r\r\n")
+
 class CIncrementalNewlineDecoderTest(IncrementalNewlineDecoderTest):
     pass
 
diff --git a/Misc/NEWS.d/next/Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst b/Misc/NEWS.d/next/Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst
new file mode 100644
index 000000000000..b77ed8685bfc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst
@@ -0,0 +1,2 @@
+Fix incorrect parsing of :class:`_io.IncrementalNewlineDecoder`'s
+*translate* argument.
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index d6b1e9437883..73d1b2e8a23b 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -261,7 +261,7 @@ _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self,
     }
     Py_INCREF(self->errors);
 
-    self->translate = translate;
+    self->translate = translate ? 1 : 0;
     self->seennl = 0;
     self->pendingcr = 0;
 



More information about the Python-checkins mailing list