Fix Python 3 integer division in zmachine modules

Python 2 `/` did integer division, Python 3 returns float. Changed to
`//` in zobjectparser (attribute byte offset) and zmemory (address
bounds checks). Zork 1 hit this on the first test_attr opcode.
This commit is contained in:
Jared Miller 2026-02-09 21:07:16 -05:00
parent 8eb2371ce1
commit e1c6a92368
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 5 additions and 5 deletions

View file

@ -235,18 +235,18 @@ class ZMemory:
def word_address(self, address): def word_address(self, address):
"""Return the 'actual' address of word address ADDRESS.""" """Return the 'actual' address of word address ADDRESS."""
if address < 0 or address > (self._total_size / 2): if address < 0 or address > (self._total_size // 2):
raise ZMemoryOutOfBounds raise ZMemoryOutOfBounds
return address * 2 return address * 2
def packed_address(self, address): def packed_address(self, address):
"""Return the 'actual' address of packed address ADDRESS.""" """Return the 'actual' address of packed address ADDRESS."""
if 1 <= self.version <= 3: if 1 <= self.version <= 3:
if address < 0 or address > (self._total_size / 2): if address < 0 or address > (self._total_size // 2):
raise ZMemoryOutOfBounds raise ZMemoryOutOfBounds
return address * 2 return address * 2
elif 4 <= self.version <= 5: elif 4 <= self.version <= 5:
if address < 0 or address > (self._total_size / 4): if address < 0 or address > (self._total_size // 4):
raise ZMemoryOutOfBounds raise ZMemoryOutOfBounds
return address * 4 return address * 4
else: else:

View file

@ -169,12 +169,12 @@ class ZObjectParser:
if 1 <= self._memory.version <= 3: if 1 <= self._memory.version <= 3:
if not (0 <= attrnum <= 31): if not (0 <= attrnum <= 31):
raise ZObjectIllegalAttributeNumber raise ZObjectIllegalAttributeNumber
bf = BitField(self._memory[object_addr + (attrnum / 8)]) bf = BitField(self._memory[object_addr + (attrnum // 8)])
elif 4 <= self._memory.version <= 5: elif 4 <= self._memory.version <= 5:
if not (0 <= attrnum <= 47): if not (0 <= attrnum <= 47):
raise ZObjectIllegalAttributeNumber raise ZObjectIllegalAttributeNumber
bf = BitField(self._memory[object_addr + (attrnum / 8)]) bf = BitField(self._memory[object_addr + (attrnum // 8)])
else: else:
raise ZObjectIllegalVersion raise ZObjectIllegalVersion