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

Skip to content

Commit 430f68b

Browse files
committed
Move registration of the codec search function to the module scope
so it is only executed once. Otherwise the same search function is repeated added to the codec search path when regrtest is run with -R and leaks are reported.
1 parent a98e769 commit 430f68b

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

Lib/test/test_unicode.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99
import unittest, sys, string, codecs, new
1010
from test import test_support, string_tests
1111

12+
# Error handling (bad decoder return)
13+
def search_function(encoding):
14+
def decode1(input, errors="strict"):
15+
return 42 # not a tuple
16+
def encode1(input, errors="strict"):
17+
return 42 # not a tuple
18+
def encode2(input, errors="strict"):
19+
return (42, 42) # no unicode
20+
def decode2(input, errors="strict"):
21+
return (42, 42) # no unicode
22+
if encoding=="test.unicode1":
23+
return (encode1, decode1, None, None)
24+
elif encoding=="test.unicode2":
25+
return (encode2, decode2, None, None)
26+
else:
27+
return None
28+
codecs.register(search_function)
29+
1230
class UnicodeTest(
1331
string_tests.CommonTest,
1432
string_tests.MixinStrUnicodeUserStringTest,
@@ -567,23 +585,6 @@ def test_codecs_errors(self):
567585
# Error handling (truncated escape sequence)
568586
self.assertRaises(UnicodeError, "\\".decode, "unicode-escape")
569587

570-
# Error handling (bad decoder return)
571-
def search_function(encoding):
572-
def decode1(input, errors="strict"):
573-
return 42 # not a tuple
574-
def encode1(input, errors="strict"):
575-
return 42 # not a tuple
576-
def encode2(input, errors="strict"):
577-
return (42, 42) # no unicode
578-
def decode2(input, errors="strict"):
579-
return (42, 42) # no unicode
580-
if encoding=="test.unicode1":
581-
return (encode1, decode1, None, None)
582-
elif encoding=="test.unicode2":
583-
return (encode2, decode2, None, None)
584-
else:
585-
return None
586-
codecs.register(search_function)
587588
self.assertRaises(TypeError, "hello".decode, "test.unicode1")
588589
self.assertRaises(TypeError, unicode, "hello", "test.unicode2")
589590
self.assertRaises(TypeError, u"hello".encode, "test.unicode1")

0 commit comments

Comments
 (0)