From c7ceac11f88a36b4027d3db02e812645d5862c34 Mon Sep 17 00:00:00 2001 From: jmgoodman Date: Fri, 5 May 2023 14:05:23 +0200 Subject: [PATCH 1/3] brpylib edits for 'with' compatibility --- brpylib/brpylib.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/brpylib/brpylib.py b/brpylib/brpylib.py index ed07623..8d09b26 100644 --- a/brpylib/brpylib.py +++ b/brpylib/brpylib.py @@ -507,7 +507,10 @@ def __init__(self, datafile=""): self.datafile = datafile self.basic_header = {} self.extended_headers = [] - + + self.openfile() + + def openfile(self): # Run openfilecheck and open the file passed or allow user to browse to one self.datafile = openfilecheck( "rb", @@ -1003,6 +1006,13 @@ def close(self): name = self.datafile.name self.datafile.close() print("\n" + name.split("/")[-1] + " closed") + + # add "enter" and "exit" methods for compatibility with "with": + def __enter__(self): + pass + + def __exit__(self,*args): + self.close() class NsxFile: @@ -1016,7 +1026,10 @@ def __init__(self, datafile=""): self.datafile = datafile self.basic_header = {} self.extended_headers = [] - + + self.openfile() + + def openfile(self): # Run openfilecheck and open the file passed or allow user to browse to one self.datafile = openfilecheck( "rb", @@ -1720,3 +1733,10 @@ def close(self): name = self.datafile.name self.datafile.close() print("\n" + name.split("/")[-1] + " closed") + + # add "enter" and "exit" methods for compatibility with "with": + def __enter__(self): + pass + + def __exit__(self,*args): + self.close() From c5636dbb2c0b803acc1909266b52032c25ab3eda Mon Sep 17 00:00:00 2001 From: jmgoodman Date: Fri, 5 May 2023 14:15:04 +0200 Subject: [PATCH 2/3] Fixing a bug: __enter__ needs to return the file object --- brpylib/brpylib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brpylib/brpylib.py b/brpylib/brpylib.py index 8d09b26..58e01ad 100644 --- a/brpylib/brpylib.py +++ b/brpylib/brpylib.py @@ -1009,7 +1009,7 @@ def close(self): # add "enter" and "exit" methods for compatibility with "with": def __enter__(self): - pass + return self def __exit__(self,*args): self.close() @@ -1736,7 +1736,7 @@ def close(self): # add "enter" and "exit" methods for compatibility with "with": def __enter__(self): - pass + return self def __exit__(self,*args): self.close() From 236743657b7971c830828e2db2be7d8abf99a503 Mon Sep 17 00:00:00 2001 From: jmgoodman Date: Fri, 5 May 2023 14:18:27 +0200 Subject: [PATCH 3/3] Update brpylib.py --- brpylib/brpylib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brpylib/brpylib.py b/brpylib/brpylib.py index 58e01ad..b32bdc7 100644 --- a/brpylib/brpylib.py +++ b/brpylib/brpylib.py @@ -1009,7 +1009,7 @@ def close(self): # add "enter" and "exit" methods for compatibility with "with": def __enter__(self): - return self + return self # thankfully this returns a reference, not a copy def __exit__(self,*args): self.close() @@ -1736,7 +1736,7 @@ def close(self): # add "enter" and "exit" methods for compatibility with "with": def __enter__(self): - return self + return self # thankfully this returns a reference, not a copy def __exit__(self,*args): self.close()