@@ -59,18 +59,22 @@ class SlickQueue:
5959 """
6060
6161 def __init__ (self , * , name : Optional [str ] = None , size : Optional [int ] = None , element_size : Optional [int ] = None ):
62- # On Linux, POSIX shared memory names must start with /
63- # The C++ slick_queue library passes the name directly to shm_open(),
64- # which requires the / prefix. Python's SharedMemory strips it from .name,
65- # but we need to add it for C++ interop.
62+ # Store the original user-provided name (without / prefix)
63+ # Python's SharedMemory will add the / prefix on POSIX systems automatically.
64+ # We strip any leading / to avoid double-prefixing (//name) on POSIX systems.
6665 self .name = name
67- if self .name is not None and sys .platform != 'win32' and not self .name .startswith ('/' ):
68- self .name = '/' + self .name
66+ if self .name is not None and self .name .startswith ('/' ):
67+ # Strip leading / if user provided it - Python's SharedMemory will add it back on POSIX
68+ self .name = self .name [1 :]
6969
7070 # macOS has a 31-character limit for POSIX shared memory names (including leading /)
71- if self .name is not None and sys .platform == 'darwin' and len (self .name ) > 31 :
72- raise ValueError (f"Shared memory name '{ self .name } ' is { len (self .name )} characters, "
73- f"but macOS has a 31-character limit. Please use a shorter name." )
71+ # Check the length that will be used (with / prefix on POSIX systems)
72+ if self .name is not None and sys .platform == 'darwin' :
73+ # On macOS, Python's SharedMemory will prepend /, so check total length
74+ final_name = '/' + self .name
75+ if len (final_name ) > 31 :
76+ raise ValueError (f"Shared memory name '{ final_name } ' is { len (final_name )} characters, "
77+ f"but macOS has a 31-character limit. Please use a shorter name." )
7478
7579 self .use_shm = name is not None
7680 self ._shm : Optional [SharedMemory ] = None
@@ -206,17 +210,25 @@ def get_shm_name(self) -> Optional[str]:
206210 """
207211 Get the actual shared memory name for C++ interop.
208212
209- Returns the name with POSIX / prefix on Linux (required by C++ shm_open).
210- Python's SharedMemory.name property strips the / prefix, but this method
211- returns self.name which preserves it for C++ interop .
213+ Returns the name with POSIX / prefix (required by C++ shm_open).
214+ On POSIX systems (Linux/macOS), this returns the name with the / prefix.
215+ On Windows, it returns the name without modification .
212216
213217 Returns:
214218 The shared memory name that C++ code should use to open the queue.
215- On Linux , this will have the / prefix that shm_open() requires.
219+ On POSIX systems , this will have the / prefix that shm_open() requires.
216220 """
217- # Return self.name (which has / prefix on Linux) rather than self._shm.name
218- # (which has / stripped by Python)
219- return self .name
221+ if self ._shm is not None :
222+ # Use the actual name from SharedMemory (which has / prefix on POSIX)
223+ return self ._shm ._name
224+ elif self .name is not None :
225+ # If SharedMemory not created yet, construct the expected name
226+ # On POSIX, need to add / prefix; on Windows, use as-is
227+ if sys .platform != 'win32' :
228+ return '/' + self .name
229+ else :
230+ return self .name
231+ return None
220232
221233 # Public API mirroring C++ methods
222234 def reserve (self , n : int = 1 ) -> int :
0 commit comments