Packing python bools as tw net bools just works

python handles bools more like the ints 0 and 1
and those are exactly the values the
teeworlds network protocol expects

I can totally see this breaking in python4
or a new mypy version

maybe one day a ``get_bool()`` could make sense
also for readability

but for now this should be stable as long as the
tests pass
This commit is contained in:
ChillerDragon 2023-04-09 12:11:51 +02:00
parent f1d562f25f
commit 3b93a6bba2
2 changed files with 14 additions and 1 deletions

View file

@ -471,7 +471,7 @@ def pack_field(member: NetMessageMemberJson) -> str:
packer = 'int'
elif member['type']['kind'] in ('int32', 'tick'):
packer = 'int'
elif member['type']['kind'] == 'boolean': # TODO: can we back True and False as int?
elif member['type']['kind'] == 'boolean':
packer = 'int'
elif member['type']['kind'] == 'tune_param':
packer = 'int'

View file

@ -11,6 +11,19 @@ def test_unpack_ints_and_strings() -> None:
assert u.get_str() == 'foo'
assert u.get_str() == 'bar'
def test_pack_bools() -> None:
assert pack_int(False) == b'\x00'
assert pack_int(True) == b'\x01'
assert pack_int(0) == b'\x00'
assert pack_int(1) == b'\x01'
def test_unpack_bools() -> None:
data: bytes = b'\x00\x01'
u = Unpacker(data)
assert (u.get_int() == 1) is False
assert (u.get_int() == 1) is True
def test_simple_repack() -> None:
data: bytes = pack_str('hello world')
assert data == b'hello world\x00'