From fc85ec02c6a6279cfad7d83d3e90eea1fb04da78 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 24 Apr 2025 11:01:59 -0400 Subject: [PATCH 1/2] fix: SytemError in Semaphore init if subclassed in extension module --- a_sync/primitives/locks/semaphore.pyx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/a_sync/primitives/locks/semaphore.pyx b/a_sync/primitives/locks/semaphore.pyx index a707b415..70b2c112 100644 --- a/a_sync/primitives/locks/semaphore.pyx +++ b/a_sync/primitives/locks/semaphore.pyx @@ -101,7 +101,16 @@ cdef class Semaphore(_DebugDaemonMixin): ) from e.__cause__ # we need a constant to coerce to char* - cdef bytes encoded_name = (name or getattr(self, "__origin__", "")).encode("utf-8") + cdef bytes encoded_name + try: + encoded_name = (name or getattr(self, "__origin__", "")).encode("utf-8") + except SystemError: + if str(e) == "bad argument to internal function": + # This can happen if Semaphore is subclassed in an extension module. No problem. + encoded_name = b"" + else: + raise + cdef Py_ssize_t length = len(encoded_name) # Allocate memory for the char* and add 1 for the null character From bf1eda9af40a0e877dc669fd14269c5e68497575 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 24 Apr 2025 11:07:31 -0400 Subject: [PATCH 2/2] fix: UnboundLocalError --- a_sync/primitives/locks/semaphore.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/a_sync/primitives/locks/semaphore.pyx b/a_sync/primitives/locks/semaphore.pyx index 70b2c112..660bdd7e 100644 --- a/a_sync/primitives/locks/semaphore.pyx +++ b/a_sync/primitives/locks/semaphore.pyx @@ -104,9 +104,9 @@ cdef class Semaphore(_DebugDaemonMixin): cdef bytes encoded_name try: encoded_name = (name or getattr(self, "__origin__", "")).encode("utf-8") - except SystemError: + except SystemError as e: if str(e) == "bad argument to internal function": - # This can happen if Semaphore is subclassed in an extension module. No problem. + # This can happen if Semaphore is subclassed in a mypyc extension module due to conflict with getattr. No problem. encoded_name = b"" else: raise