From 19f789bdfd58eba2ed8fe604bbabf8df0fcc0771 Mon Sep 17 00:00:00 2001 From: Johannes Krampf Date: Sun, 19 Jan 2014 16:03:49 +0100 Subject: build: Be careful with unicode and byte strings for python3 compatibility. Signed-off-by: Johannes Krampf --- scripts/buildrom.py | 6 ++++-- scripts/checkrom.py | 6 ++++-- scripts/layoutrom.py | 14 +++++++------- scripts/python23compat.py | 14 ++++++++++++++ scripts/readserial.py | 13 +++++++++---- scripts/transdump.py | 5 ++++- scripts/vgafixup.py | 4 ++-- 7 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 scripts/python23compat.py (limited to 'scripts') diff --git a/scripts/buildrom.py b/scripts/buildrom.py index 36de14e..8ff60e2 100755 --- a/scripts/buildrom.py +++ b/scripts/buildrom.py @@ -7,6 +7,8 @@ import sys +from python23compat import as_bytes + def alignpos(pos, alignbytes): mask = alignbytes - 1 return (pos + mask) & ~mask @@ -26,7 +28,7 @@ def main(): count = len(data) # Pad to a 512 byte boundary - data += "\0" * (alignpos(count, 512) - count) + data += as_bytes("\0") * (alignpos(count, 512) - count) count = len(data) # Check if a pci header is present @@ -35,7 +37,7 @@ def main(): data = data[:pcidata + 16] + chr(int(count/512)) + chr(0) + data[pcidata + 18:] # Fill in size field; clear checksum field - data = data[:2] + chr(int(count/512)) + data[3:6] + "\0" + data[7:] + data = data[:2] + chr(int(count/512)) + data[3:6] + as_bytes("\0") + data[7:] # Checksum rom newsum = (256 - checksum(data)) & 0xff diff --git a/scripts/checkrom.py b/scripts/checkrom.py index 30c9db2..83d4671 100755 --- a/scripts/checkrom.py +++ b/scripts/checkrom.py @@ -8,6 +8,8 @@ import sys import layoutrom +from python23compat import as_bytes + def subst(data, offset, new): return data[:offset] + new + data[offset + len(new):] @@ -24,7 +26,7 @@ def main(): objinfo, finalsize, rawfile, outfile = sys.argv[1:] # Read in symbols - objinfofile = open(objinfo, 'rb') + objinfofile = open(objinfo, 'r') symbols = layoutrom.parseObjDump(objinfofile, 'in')[1] # Read in raw file @@ -90,7 +92,7 @@ def main(): # Write final file f = open(outfile, 'wb') - f.write(("\0" * (finalsize - datasize)) + rawdata) + f.write((as_bytes("\0") * (finalsize - datasize)) + rawdata) f.close() if __name__ == '__main__': diff --git a/scripts/layoutrom.py b/scripts/layoutrom.py index 3e57787..b325406 100755 --- a/scripts/layoutrom.py +++ b/scripts/layoutrom.py @@ -363,7 +363,7 @@ def writeLinkerScripts(li, out16, out32seg, out32flat): int(li.zonelow_base / 16), li.sec16_start - BUILD_BIOS_ADDR, outRelSections(li.sections16, 'code16_start', useseg=1)) - outfile = open(out16, 'wb') + outfile = open(out16, 'w') outfile.write(COMMONHEADER + out + COMMONTRAILER) outfile.close() @@ -375,7 +375,7 @@ def writeLinkerScripts(li, out16, out32seg, out32flat): } """ % (li.sec32seg_start - BUILD_BIOS_ADDR, outRelSections(li.sections32seg, 'code32seg_start', useseg=1)) - outfile = open(out32seg, 'wb') + outfile = open(out32seg, 'w') outfile.write(COMMONHEADER + out + COMMONTRAILER) outfile.close() @@ -445,7 +445,7 @@ PHDRS text PT_LOAD AT ( code32flat_start ) ; } """ - outfile = open(out32flat, 'wb') + outfile = open(out32flat, 'w') outfile.write(out) outfile.close() @@ -644,7 +644,7 @@ def parseObjDump(file, fileid): # Parser for constants in simple C header files. def scanconfig(file): - f = open(file, 'rb') + f = open(file, 'r') opts = {} for l in f.readlines(): parts = l.split() @@ -663,9 +663,9 @@ def main(): in16, in32seg, in32flat, cfgfile, out16, out32seg, out32flat = sys.argv[1:] # Read in the objdump information - infile16 = open(in16, 'rb') - infile32seg = open(in32seg, 'rb') - infile32flat = open(in32flat, 'rb') + infile16 = open(in16, 'r') + infile32seg = open(in32seg, 'r') + infile32flat = open(in32flat, 'r') # infoX = (sections, symbols) info16 = parseObjDump(infile16, '16') diff --git a/scripts/python23compat.py b/scripts/python23compat.py new file mode 100644 index 0000000..572b7f1 --- /dev/null +++ b/scripts/python23compat.py @@ -0,0 +1,14 @@ +# Helper code for compatibility of the code with both Python 2 and Python 3 +# +# Copyright (C) 2014 Johannes Krampf +# +# This file may be distributed under the terms of the GNU GPLv3 license. + +import sys + +if (sys.version_info > (3, 0)): + def as_bytes(str): + return bytes(str, "ASCII") +else: + def as_bytes(str): + return str diff --git a/scripts/readserial.py b/scripts/readserial.py index 5b40fdc..4f29648 100755 --- a/scripts/readserial.py +++ b/scripts/readserial.py @@ -13,6 +13,8 @@ import time import select import optparse +from python23compat import as_bytes + # Reset time counter after this much idle time. RESTARTINTERVAL = 60 # Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data @@ -25,7 +27,7 @@ def calibrateserialwrite(outfile, byteadjust): data = data * 80 while 1: st = time.time() - outfile.write(data) + outfile.write(as_bytes(data)) outfile.flush() et = time.time() sys.stdout.write( @@ -85,11 +87,11 @@ def readserial(infile, logfile, byteadjust): msg = "\n\n======= %s (adjust=%.1fus)\n" % ( time.asctime(time.localtime(datatime)), byteadjust * 1000000) sys.stdout.write(msg) - logfile.write(msg) + logfile.write(as_bytes(msg)) lasttime = datatime # Translate unprintable chars; add timestamps - out = "" + out = as_bytes("") for c in d: if isnewline: delta = datatime - starttime - (charcount * byteadjust) @@ -113,7 +115,10 @@ def readserial(infile, logfile, byteadjust): continue out += c - sys.stdout.write(out) + if (sys.version_info > (3, 0)): + sys.stdout.buffer.write(out) + else: + sys.stdout.write(out) sys.stdout.flush() logfile.write(out) logfile.flush() diff --git a/scripts/transdump.py b/scripts/transdump.py index 4caaeb7..665f04a 100755 --- a/scripts/transdump.py +++ b/scripts/transdump.py @@ -44,7 +44,10 @@ def main(): filehdl = open(filename, 'r') mem = parseMem(filehdl) for i in mem: - sys.stdout.write(struct.pack(" (3, 0)): + sys.stdout.buffer.write(struct.pack("