got sv.bc working for pospopcount
[openpower-isa.git] / src / openpower / simulator / program.py
index c2b4adbe1f6acdd1360629e6ac343eb4a9c19881..bf86d1317b6121994fbb92052d2e392c311f7399 100644 (file)
@@ -1,4 +1,4 @@
-# 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>
 
@@ -62,13 +62,17 @@ class Program:
         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:
@@ -88,12 +92,12 @@ class Program:
                     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):