summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/__init__.py24
-rw-r--r--python/binaryview.py6
2 files changed, 30 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
diff --git a/python/binaryview.py b/python/binaryview.py
index 9074b58c..994d9edd 100644
--- a/python/binaryview.py
+++ b/python/binaryview.py
@@ -1323,6 +1323,12 @@ class BinaryView(object):
self._notifications = {}
self._next_address = None # Do NOT try to access view before init() is called, use placeholder
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, traceback):
+ self.file.close()
+
def __del__(self):
for i in self.notifications.values():
i._unregister()