summaryrefslogtreecommitdiff
path: root/python/__init__.py
diff options
context:
space:
mode:
authorPeter LaFosse <peter@vector35.com>2020-05-27 09:01:19 -0400
committerPeter LaFosse <peter@vector35.com>2020-05-27 10:26:29 -0400
commit4c9fb0b8b94bbd422dc0ea63c50c784624bf6be5 (patch)
tree546be963cb4227b4a4bf5fcfff64907eb1af5aa0 /python/__init__.py
parentfed4a230aab3b689b57ce02edd8948691f94baee (diff)
Add context manager for BinaryViews in the python API
Diffstat (limited to 'python/__init__.py')
-rw-r--r--python/__init__.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/python/__init__.py b/python/__init__.py
index c046d51e..82f591db 100644
--- a/python/__init__.py
+++ b/python/__init__.py
@@ -255,3 +255,27 @@ def get_memory_usage_info():
core.BNFreeMemoryUsageInfo(info, count.value)
return result
+
+class open_view(object):
+ """
+ Context Manager for BinaryView objects
+
+ :Example:
+ >>> from binaryninja import *
+ >>> with open_view("/bin/ls") as bv:
+ ... print(len(bv.functions))
+ ...
+ 128
+
+ """
+ def __init__(self, *args, **kwargs):
+ self.args = args
+ self.kwargs = kwargs
+
+ def __enter__(self):
+ self.view = BinaryViewType.get_view_of_file(*self.args, **self.kwargs)
+ return self.view
+
+ def __exit__(self, type, value, traceback):
+ if self.view is not None:
+ self.view.file.close() \ No newline at end of file