Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e553e2

Browse files
committed
Avoid triggering the refleak detector
1 parent 4b9b936 commit 4e553e2

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

Lib/test/test_codecs.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,15 @@ def test_bad_encoding_output_type(self):
23862386
# currently *only* want this to happen for relatively stateless
23872387
# exceptions, where the only significant information they contain is their
23882388
# type and a single str argument.
2389+
2390+
# Use a local codec registry to avoid appearing to leak objects when
2391+
# registering multiple seach functions
2392+
_TEST_CODECS = {}
2393+
2394+
def _get_test_codec(codec_name):
2395+
return _TEST_CODECS.get(codec_name)
2396+
codecs.register(_get_test_codec) # Returns None, not usable as a decorator
2397+
23892398
class ExceptionChainingTest(unittest.TestCase):
23902399

23912400
def setUp(self):
@@ -2395,19 +2404,16 @@ def setUp(self):
23952404
# The codecs module normalizes codec names, although this doesn't
23962405
# appear to be formally documented...
23972406
self.codec_name = repr(self).lower().replace(" ", "-")
2398-
self.codec_info = None
2399-
codecs.register(self.get_codec)
24002407

2401-
def get_codec(self, codec_name):
2402-
if codec_name != self.codec_name:
2403-
return None
2404-
return self.codec_info
2408+
def tearDown(self):
2409+
_TEST_CODECS.pop(self.codec_name, None)
24052410

24062411
def set_codec(self, obj_to_raise):
24072412
def raise_obj(*args, **kwds):
24082413
raise obj_to_raise
2409-
self.codec_info = codecs.CodecInfo(raise_obj, raise_obj,
2410-
name=self.codec_name)
2414+
codec_info = codecs.CodecInfo(raise_obj, raise_obj,
2415+
name=self.codec_name)
2416+
_TEST_CODECS[self.codec_name] = codec_info
24112417

24122418
@contextlib.contextmanager
24132419
def assertWrapped(self, operation, exc_type, msg):

0 commit comments

Comments
 (0)