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