Skip to content

Commit 14cdc21

Browse files
bpo-41919: Avoid resource leak in test_io (GH-22973)
Co-authored-by: Pablo Galindo <[email protected]>
1 parent df8d4c8 commit 14cdc21

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Lib/test/test_io.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -2519,15 +2519,17 @@ def process_word(self):
25192519

25202520
codecEnabled = False
25212521

2522-
@classmethod
2523-
def lookupTestDecoder(cls, name):
2524-
if cls.codecEnabled and name == 'test_decoder':
2525-
latin1 = codecs.lookup('latin-1')
2526-
return codecs.CodecInfo(
2527-
name='test_decoder', encode=latin1.encode, decode=None,
2528-
incrementalencoder=None,
2529-
streamreader=None, streamwriter=None,
2530-
incrementaldecoder=cls)
2522+
2523+
# bpo-41919: This method is separated from StatefulIncrementalDecoder to avoid a resource leak
2524+
# when registering codecs and cleanup functions.
2525+
def lookupTestDecoder(name):
2526+
if StatefulIncrementalDecoder.codecEnabled and name == 'test_decoder':
2527+
latin1 = codecs.lookup('latin-1')
2528+
return codecs.CodecInfo(
2529+
name='test_decoder', encode=latin1.encode, decode=None,
2530+
incrementalencoder=None,
2531+
streamreader=None, streamwriter=None,
2532+
incrementaldecoder=StatefulIncrementalDecoder)
25312533

25322534

25332535
class StatefulIncrementalDecoderTest(unittest.TestCase):
@@ -2579,9 +2581,8 @@ def setUp(self):
25792581
self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n"
25802582
self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii")
25812583
os_helper.unlink(os_helper.TESTFN)
2582-
codecs.register(StatefulIncrementalDecoder.lookupTestDecoder)
2583-
self.addCleanup(codecs.unregister,
2584-
StatefulIncrementalDecoder.lookupTestDecoder)
2584+
codecs.register(lookupTestDecoder)
2585+
self.addCleanup(codecs.unregister, lookupTestDecoder)
25852586

25862587
def tearDown(self):
25872588
os_helper.unlink(os_helper.TESTFN)

0 commit comments

Comments
 (0)