2015-04-04 23:16:22 +01:00
|
|
|
# literals
|
|
|
|
print(b'123')
|
|
|
|
print(br'123')
|
|
|
|
print(rb'123')
|
|
|
|
print(b'\u1234')
|
|
|
|
|
|
|
|
# construction
|
2015-03-14 21:20:58 +00:00
|
|
|
print(bytes())
|
2015-04-04 23:16:22 +01:00
|
|
|
print(bytes(b'abc'))
|
2015-03-14 21:20:58 +00:00
|
|
|
|
2017-10-04 07:59:22 +01:00
|
|
|
# make sure empty bytes is converted correctly
|
|
|
|
print(str(bytes(), 'utf-8'))
|
|
|
|
|
2014-01-24 20:50:40 +00:00
|
|
|
a = b"123"
|
|
|
|
print(a)
|
|
|
|
print(str(a))
|
|
|
|
print(repr(a))
|
|
|
|
print(a[0], a[2])
|
|
|
|
print(a[-1])
|
2014-03-21 21:46:59 +00:00
|
|
|
print(str(a, "utf-8"))
|
|
|
|
print(str(a, "utf-8", "ignore"))
|
|
|
|
try:
|
|
|
|
str(a, "utf-8", "ignore", "toomuch")
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
2014-01-24 20:50:40 +00:00
|
|
|
|
|
|
|
s = 0
|
|
|
|
for i in a:
|
|
|
|
s += i
|
|
|
|
print(s)
|
2014-03-21 21:46:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
print(bytes("abc", "utf-8"))
|
|
|
|
print(bytes("abc", "utf-8", "replace"))
|
|
|
|
try:
|
|
|
|
bytes("abc")
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
try:
|
|
|
|
bytes("abc", "utf-8", "replace", "toomuch")
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
print(bytes(3))
|
|
|
|
|
|
|
|
print(bytes([3, 2, 1]))
|
|
|
|
print(bytes(range(5)))
|
|
|
|
|
2014-08-11 20:36:38 +01:00
|
|
|
# Make sure bytes are not mistreated as unicode
|
|
|
|
x = b"\xff\x8e\xfe}\xfd\x7f"
|
|
|
|
print(len(x))
|
|
|
|
print(x[0], x[1], x[2], x[3])
|
2015-01-28 20:29:51 +00:00
|
|
|
|
|
|
|
# Make sure init values are not mistreated as unicode chars
|
|
|
|
# For sequence of known len
|
|
|
|
print(bytes([128, 255]))
|
|
|
|
# For sequence of unknown len
|
|
|
|
print(bytes(iter([128, 255])))
|
2018-02-19 05:25:30 +00:00
|
|
|
|
|
|
|
# Shouldn't be able to make bytes with negative length
|
|
|
|
try:
|
|
|
|
bytes(-1)
|
|
|
|
except ValueError:
|
|
|
|
print('ValueError')
|