From beffcd6468dffc73166421724b9acbfab1259266 Mon Sep 17 00:00:00 2001 From: "David M. Palmer" Date: Sat, 6 May 2017 12:47:26 -0600 Subject: [PATCH] gen_esp32part: Make compatible with both python2.7 and python3. Merges https://github.com/espressif/esp-idf/pull/577 --- components/partition_table/gen_esp32part.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/components/partition_table/gen_esp32part.py b/components/partition_table/gen_esp32part.py index d57059f4e..7f08d6978 100755 --- a/components/partition_table/gen_esp32part.py +++ b/components/partition_table/gen_esp32part.py @@ -80,7 +80,7 @@ class PartitionTable(list): p.verify() # check for overlaps last = None - for p in sorted(self): + for p in sorted(self, key=lambda x:x.offset): if p.offset < 0x5000: raise InputError("Partition offset 0x%x is below 0x5000" % p.offset) if last is not None and p.offset < last.offset + last.size: @@ -100,10 +100,10 @@ class PartitionTable(list): raise InputError("Partition table is missing an end-of-table marker") def to_binary(self): - result = "".join(e.to_binary() for e in self) + result = b"".join(e.to_binary() for e in self) if len(result )>= MAX_PARTITION_LENGTH: raise InputError("Binary partition table length (%d) longer than max" % len(result)) - result += "\xFF" * (MAX_PARTITION_LENGTH - len(result)) # pad the sector, for signing + result += b"\xFF" * (MAX_PARTITION_LENGTH - len(result)) # pad the sector, for signing return result def to_csv(self, simple_formatting=False): @@ -137,7 +137,7 @@ class PartitionDefinition(object): }, } - MAGIC_BYTES = "\xAA\x50" + MAGIC_BYTES = b"\xAA\x50" ALIGNMENT = { APP_TYPE : 0x1000, @@ -267,7 +267,7 @@ class PartitionDefinition(object): self.MAGIC_BYTES, self.type, self.subtype, self.offset, self.size, - self.name, + self.name.encode(), flags) def to_csv(self, simple_formatting=False): @@ -346,10 +346,12 @@ def main(): if input_is_binary: output = table.to_csv() + with sys.stdout if args.output == '-' else open(args.output, 'w') as f: + f.write(output) else: output = table.to_binary() - with sys.stdout if args.output == '-' else open(args.output, 'w') as f: - f.write(output) + with sys.stdout.buffer if args.output == '-' else open(args.output, 'wb') as f: + f.write(output) if __name__ == '__main__': try: