gh-124665: Add _PyCodec_UnregisterError and _codecs._unregister_error (#124677)

This commit is contained in:
Bénédikt Tran 2024-09-29 02:25:23 +02:00 committed by GitHub
parent 04c837d9d8
commit c00964ecd5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 136 additions and 2 deletions

View file

@ -1,3 +1,4 @@
from _codecs import _unregister_error as _codecs_unregister_error
import codecs
import html.entities
import itertools
@ -1210,7 +1211,6 @@ class CodecCallbackTest(unittest.TestCase):
'\ufffd\x00\x00'
)
def test_fake_error_class(self):
handlers = [
codecs.strict_errors,
@ -1235,6 +1235,31 @@ class CodecCallbackTest(unittest.TestCase):
with self.assertRaises((TypeError, FakeUnicodeError)):
handler(FakeUnicodeError())
def test_reject_unregister_builtin_error_handler(self):
for name in [
'strict', 'ignore', 'replace', 'backslashreplace', 'namereplace',
'xmlcharrefreplace', 'surrogateescape', 'surrogatepass',
]:
with self.subTest(name):
self.assertRaises(ValueError, _codecs_unregister_error, name)
def test_unregister_custom_error_handler(self):
def custom_handler(exc):
raise exc
custom_name = 'test.test_unregister_custom_error_handler'
self.assertRaises(LookupError, codecs.lookup_error, custom_name)
codecs.register_error(custom_name, custom_handler)
self.assertIs(codecs.lookup_error(custom_name), custom_handler)
self.assertTrue(_codecs_unregister_error(custom_name))
self.assertRaises(LookupError, codecs.lookup_error, custom_name)
def test_unregister_custom_unknown_error_handler(self):
unknown_name = 'test.test_unregister_custom_unknown_error_handler'
self.assertRaises(LookupError, codecs.lookup_error, unknown_name)
self.assertFalse(_codecs_unregister_error(unknown_name))
self.assertRaises(LookupError, codecs.lookup_error, unknown_name)
if __name__ == "__main__":
unittest.main()