本文整理汇总了Python中binascii.b2a_qp方法的典型用法代码示例。如果您正苦于以下问题:Python binascii.b2a_qp方法的具体用法?Python binascii.b2a_qp怎么用?Python binascii.b2a_qp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类binascii
的用法示例。
在下文中一共展示了binascii.b2a_qp方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_bytes_content
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def set_bytes_content(msg, data, maintype, subtype, cte='base64',
disposition=None, filename=None, cid=None,
params=None, headers=None):
_prepare_set(msg, maintype, subtype, headers)
if cte == 'base64':
data = _encode_base64(data, max_line_length=msg.policy.max_line_length)
elif cte == 'quoted-printable':
# XXX: quoprimime.body_encode won't encode newline characters in data,
# so we can't use it. This means max_line_length is ignored. Another
# bug to fix later. (Note: encoders.quopri is broken on line ends.)
data = binascii.b2a_qp(data, istext=False, header=False, quotetabs=True)
data = data.decode('ascii')
elif cte == '7bit':
# Make sure it really is only ASCII. The early warning here seems
# worth the overhead...if you care write your own content manager :).
data.encode('ascii')
elif cte in ('8bit', 'binary'):
data = data.decode('ascii', 'surrogateescape')
msg.set_payload(data)
msg['Content-Transfer-Encoding'] = cte
_finalize_set(msg, disposition, filename, cid, params)
示例2: write
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def write(self, chunk: bytes) -> None:
if self._compress is not None:
if chunk:
chunk = self._compress.compress(chunk)
if not chunk:
return
if self._encoding == 'base64':
buf = self._encoding_buffer
assert buf is not None
buf.extend(chunk)
if buf:
div, mod = divmod(len(buf), 3)
enc_chunk, self._encoding_buffer = (
buf[:div * 3], buf[div * 3:])
if enc_chunk:
b64chunk = base64.b64encode(enc_chunk)
await self._writer.write(b64chunk)
elif self._encoding == 'quoted-printable':
await self._writer.write(binascii.b2a_qp(chunk))
else:
await self._writer.write(chunk)
示例3: test_qp
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def test_qp(self):
# A test for SF bug 534347 (segfaults without the proper fix)
try:
binascii.a2b_qp("", **{1:1})
except TypeError:
pass
else:
self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
self.assertEqual(binascii.a2b_qp("= "), "= ")
self.assertEqual(binascii.a2b_qp("=="), "=")
self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
self.assertEqual(
binascii.b2a_qp("\xff\r\n\xff\n\xff"),
"=FF\r\n=FF\r\n=FF"
)
self.assertEqual(
binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
"0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
)
示例4: write
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def write(self, chunk):
if self._compress is not None:
if chunk:
chunk = self._compress.compress(chunk)
if not chunk:
return
if self._encoding == 'base64':
self._encoding_buffer.extend(chunk)
if self._encoding_buffer:
buffer = self._encoding_buffer
div, mod = divmod(len(buffer), 3)
enc_chunk, self._encoding_buffer = (
buffer[:div * 3], buffer[div * 3:])
if enc_chunk:
enc_chunk = base64.b64encode(enc_chunk)
yield from self._writer.write(enc_chunk)
elif self._encoding == 'quoted-printable':
yield from self._writer.write(binascii.b2a_qp(chunk))
else:
yield from self._writer.write(chunk)
示例5: encodestring
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def encodestring(s, quotetabs = 0, header = 0):
if b2a_qp is not None:
return b2a_qp(s, quotetabs = quotetabs, header = header)
from cStringIO import StringIO
infp = StringIO(s)
outfp = StringIO()
encode(infp, outfp, quotetabs, header)
return outfp.getvalue()
示例6: test_qp
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def test_qp(self):
# A test for SF bug 534347 (segfaults without the proper fix)
try:
binascii.a2b_qp("", **{1:1})
except TypeError:
pass
else:
self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
self.assertEqual(binascii.a2b_qp("= "), "= ")
self.assertEqual(binascii.a2b_qp("=="), "=")
self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
self.assertEqual(
binascii.b2a_qp("\xff\r\n\xff\n\xff"),
"=FF\r\n=FF\r\n=FF"
)
self.assertEqual(
binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
"0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
)
self.assertEqual(binascii.b2a_qp('\0\n'), '=00\n')
self.assertEqual(binascii.b2a_qp('\0\n', quotetabs=True), '=00\n')
self.assertEqual(binascii.b2a_qp('foo\tbar\t\n'), 'foo\tbar=09\n')
self.assertEqual(binascii.b2a_qp('foo\tbar\t\n', quotetabs=True), 'foo=09bar=09\n')
self.assertEqual(binascii.b2a_qp('.'), '=2E')
self.assertEqual(binascii.b2a_qp('.\n'), '=2E\n')
self.assertEqual(binascii.b2a_qp('a.\n'), 'a.\n')
示例7: _ConvertAttributeValueToDict
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def _ConvertAttributeValueToDict(cls, attribute_value):
"""Converts an attribute value into a JSON dictionary.
Args:
attribute_value (object): an attribute value.
Returns:
dict|list: The JSON serialized object which can be a dictionary or a list.
"""
if isinstance(attribute_value, bytes):
encoded_value = binascii.b2a_qp(attribute_value)
encoded_value = codecs.decode(encoded_value, 'ascii')
attribute_value = {
'__type__': 'bytes',
'stream': '{0:s}'.format(encoded_value)
}
elif isinstance(attribute_value, (list, tuple)):
json_list = []
for list_element in attribute_value:
json_dict = cls._ConvertAttributeValueToDict(list_element)
json_list.append(json_dict)
if isinstance(attribute_value, list):
attribute_value = json_list
else:
attribute_value = {
'__type__': 'tuple',
'values': json_list
}
elif isinstance(attribute_value, collections.Counter):
attribute_value = cls._ConvertCollectionsCounterToDict(attribute_value)
elif isinstance(attribute_value, dfvfs_path_spec.PathSpec):
attribute_value = cls._ConvertPathSpecToDict(attribute_value)
elif isinstance(attribute_value, containers_interface.AttributeContainer):
attribute_value = cls._ConvertAttributeContainerToDict(attribute_value)
return attribute_value
示例8: encodestring
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def encodestring(s, quotetabs=False, header=False):
if b2a_qp is not None:
return b2a_qp(s, quotetabs=quotetabs, header=header)
from io import BytesIO
infp = BytesIO(s)
outfp = BytesIO()
encode(infp, outfp, quotetabs, header)
return outfp.getvalue()
示例9: test_qp
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def test_qp(self):
binascii.a2b_qp(data=b"", header=False) # Keyword arguments allowed
# A test for SF bug 534347 (segfaults without the proper fix)
try:
binascii.a2b_qp(b"", **{1:1})
except TypeError:
pass
else:
self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
self.assertEqual(binascii.a2b_qp(b"= "), b"= ")
self.assertEqual(binascii.a2b_qp(b"=="), b"=")
self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX")
self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
self.assertEqual(binascii.a2b_qp(b"=00\r\n=00"), b"\x00\r\n\x00")
self.assertEqual(
binascii.b2a_qp(b"\xff\r\n\xff\n\xff"),
b"=FF\r\n=FF\r\n=FF")
self.assertEqual(
binascii.b2a_qp(b"0"*75+b"\xff\r\n\xff\r\n\xff"),
b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF")
self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n')
self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n')
self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n'), b'foo\tbar=09\n')
self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n', quotetabs=True),
b'foo=09bar=09\n')
self.assertEqual(binascii.b2a_qp(b'.'), b'=2E')
self.assertEqual(binascii.b2a_qp(b'.\n'), b'=2E\n')
self.assertEqual(binascii.b2a_qp(b'a.\n'), b'a.\n')
示例10: process_buffers_for_display
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def process_buffers_for_display(s, limit=40):
"""Process a buffer for human-readable display.
This function performs the following operation on each of the buffers in `s`.
1. Truncate input buffer if the length of the buffer is greater than
`limit`, to prevent large strings from overloading the frontend.
2. Apply `binascii.b2a_qp` on the truncated buffer to make the buffer
printable and convertible to JSON.
3. If truncation happened (in step 1), append a string at the end
describing the original length and the truncation.
Args:
s: The buffer to be processed, either a single buffer or a nested array of
them.
limit: Length limit for each buffer, beyond which truncation will occur.
Return:
A single processed buffer or a nested array of processed buffers.
"""
if isinstance(s, (list, tuple)):
return [process_buffers_for_display(elem, limit=limit) for elem in s]
else:
length = len(s)
if length > limit:
return binascii.b2a_qp(
s[:limit]
) + b" (length-%d truncated at %d bytes)" % (length, limit)
else:
return binascii.b2a_qp(s)
示例11: testBinaryScalarBelowLimit
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def testBinaryScalarBelowLimit(self):
x = b"\x01\x02\x03"
self.assertEqual(
binascii.b2a_qp(x), tensor_helper.process_buffers_for_display(x, 10)
)
示例12: testBinaryScalarAboveLimit
# 需要导入模块: import binascii [as 别名]
# 或者: from binascii import b2a_qp [as 别名]
def testBinaryScalarAboveLimit(self):
x = b"\x01\x02\x03"
self.assertEqual(
binascii.b2a_qp(x[:2]) + b" (length-3 truncated at 2 bytes)",
tensor_helper.process_buffers_for_display(x, 2),
)