本文整理汇总了Python中codecs.ignore_errors方法的典型用法代码示例。如果您正苦于以下问题:Python codecs.ignore_errors方法的具体用法?Python codecs.ignore_errors怎么用?Python codecs.ignore_errors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类codecs
的用法示例。
在下文中一共展示了codecs.ignore_errors方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_fake_error_class
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_fake_error_class(self):
handlers = [
codecs.strict_errors,
codecs.ignore_errors,
codecs.replace_errors,
codecs.backslashreplace_errors,
codecs.xmlcharrefreplace_errors,
]
for cls in UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError:
class FakeUnicodeError(str):
__class__ = cls
for handler in handlers:
self.assertRaises(TypeError, handler, FakeUnicodeError())
class FakeUnicodeError(Exception):
__class__ = cls
for handler in handlers:
with self.assertRaises((TypeError, FakeUnicodeError)):
handler(FakeUnicodeError())
示例2: test_lookup
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_lookup(self):
self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
self.assertEqual(codecs.ignore_errors, codecs.lookup_error("ignore"))
self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
self.assertEqual(
codecs.xmlcharrefreplace_errors,
codecs.lookup_error("xmlcharrefreplace")
)
self.assertEqual(
codecs.backslashreplace_errors,
codecs.lookup_error("backslashreplace")
)
self.assertEqual(
codecs.namereplace_errors,
codecs.lookup_error("namereplace")
)
示例3: test_fake_error_class
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_fake_error_class(self):
handlers = [
codecs.strict_errors,
codecs.ignore_errors,
codecs.replace_errors,
codecs.backslashreplace_errors,
codecs.namereplace_errors,
codecs.xmlcharrefreplace_errors,
codecs.lookup_error('surrogateescape'),
codecs.lookup_error('surrogatepass'),
]
for cls in UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError:
class FakeUnicodeError(str):
__class__ = cls
for handler in handlers:
with self.subTest(handler=handler, error_class=cls):
self.assertRaises(TypeError, handler, FakeUnicodeError())
class FakeUnicodeError(Exception):
__class__ = cls
for handler in handlers:
with self.subTest(handler=handler, error_class=cls):
with self.assertRaises((TypeError, FakeUnicodeError)):
handler(FakeUnicodeError())
示例4: test_fake_error_class
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_fake_error_class(self):
handlers = [
codecs.strict_errors,
codecs.ignore_errors,
codecs.replace_errors,
codecs.backslashreplace_errors,
codecs.xmlcharrefreplace_errors,
codecs.lookup_error('surrogateescape'),
codecs.lookup_error('surrogatepass'),
]
for cls in UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError:
class FakeUnicodeError(str):
__class__ = cls
for handler in handlers:
with self.subTest(handler=handler, error_class=cls):
self.assertRaises(TypeError, handler, FakeUnicodeError())
class FakeUnicodeError(Exception):
__class__ = cls
for handler in handlers:
with self.subTest(handler=handler, error_class=cls):
with self.assertRaises((TypeError, FakeUnicodeError)):
handler(FakeUnicodeError())
示例5: test_badandgoodignoreexceptions
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_badandgoodignoreexceptions(self):
# "ignore" complains about a non-exception passed in
self.assertRaises(
TypeError,
codecs.ignore_errors,
42
)
# "ignore" complains about the wrong exception type
self.assertRaises(
TypeError,
codecs.ignore_errors,
UnicodeError("ouch")
)
# If the correct exception is passed in, "ignore" returns an empty replacement
self.assertEqual(
codecs.ignore_errors(
UnicodeEncodeError("ascii", u"a\u3042b", 1, 2, "ouch")),
(u"", 2)
)
self.assertEqual(
codecs.ignore_errors(
UnicodeDecodeError("ascii", "a\xffb", 1, 2, "ouch")),
(u"", 2)
)
self.assertEqual(
codecs.ignore_errors(
UnicodeTranslateError(u"a\u3042b", 1, 2, "ouch")),
(u"", 2)
)
示例6: test_lookup
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_lookup(self):
self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
self.assertEqual(codecs.ignore_errors, codecs.lookup_error("ignore"))
self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
self.assertEqual(
codecs.xmlcharrefreplace_errors,
codecs.lookup_error("xmlcharrefreplace")
)
self.assertEqual(
codecs.backslashreplace_errors,
codecs.lookup_error("backslashreplace")
)
示例7: test_errorcallback_custom_ignore
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_errorcallback_custom_ignore(self):
# Issue #23215: MemoryError with custom error handlers and multibyte codecs
data = 100 * unichr(0xdc00)
codecs.register_error("test.ignore", codecs.ignore_errors)
for enc in ALL_CJKENCODINGS:
self.assertEqual(data.encode(enc, "test.ignore"), b'')
示例8: test_decode_callback
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_decode_callback(self):
if sys.maxunicode > 0xffff:
codecs.register_error("UnicodeInternalTest", codecs.ignore_errors)
decoder = codecs.getdecoder("unicode_internal")
ab = u"ab".encode("unicode_internal")
ignored = decoder("%s\x22\x22\x22\x22%s" % (ab[:4], ab[4:]),
"UnicodeInternalTest")
self.assertEqual((u"ab", 12), ignored)
示例9: test_badandgoodignoreexceptions
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_badandgoodignoreexceptions(self):
# "ignore" complains about a non-exception passed in
self.assertRaises(
TypeError,
codecs.ignore_errors,
42
)
# "ignore" complains about the wrong exception type
self.assertRaises(
TypeError,
codecs.ignore_errors,
UnicodeError("ouch")
)
# If the correct exception is passed in, "ignore" returns an empty replacement
self.assertEqual(
codecs.ignore_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
(u"", 1)
)
self.assertEqual(
codecs.ignore_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")),
(u"", 1)
)
self.assertEqual(
codecs.ignore_errors(UnicodeTranslateError(u"\u3042", 0, 1, "ouch")),
(u"", 1)
)
示例10: create_fb_format
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def create_fb_format(lines_file, convo_file, outpath):
print('[building fbformat]')
with open(os.path.join(outpath, 'train.txt'), 'w') as ftrain, open(
os.path.join(outpath, 'valid.txt'), 'w'
) as fvalid, open(os.path.join(outpath, 'test.txt'), 'w') as ftest:
lines = {}
codecs.register_error('strict', codecs.ignore_errors)
with codecs.open(lines_file, 'r') as f:
for line in f:
l = line.split(' +++$+++ ')
lines[l[0]] = ' '.join(l[4:]).strip('\n').replace('\t', ' ')
cnt = 0
with codecs.open(convo_file, 'r') as f:
for line in f:
l = line.split(' ')
convo = ' '.join(l[6:]).strip('\n').strip('[').strip(']')
c = convo.replace("'", '').replace(' ', '').split(',')
# forward conversation
s = ''
index = 0
for i in range(0, len(c), 2):
index += 1
s += str(index) + ' ' + lines[c[i]]
if len(c) > i + 1:
s += '\t' + lines[c[i + 1]]
s += '\n'
cnt = cnt + 1
handle = ftrain
if (cnt % 10) == 0:
handle = ftest
if (cnt % 10) == 1:
handle = fvalid
handle.write(s + '\n')
示例11: test_badandgoodignoreexceptions
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_badandgoodignoreexceptions(self):
# "ignore" complains about a non-exception passed in
self.assertRaises(
TypeError,
codecs.ignore_errors,
42
)
# "ignore" complains about the wrong exception type
self.assertRaises(
TypeError,
codecs.ignore_errors,
UnicodeError("ouch")
)
# If the correct exception is passed in, "ignore" returns an empty replacement
self.assertEqual(
codecs.ignore_errors(
UnicodeEncodeError("ascii", "a\u3042b", 1, 2, "ouch")),
("", 2)
)
self.assertEqual(
codecs.ignore_errors(
UnicodeDecodeError("ascii", bytearray(b"a\xffb"), 1, 2, "ouch")),
("", 2)
)
self.assertEqual(
codecs.ignore_errors(
UnicodeTranslateError("a\u3042b", 1, 2, "ouch")),
("", 2)
)
示例12: test_errorcallback_custom_ignore
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_errorcallback_custom_ignore(self):
# Issue #23215: MemoryError with custom error handlers and multibyte codecs
data = 100 * "\udc00"
codecs.register_error("test.ignore", codecs.ignore_errors)
for enc in ALL_CJKENCODINGS:
self.assertEqual(data.encode(enc, "test.ignore"), b'')
示例13: test_decode_callback
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import ignore_errors [as 别名]
def test_decode_callback(self):
codecs.register_error("UnicodeInternalTest", codecs.ignore_errors)
decoder = codecs.getdecoder("unicode_internal")
with support.check_warnings(('unicode_internal codec has been '
'deprecated', DeprecationWarning)):
ab = "ab".encode("unicode_internal").decode()
ignored = decoder(bytes("%s\x22\x22\x22\x22%s" % (ab[:4], ab[4:]),
"ascii"),
"UnicodeInternalTest")
self.assertEqual(("ab", 12), ignored)