我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用__builtin__.bytes()。
def test_ab64_encode(self): """ab64_encode()""" from passlib.utils.binary import ab64_encode # accept bytes self.assertEqual(ab64_encode(hb("69b7")), b"abc") # reject unicode self.assertRaises(TypeError if PY3 else UnicodeEncodeError, ab64_encode, hb("69b7").decode("latin-1")) # insert correct padding before decoding self.assertEqual(ab64_encode(hb("69b71d")), b"abcd") # 0 mod 4 self.assertEqual(ab64_encode(hb("69b71d79")), b"abcdeQ") # 2 mod 4 self.assertEqual(ab64_encode(hb("69b71d79f8")), b"abcdefg") # 3 mod 4 # output "./" altchars self.assertEqual(ab64_encode(hb("69bfbf")), b"ab./")
def test_b64s_decode(self): """b64s_decode()""" from passlib.utils.binary import b64s_decode # accept bytes or unicode self.assertEqual(b64s_decode(b"abc"), hb("69b7")) self.assertEqual(b64s_decode(u("abc")), hb("69b7")) # reject non-ascii unicode self.assertRaises(ValueError, b64s_decode, u("ab\xff")) # underlying a2b_ascii treats non-base64 chars as "Incorrect padding" self.assertRaises(TypeError, b64s_decode, b"ab\xff") self.assertRaises(TypeError, b64s_decode, b"ab!") self.assertRaises(TypeError, b64s_decode, u("ab!")) # insert correct padding, handle dirty padding bits self.assertEqual(b64s_decode(b"abcd"), hb("69b71d")) # 0 mod 4 self.assertRaises(ValueError, b64s_decode, b"abcde") # 1 mod 4 self.assertEqual(b64s_decode(b"abcdef"), hb("69b71d79")) # 2 mod 4, dirty padding bits self.assertEqual(b64s_decode(b"abcdeQ"), hb("69b71d79")) # 2 mod 4, clean padding bits self.assertEqual(b64s_decode(b"abcdefg"), hb("69b71d79f8")) # 3 mod 4, clean padding bits
def test_b64s_encode(self): """b64s_encode()""" from passlib.utils.binary import b64s_encode # accept bytes self.assertEqual(b64s_encode(hb("69b7")), b"abc") # reject unicode self.assertRaises(TypeError if PY3 else UnicodeEncodeError, b64s_encode, hb("69b7").decode("latin-1")) # insert correct padding before decoding self.assertEqual(b64s_encode(hb("69b71d")), b"abcd") # 0 mod 4 self.assertEqual(b64s_encode(hb("69b71d79")), b"abcdeQ") # 2 mod 4 self.assertEqual(b64s_encode(hb("69b71d79f8")), b"abcdefg") # 3 mod 4 # output "+/" altchars self.assertEqual(b64s_encode(hb("69bfbf")), b"ab+/")
def test_non_numeric_input_types(self): # Test possible non-numeric types for the argument x, including # subclasses of the explicitly documented accepted types. class CustomStr(str): pass class CustomBytes(bytes): pass class CustomByteArray(bytearray): pass values = [b'100', bytearray(b'100'), CustomStr('100'), CustomBytes(b'100'), CustomByteArray(b'100')] for x in values: msg = 'x has type %s' % type(x).__name__ self.assertEqual(int(x), 100, msg=msg) self.assertEqual(int(x, 2), 4, msg=msg)
def test_getrandbytes(self): """getrandbytes()""" from passlib.utils import getrandbytes wrapper = partial(getrandbytes, self.getRandom()) self.assertEqual(len(wrapper(0)), 0) a = wrapper(10) b = wrapper(10) self.assertIsInstance(a, bytes) self.assertEqual(len(a), 10) self.assertEqual(len(b), 10) self.assertNotEqual(a, b)
def test_getrandstr(self, seed): """getrandstr()""" from passlib.utils import getrandstr wrapper = partial(getrandstr, self.getRandom(seed=seed)) # count 0 self.assertEqual(wrapper('abc',0), '') # count <0 self.assertRaises(ValueError, wrapper, 'abc', -1) # letters 0 self.assertRaises(ValueError, wrapper, '', 0) # letters 1 self.assertEqual(wrapper('a', 5), 'aaaaa') # NOTE: the following parts are non-deterministic, # with a small chance of failure (outside chance it may pick # a string w/o one char, even more remote chance of picking # same string). to combat this, we run it against multiple # fixed seeds (using run_with_fixed_seeds decorator), # and hope that they're sufficient to test the range of behavior. # letters x = wrapper(u('abc'), 32) y = wrapper(u('abc'), 32) self.assertIsInstance(x, unicode) self.assertNotEqual(x,y) self.assertEqual(sorted(set(x)), [u('a'),u('b'),u('c')]) # bytes x = wrapper(b'abc', 32) y = wrapper(b'abc', 32) self.assertIsInstance(x, bytes) self.assertNotEqual(x,y) # NOTE: decoding this due to py3 bytes self.assertEqual(sorted(set(x.decode("ascii"))), [u('a'),u('b'),u('c')])
def test_bytes(self): """test b() helper, bytes and native str type""" if PY3: import builtins self.assertIs(bytes, builtins.bytes) else: import __builtin__ as builtins self.assertIs(bytes, builtins.str) self.assertIsInstance(b'', bytes) self.assertIsInstance(b'\x00\xff', bytes) if PY3: self.assertEqual(b'\x00\xff'.decode("latin-1"), "\x00\xff") else: self.assertEqual(b'\x00\xff', "\x00\xff")
def test_to_bytes(self): """test to_bytes()""" from passlib.utils import to_bytes # check unicode inputs self.assertEqual(to_bytes(u('abc')), b'abc') self.assertEqual(to_bytes(u('\x00\xff')), b'\x00\xc3\xbf') # check unicode w/ encodings self.assertEqual(to_bytes(u('\x00\xff'), 'latin-1'), b'\x00\xff') self.assertRaises(ValueError, to_bytes, u('\x00\xff'), 'ascii') # check bytes inputs self.assertEqual(to_bytes(b'abc'), b'abc') self.assertEqual(to_bytes(b'\x00\xff'), b'\x00\xff') self.assertEqual(to_bytes(b'\x00\xc3\xbf'), b'\x00\xc3\xbf') # check byte inputs ignores enocding self.assertEqual(to_bytes(b'\x00\xc3\xbf', "latin-1"), b'\x00\xc3\xbf') # check bytes transcoding self.assertEqual(to_bytes(b'\x00\xc3\xbf', "latin-1", "", "utf-8"), b'\x00\xff') # check other self.assertRaises(AssertionError, to_bytes, 'abc', None) self.assertRaises(TypeError, to_bytes, None)
def test_ab64_decode(self): """ab64_decode()""" from passlib.utils.binary import ab64_decode # accept bytes or unicode self.assertEqual(ab64_decode(b"abc"), hb("69b7")) self.assertEqual(ab64_decode(u("abc")), hb("69b7")) # reject non-ascii unicode self.assertRaises(ValueError, ab64_decode, u("ab\xff")) # underlying a2b_ascii treats non-base64 chars as "Incorrect padding" self.assertRaises(TypeError, ab64_decode, b"ab\xff") self.assertRaises(TypeError, ab64_decode, b"ab!") self.assertRaises(TypeError, ab64_decode, u("ab!")) # insert correct padding, handle dirty padding bits self.assertEqual(ab64_decode(b"abcd"), hb("69b71d")) # 0 mod 4 self.assertRaises(ValueError, ab64_decode, b"abcde") # 1 mod 4 self.assertEqual(ab64_decode(b"abcdef"), hb("69b71d79")) # 2 mod 4, dirty padding bits self.assertEqual(ab64_decode(b"abcdeQ"), hb("69b71d79")) # 2 mod 4, clean padding bits self.assertEqual(ab64_decode(b"abcdefg"), hb("69b71d79f8")) # 3 mod 4, clean padding bits # support "./" or "+/" altchars # (lets us transition to "+/" representation, merge w/ b64s_decode) self.assertEqual(ab64_decode(b"ab+/"), hb("69bfbf")) self.assertEqual(ab64_decode(b"ab./"), hb("69bfbf"))
def test_decode_bytes_padding(self): """test decode_bytes() ignores padding bits""" bchr = (lambda v: bytes([v])) if PY3 else chr engine = self.engine m = self.m decode = engine.decode_bytes BNULL = b"\x00" # length == 2 mod 4: 4 bits of padding self.assertEqual(decode(m(0,0)), BNULL) for i in range(0,6): if engine.big: # 4 lsb padding correct = BNULL if i < 4 else bchr(1<<(i-4)) else: # 4 msb padding correct = bchr(1<<(i+6)) if i < 2 else BNULL self.assertEqual(decode(m(0,1<<i)), correct, "%d/4 bits:" % i) # length == 3 mod 4: 2 bits of padding self.assertEqual(decode(m(0,0,0)), BNULL*2) for i in range(0,6): if engine.big: # 2 lsb are padding correct = BNULL if i < 2 else bchr(1<<(i-2)) else: # 2 msg are padding correct = bchr(1<<(i+4)) if i < 4 else BNULL self.assertEqual(decode(m(0,0,1<<i)), BNULL + correct, "%d/2 bits:" % i)
def bascii_to_str(s): assert isinstance(s, bytes) return s.decode("ascii")
def iter_byte_values(s): assert isinstance(s, bytes) return s
def iter_byte_chars(s): assert isinstance(s, bytes) # FIXME: there has to be a better way to do this return (bytes([c]) for c in s)
def bascii_to_str(s): assert isinstance(s, bytes) return s
def iter_byte_chars(s): assert isinstance(s, bytes) return s
def test_bytes_mul(self): self.assertEqual(b'\x00' * int(5), b'\x00' * 5) self.assertEqual(bytes(b'\x00') * int(5), bytes(b'\x00') * 5)
def test_int_bytes(self): self.assertEqual(int(b'a\r\n', 16), 10) self.assertEqual(int(bytes(b'a\r\n'), 16), 10)
def test_getrandbytes(self): "test getrandbytes()" from passlib.utils import getrandbytes, rng def f(*a,**k): return getrandbytes(rng, *a, **k) self.assertEqual(len(f(0)), 0) a = f(10) b = f(10) self.assertIsInstance(a, bytes) self.assertEqual(len(a), 10) self.assertEqual(len(b), 10) self.assertNotEqual(a, b)
def test_getrandstr(self): "test getrandstr()" from passlib.utils import getrandstr, rng def f(*a,**k): return getrandstr(rng, *a, **k) # count 0 self.assertEqual(f('abc',0), '') # count <0 self.assertRaises(ValueError, f, 'abc', -1) # letters 0 self.assertRaises(ValueError, f, '', 0) # letters 1 self.assertEqual(f('a',5), 'aaaaa') # letters x = f(u('abc'), 16) y = f(u('abc'), 16) self.assertIsInstance(x, unicode) self.assertNotEqual(x,y) self.assertEqual(sorted(set(x)), [u('a'),u('b'),u('c')]) # bytes x = f(b('abc'), 16) y = f(b('abc'), 16) self.assertIsInstance(x, bytes) self.assertNotEqual(x,y) # NOTE: decoding this due to py3 bytes self.assertEqual(sorted(set(x.decode("ascii"))), [u('a'),u('b'),u('c')]) # generate_password from passlib.utils import generate_password self.assertEqual(len(generate_password(15)), 15)
def test_bytes(self): "test b() helper, bytes and native str type" if PY3: import builtins self.assertIs(bytes, builtins.bytes) else: import __builtin__ as builtins self.assertIs(bytes, builtins.str) self.assertIsInstance(b(''), bytes) self.assertIsInstance(b('\x00\xff'), bytes) if PY3: self.assertEqual(b('\x00\xff').decode("latin-1"), "\x00\xff") else: self.assertEqual(b('\x00\xff'), "\x00\xff")
def test_to_bytes(self): "test to_bytes()" from passlib.utils import to_bytes # check unicode inputs self.assertEqual(to_bytes(u('abc')), b('abc')) self.assertEqual(to_bytes(u('\x00\xff')), b('\x00\xc3\xbf')) # check unicode w/ encodings self.assertEqual(to_bytes(u('\x00\xff'), 'latin-1'), b('\x00\xff')) self.assertRaises(ValueError, to_bytes, u('\x00\xff'), 'ascii') # check bytes inputs self.assertEqual(to_bytes(b('abc')), b('abc')) self.assertEqual(to_bytes(b('\x00\xff')), b('\x00\xff')) self.assertEqual(to_bytes(b('\x00\xc3\xbf')), b('\x00\xc3\xbf')) # check byte inputs ignores enocding self.assertEqual(to_bytes(b('\x00\xc3\xbf'), "latin-1"), b('\x00\xc3\xbf')) # check bytes transcoding self.assertEqual(to_bytes(b('\x00\xc3\xbf'), "latin-1", "", "utf-8"), b('\x00\xff')) # check other self.assertRaises(AssertionError, to_bytes, 'abc', None) self.assertRaises(TypeError, to_bytes, None)
def test_decode_bytes_padding(self): "test decode_bytes() ignores padding bits" bchr = (lambda v: bytes([v])) if PY3 else chr engine = self.engine m = self.m decode = engine.decode_bytes BNULL = b("\x00") # length == 2 mod 4: 4 bits of padding self.assertEqual(decode(m(0,0)), BNULL) for i in range(0,6): if engine.big: # 4 lsb padding correct = BNULL if i < 4 else bchr(1<<(i-4)) else: # 4 msb padding correct = bchr(1<<(i+6)) if i < 2 else BNULL self.assertEqual(decode(m(0,1<<i)), correct, "%d/4 bits:" % i) # length == 3 mod 4: 2 bits of padding self.assertEqual(decode(m(0,0,0)), BNULL*2) for i in range(0,6): if engine.big: # 2 lsb are padding correct = BNULL if i < 2 else bchr(1<<(i-2)) else: # 2 msg are padding correct = bchr(1<<(i+4)) if i < 4 else BNULL self.assertEqual(decode(m(0,0,1<<i)), BNULL + correct, "%d/2 bits:" % i)
def iter_byte_values(s): assert isinstance(s, bytes) return (ord(c) for c in s)
def test_getrandbytes(self): """test getrandbytes()""" from passlib.utils import getrandbytes, rng def f(*a,**k): return getrandbytes(rng, *a, **k) self.assertEqual(len(f(0)), 0) a = f(10) b = f(10) self.assertIsInstance(a, bytes) self.assertEqual(len(a), 10) self.assertEqual(len(b), 10) self.assertNotEqual(a, b)
def test_getrandstr(self): """test getrandstr()""" from passlib.utils import getrandstr, rng def f(*a,**k): return getrandstr(rng, *a, **k) # count 0 self.assertEqual(f('abc',0), '') # count <0 self.assertRaises(ValueError, f, 'abc', -1) # letters 0 self.assertRaises(ValueError, f, '', 0) # letters 1 self.assertEqual(f('a',5), 'aaaaa') # letters x = f(u('abc'), 16) y = f(u('abc'), 16) self.assertIsInstance(x, unicode) self.assertNotEqual(x,y) self.assertEqual(sorted(set(x)), [u('a'),u('b'),u('c')]) # bytes x = f(b('abc'), 16) y = f(b('abc'), 16) self.assertIsInstance(x, bytes) self.assertNotEqual(x,y) # NOTE: decoding this due to py3 bytes self.assertEqual(sorted(set(x.decode("ascii"))), [u('a'),u('b'),u('c')]) # generate_password from passlib.utils import generate_password self.assertEqual(len(generate_password(15)), 15)
def test_bytes(self): """test b() helper, bytes and native str type""" if PY3: import builtins self.assertIs(bytes, builtins.bytes) else: import __builtin__ as builtins self.assertIs(bytes, builtins.str) self.assertIsInstance(b(''), bytes) self.assertIsInstance(b('\x00\xff'), bytes) if PY3: self.assertEqual(b('\x00\xff').decode("latin-1"), "\x00\xff") else: self.assertEqual(b('\x00\xff'), "\x00\xff")
def test_to_bytes(self): """test to_bytes()""" from passlib.utils import to_bytes # check unicode inputs self.assertEqual(to_bytes(u('abc')), b('abc')) self.assertEqual(to_bytes(u('\x00\xff')), b('\x00\xc3\xbf')) # check unicode w/ encodings self.assertEqual(to_bytes(u('\x00\xff'), 'latin-1'), b('\x00\xff')) self.assertRaises(ValueError, to_bytes, u('\x00\xff'), 'ascii') # check bytes inputs self.assertEqual(to_bytes(b('abc')), b('abc')) self.assertEqual(to_bytes(b('\x00\xff')), b('\x00\xff')) self.assertEqual(to_bytes(b('\x00\xc3\xbf')), b('\x00\xc3\xbf')) # check byte inputs ignores enocding self.assertEqual(to_bytes(b('\x00\xc3\xbf'), "latin-1"), b('\x00\xc3\xbf')) # check bytes transcoding self.assertEqual(to_bytes(b('\x00\xc3\xbf'), "latin-1", "", "utf-8"), b('\x00\xff')) # check other self.assertRaises(AssertionError, to_bytes, 'abc', None) self.assertRaises(TypeError, to_bytes, None)
def test_decode_bytes_padding(self): """test decode_bytes() ignores padding bits""" bchr = (lambda v: bytes([v])) if PY3 else chr engine = self.engine m = self.m decode = engine.decode_bytes BNULL = b("\x00") # length == 2 mod 4: 4 bits of padding self.assertEqual(decode(m(0,0)), BNULL) for i in range(0,6): if engine.big: # 4 lsb padding correct = BNULL if i < 4 else bchr(1<<(i-4)) else: # 4 msb padding correct = bchr(1<<(i+6)) if i < 2 else BNULL self.assertEqual(decode(m(0,1<<i)), correct, "%d/4 bits:" % i) # length == 3 mod 4: 2 bits of padding self.assertEqual(decode(m(0,0,0)), BNULL*2) for i in range(0,6): if engine.big: # 2 lsb are padding correct = BNULL if i < 2 else bchr(1<<(i-2)) else: # 2 msg are padding correct = bchr(1<<(i+4)) if i < 4 else BNULL self.assertEqual(decode(m(0,0,1<<i)), BNULL + correct, "%d/2 bits:" % i)