Skip to content

Commit

Permalink
py: Fix contextmanager issues
Browse files Browse the repository at this point in the history
This might need another look...
  • Loading branch information
tomba committed Sep 30, 2024
1 parent 31b56d4 commit 8ed576f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions py/rwmem/mappedregisterfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def __enter__(self):

def __exit__(self, exc_type, exc_value, exc_tb):
self._map.close()
del self._regblock
self._registers.clear()

def __getitem__(self, key: str):
if key not in self._registers:
Expand Down
14 changes: 10 additions & 4 deletions py/rwmem/registerfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,17 @@ def __init__(self, source: str | bytes | BinaryIO) -> None:
self._map = mmap.mmap(self.fd, 0, mmap.MAP_SHARED, access=mmap.ACCESS_COPY)
finally:
os.close(self.fd)

self._mmap = self._map
elif isinstance(source, bytes):
# XXX ctypes requires a writeable buffer...
self._map = bytearray(source)
self._mmap = None
else:
self.fd = source.fileno()
# ctypes requires a writeable mmap, so we use ACCESS_COPY
self._map = mmap.mmap(self.fd, 0, mmap.MAP_SHARED, access=mmap.ACCESS_COPY)
self._mmap = self._map

self.rfd = RegisterFileData.from_buffer(self._map)

Expand Down Expand Up @@ -243,15 +247,17 @@ def __init__(self, source: str | bytes | BinaryIO) -> None:
self._regblock_infos: dict[str, RegisterBlock | None] = dict.fromkeys(rb_names)

def close(self):
if self.fd:
self._map.close()
if self._mmap:
self._mmap.close()

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, exc_tb):
if self.fd:
self._map.close()
del self.rfd
self._regblock_infos.clear()
if self._mmap:
self._mmap.close()

@property
def num_blocks(self) -> int:
Expand Down
12 changes: 6 additions & 6 deletions py/tests/test_mmap_regs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
REGS_PATH = os.path.dirname(os.path.abspath(__file__)) + '/test.regs'
BIN_PATH = os.path.dirname(os.path.abspath(__file__)) + '/test.bin'

#class ContextManagerTests(unittest.TestCase):
# def test(self):
# with rw.RegisterFile(REGS_PATH) as rf:
# with rw.MappedRegisterBlock(BIN_PATH, rf['BLOCK1'],
# mode=rw.MapMode.Read) as map:
# self.assertEqual(map['REG1'].value, 0xf00dbaad)
class ContextManagerTests(unittest.TestCase):
def test(self):
with rw.RegisterFile(REGS_PATH) as rf:
with rw.MappedRegisterBlock(BIN_PATH, rf['BLOCK1'],
mode=rw.MapMode.Read) as map:
self.assertEqual(map['REG1'].value, 0xf00dbaad)

class MmapRegsTests(unittest.TestCase):
def setUp(self):
Expand Down
8 changes: 4 additions & 4 deletions py/tests/test_registerfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

REGS_PATH = os.path.dirname(os.path.abspath(__file__)) + '/test.regs'

#class ContextManagerTests(unittest.TestCase):
# def test(self):
# with rw.RegisterFile(REGS_PATH) as rf:
# pass
class ContextManagerTests(unittest.TestCase):
def test(self):
with rw.RegisterFile(REGS_PATH) as rf:
pass

class MmapRegsTests(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 8ed576f

Please sign in to comment.