Skip to content

Commit

Permalink
fix more array.tostring() removal issues in Python 3.9
Browse files Browse the repository at this point in the history
Several shell.py script versions use the depreciated array.tostring() method,
which has been removed in Python 3.9. I changed these instances to .tobytes(),
and adjusted .ljust()'s second argument to be a byte string.

I also fixed a related issue, where #flash'ing from a shell.py which assumes a
2-cell wide Forth to a non-.hex file would error
because the unsigned short array returned from serialize() would be written to
file with assumptions of signed short:

open(dest, "wb").write(array.array("h", d).tobytes())

This would cause an overflow error. Changing "h" to "H" in that last code
snippet (in swapforth.py), lines up the signdedness.
  • Loading branch information
stuij committed Oct 14, 2022
1 parent 8dbff77 commit 9c40064
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion j1a/nandland-go/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def serialize(self):
for l in lines:
l = l.split()
s += [int(b, 16) for b in l[1:17]]
s = array.array('B', s).tostring().ljust(8192, chr(0xff))
s = array.array('B', s).tobytes().ljust(8192, bytes([0xFF]))
return array.array('H', s)

if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion j1a/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def serialize(self):
for l in lines:
l = l.split()
s += [int(b, 16) for b in l[1:17]]
s = array.array('B', s).tostring().ljust(8192, chr(0xff))
s = array.array('B', s).tobytes().ljust(8192, bytes([0xFF]))
return array.array('H', s)

if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion j1a/verilator/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def serialize(self):
for l in lines:
l = l.split()
s += [int(b, 16) for b in l[1:17]]
s = array.array('B', s).tostring().ljust(8192, chr(0xff))
s = array.array('B', s).tobytes().ljust(8192, bytes([0xFF]))
return array.array('H', s)

if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion j1a/verilator/simshell4.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def serialize(self):
for l in lines:
l = l.split()
s += [int(b, 16) for b in l[1:17]]
s = array.array('B', s).tostring().ljust(8192, chr(0xff))
s = array.array('B', s).tobytes().ljust(8192, bytes([0xFF]))
return array.array('H', s)

if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions shell/swapforth.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ def shellcmd(self, cmd):
if dest.endswith('.hex'):
open(dest, "w").write("".join(["%08x\n" % (x & 0xffffffff) for x in d]))
else:
open(dest, "wb").write(array.array("i", d).tostring())
open(dest, "wb").write(array.array("i", d).tobytes())
else:
if dest.endswith('.hex'):
open(dest, "w").write("".join(["%04x\n" % (x & 0xffff) for x in d]))
else:
open(dest, "wb").write(array.array("h", d).tostring())
open(dest, "wb").write(array.array("H", d).tobytes())
elif cmd.startswith('#setclock'):
n = datetime.utcnow()
cmd = "decimal %d %d %d %d %d %d >time&date" % (n.second, n.minute, n.hour, n.day, n.month, n.year)
Expand Down

0 comments on commit 9c40064

Please sign in to comment.