-# License: LGPLv3+
+# SPDX-License-Identifier: LGPL-3-or-later
# Copyright (C) 2020 Michael Nolan <mtnolan2640@gmail.com>
# Copyright (C) 2020 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
self.close()
def _get_binary(self, elffile):
- self.binfile = tempfile.NamedTemporaryFile(suffix=".bin")
- args = [cmds['objcopy'],
- "-O", "binary",
- "-I", self.endian_fmt,
- elffile.name,
- self.binfile.name]
- subprocess.check_output(args)
+ with tempfile.NamedTemporaryFile(suffix=".bin") as binfile:
+ args = [cmds['objcopy'],
+ "-O", "binary",
+ "-I", self.endian_fmt,
+ elffile.name,
+ binfile.name]
+ subprocess.check_output(args)
+ # copy over to persistent binfile (BytesIO)
+ self.binfile = BytesIO()
+ self.binfile.write(binfile.read())
+ self.binfile.seek(0)
def _link(self, ofile):
with tempfile.NamedTemporaryFile(suffix=".elf") as elffile:
self.obj_fmt,
"-o",
outfile.name]
- p = subprocess.Popen(args, stdin=subprocess.PIPE)
- p.communicate(self.assembly.encode('utf-8'))
- if p.wait() != 0:
- print("Error in program:")
- print(self.assembly)
- sys.exit(1)
+ with subprocess.Popen(args, stdin=subprocess.PIPE) as p:
+ p.communicate(self.assembly.encode('utf-8'))
+ if p.wait() != 0:
+ print("Error in program:")
+ print(self.assembly)
+ sys.exit(1)
self._link(outfile)
def _get_instructions(self):