本文整理汇总了Python中codecs.StreamRecoder方法的典型用法代码示例。如果您正苦于以下问题:Python codecs.StreamRecoder方法的具体用法?Python codecs.StreamRecoder怎么用?Python codecs.StreamRecoder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类codecs
的用法示例。
在下文中一共展示了codecs.StreamRecoder方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bad_stream_exception
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import StreamRecoder [as 别名]
def test_bad_stream_exception(all_parsers, csv_dir_path):
# see gh-13652
#
# This test validates that both the Python engine and C engine will
# raise UnicodeDecodeError instead of C engine raising ParserError
# and swallowing the exception that caused read to fail.
path = os.path.join(csv_dir_path, "sauron.SHIFT_JIS.csv")
codec = codecs.lookup("utf-8")
utf8 = codecs.lookup('utf-8')
parser = all_parsers
msg = ("'utf-8' codec can't decode byte" if compat.PY3
else "'utf8' codec can't decode byte")
# Stream must be binary UTF8.
with open(path, "rb") as handle, codecs.StreamRecoder(
handle, utf8.encode, utf8.decode, codec.streamreader,
codec.streamwriter) as stream:
with pytest.raises(UnicodeDecodeError, match=msg):
parser.read_csv(stream)
示例2: test_bad_stream_exception
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import StreamRecoder [as 别名]
def test_bad_stream_exception(self):
# Issue 13652:
# This test validates that both python engine
# and C engine will raise UnicodeDecodeError instead of
# c engine raising ParserError and swallowing exception
# that caused read to fail.
codec = codecs.lookup("utf-8")
utf8 = codecs.lookup('utf-8')
if compat.PY3:
msg = "'utf-8' codec can't decode byte"
else:
msg = "'utf8' codec can't decode byte"
# stream must be binary UTF8
with open(self.csv_shiftjs, "rb") as handle, codecs.StreamRecoder(
handle, utf8.encode, utf8.decode, codec.streamreader,
codec.streamwriter) as stream:
with tm.assert_raises_regex(UnicodeDecodeError, msg):
self.read_csv(stream)
示例3: test_bad_stream_exception
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import StreamRecoder [as 别名]
def test_bad_stream_exception(self):
# Issue 13652:
# This test validates that both python engine
# and C engine will raise UnicodeDecodeError instead of
# c engine raising ParserError and swallowing exception
# that caused read to fail.
handle = open(self.csv_shiftjs, "rb")
codec = codecs.lookup("utf-8")
utf8 = codecs.lookup('utf-8')
# stream must be binary UTF8
stream = codecs.StreamRecoder(
handle, utf8.encode, utf8.decode, codec.streamreader,
codec.streamwriter)
if compat.PY3:
msg = "'utf-8' codec can't decode byte"
else:
msg = "'utf8' codec can't decode byte"
with tm.assert_raises_regex(UnicodeDecodeError, msg):
self.read_csv(stream)
stream.close()
示例4: __init__
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import StreamRecoder [as 别名]
def __init__(self, f, encoding):
f.seek(0)
if six.PY3:
self.reader = f
if six.PY2:
self.reader = codecs.StreamRecoder(f,
codecs.getencoder('utf-8'),
codecs.getdecoder('utf-8'),
codecs.getreader(encoding),
codecs.getwriter(encoding))