本文整理匯總了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)