Skip to content

Commit

Permalink
bpo-41919: Avoid resource leak in test_io (GH-22973)
Browse files Browse the repository at this point in the history
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
  • Loading branch information
shihai1991 and pablogsal authored Oct 25, 2020
1 parent df8d4c8 commit 14cdc21
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2519,15 +2519,17 @@ 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: 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')
return codecs.CodecInfo(
name='test_decoder', encode=latin1.encode, decode=None,
incrementalencoder=None,
streamreader=None, streamwriter=None,
incrementaldecoder=StatefulIncrementalDecoder)


class StatefulIncrementalDecoderTest(unittest.TestCase):
Expand Down Expand Up @@ -2579,9 +2581,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)
Expand Down

0 comments on commit 14cdc21

Please sign in to comment.