Convert depfixer.Elf class into a context manager.

This allows for automatic closing of its internal file handle.
pull/716/head
Elliott Sales de Andrade 8 years ago
parent 4c71695e41
commit fe0aa7daff
  1. 34
      mesonbuild/scripts/depfixer.py

@ -115,11 +115,21 @@ class Elf(DataSizes):
self.bfile = bfile self.bfile = bfile
self.verbose = verbose self.verbose = verbose
self.bf = open(bfile, 'r+b') self.bf = open(bfile, 'r+b')
(self.ptrsize, self.is_le) = self.detect_elf_type() try:
super().__init__(self.ptrsize, self.is_le) (self.ptrsize, self.is_le) = self.detect_elf_type()
self.parse_header() super().__init__(self.ptrsize, self.is_le)
self.parse_sections() self.parse_header()
self.parse_dynamic() self.parse_sections()
self.parse_dynamic()
except:
self.bf.close()
raise
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.bf.close()
def detect_elf_type(self): def detect_elf_type(self):
data = self.bf.read(6) data = self.bf.read(6)
@ -308,13 +318,13 @@ def run(args):
print('Don\'t run this unless you know what you are doing.') print('Don\'t run this unless you know what you are doing.')
print('%s: <binary file> <prefix>' % sys.argv[0]) print('%s: <binary file> <prefix>' % sys.argv[0])
exit(1) exit(1)
e = Elf(args[0]) with Elf(args[0]) as e:
if len(args) == 1: if len(args) == 1:
e.print_rpath() e.print_rpath()
e.print_runpath() e.print_runpath()
else: else:
new_rpath = args[1] new_rpath = args[1]
e.fix_rpath(new_rpath) e.fix_rpath(new_rpath)
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save