From c8ef0c89acd098d3a21041ffe3cc7ff8cc70dbb9 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sun, 25 Oct 2020 23:46:26 +0800 Subject: [PATCH 1/2] lookupTestDecoder function Split from StatefulIncrementalDecoder to avoid resource leak. --- Lib/test/test_io.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index fbaea3aaec3cbe..bfbc5644ac0d8e 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2519,15 +2519,16 @@ def process_word(self): codecEnabled = False - @classmethod - def lookupTestDecoder(cls, name): - if cls.codecEnabled and name == 'test_decoder': - latin1 = codecs.lookup('latin-1') - return codecs.CodecInfo( - name='test_decoder', encode=latin1.encode, decode=None, - incrementalencoder=None, - streamreader=None, streamwriter=None, - incrementaldecoder=cls) + +# bpo-41919: Split from StatefulIncrementalDecoder to avoid resource leak. +def lookupTestDecoder(name): + if StatefulIncrementalDecoder.codecEnabled and name == 'test_decoder': + latin1 = codecs.lookup('latin-1') + return codecs.CodecInfo( + name='test_decoder', encode=latin1.encode, decode=None, + incrementalencoder=None, + streamreader=None, streamwriter=None, + incrementaldecoder=StatefulIncrementalDecoder) class StatefulIncrementalDecoderTest(unittest.TestCase): @@ -2579,9 +2580,8 @@ def setUp(self): self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n" self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii") os_helper.unlink(os_helper.TESTFN) - codecs.register(StatefulIncrementalDecoder.lookupTestDecoder) - self.addCleanup(codecs.unregister, - StatefulIncrementalDecoder.lookupTestDecoder) + codecs.register(lookupTestDecoder) + self.addCleanup(codecs.unregister, lookupTestDecoder) def tearDown(self): os_helper.unlink(os_helper.TESTFN) From c34805805737b6bc6647ffc9d47b1c7c12430a83 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Sun, 25 Oct 2020 18:21:13 +0000 Subject: [PATCH 2/2] Update Lib/test/test_io.py --- Lib/test/test_io.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index bfbc5644ac0d8e..cc54d0ea0062f2 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2520,7 +2520,8 @@ def process_word(self): codecEnabled = False -# bpo-41919: Split from StatefulIncrementalDecoder to avoid resource leak. +# bpo-41919: This method is separated from StatefulIncrementalDecoder to avoid a resource leak +# when registering codecs and cleanup functions. def lookupTestDecoder(name): if StatefulIncrementalDecoder.codecEnabled and name == 'test_decoder': latin1 = codecs.lookup('latin-1')