mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Combine close and unlink
This gives similar semantic on Windows and UNIX. On Windows closing unlinks automatically, so do the same on UNIX.
This commit is contained in:
parent
308d9ce73b
commit
54fd6080fc
@ -52,9 +52,8 @@ class SharedMemory:
|
|||||||
def __init__(
|
def __init__(
|
||||||
self, name: str = '', size: int = 0, readonly: bool = False,
|
self, name: str = '', size: int = 0, readonly: bool = False,
|
||||||
mode: int = stat.S_IREAD | stat.S_IWRITE,
|
mode: int = stat.S_IREAD | stat.S_IWRITE,
|
||||||
prefix: str = 'calibre-', unlink_on_exit: bool = False
|
prefix: str = 'calibre-'
|
||||||
):
|
):
|
||||||
self.unlink_on_exit = unlink_on_exit
|
|
||||||
if size < 0:
|
if size < 0:
|
||||||
raise TypeError("'size' must be a non-negative integer")
|
raise TypeError("'size' must be a non-negative integer")
|
||||||
if size and name:
|
if size and name:
|
||||||
@ -172,8 +171,6 @@ class SharedMemory:
|
|||||||
|
|
||||||
def __exit__(self, *a: object) -> None:
|
def __exit__(self, *a: object) -> None:
|
||||||
self.close()
|
self.close()
|
||||||
if self.unlink_on_exit:
|
|
||||||
self.unlink()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def size(self) -> int:
|
def size(self) -> int:
|
||||||
@ -205,6 +202,7 @@ class SharedMemory:
|
|||||||
if self._fd > -1:
|
if self._fd > -1:
|
||||||
os.close(self._fd)
|
os.close(self._fd)
|
||||||
self._fd = -1
|
self._fd = -1
|
||||||
|
self.unlink()
|
||||||
|
|
||||||
def unlink(self) -> None:
|
def unlink(self) -> None:
|
||||||
"""Requests that the underlying shared memory block be destroyed.
|
"""Requests that the underlying shared memory block be destroyed.
|
||||||
@ -212,11 +210,12 @@ class SharedMemory:
|
|||||||
In order to ensure proper cleanup of resources, unlink should be
|
In order to ensure proper cleanup of resources, unlink should be
|
||||||
called once (and only once) across all processes which have access
|
called once (and only once) across all processes which have access
|
||||||
to the shared memory block."""
|
to the shared memory block."""
|
||||||
if self._name and not iswindows:
|
if self._name:
|
||||||
try:
|
if not iswindows:
|
||||||
_posixshmem.shm_unlink(self._name)
|
try:
|
||||||
except FileNotFoundError:
|
_posixshmem.shm_unlink(self._name)
|
||||||
pass
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
self._name = ''
|
self._name = ''
|
||||||
|
|
||||||
|
|
||||||
@ -227,7 +226,7 @@ def find_tests():
|
|||||||
ae = unittest.TestCase.assertEqual
|
ae = unittest.TestCase.assertEqual
|
||||||
|
|
||||||
def test_shm(self):
|
def test_shm(self):
|
||||||
with SharedMemory(size=64, unlink_on_exit=True) as shm:
|
with SharedMemory(size=64) as shm:
|
||||||
q = b'test'
|
q = b'test'
|
||||||
shm.write_data_with_size(q)
|
shm.write_data_with_size(q)
|
||||||
self.ae(shm.tell(), shm.num_bytes_for_size + len(q))
|
self.ae(shm.tell(), shm.num_bytes_for_size + len(q))
|
||||||
@ -238,7 +237,6 @@ def find_tests():
|
|||||||
shm.flush()
|
shm.flush()
|
||||||
self.ae(s2.read(4), b'ABCD')
|
self.ae(s2.read(4), b'ABCD')
|
||||||
self.assertTrue(shm.name)
|
self.assertTrue(shm.name)
|
||||||
if not iswindows:
|
self.assertFalse(shm.name)
|
||||||
self.assertFalse(shm.name)
|
|
||||||
|
|
||||||
return unittest.defaultTestLoader.loadTestsFromTestCase(TestSHM)
|
return unittest.defaultTestLoader.loadTestsFromTestCase(TestSHM)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user