Skip to content

Commit 63209e4

Browse files
authored
Merge pull request #2042 from minrk/shadow-context
preserve context reference when shadowing Socket classes
2 parents dcf8963 + d29dc4a commit 63209e4

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

tests/test_socket.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ def test_shadow(self):
560560
assert s2._shadow_obj is s
561561
assert s.underlying != p.underlying
562562
assert s2.underlying == s.underlying
563+
assert s2.context is s.context
563564
s3 = zmq.Socket(s)
564565
assert s3._shadow_obj is s
565566
assert s3.underlying == s.underlying

zmq/sugar/socket.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def __init__(
133133
shadow: Socket | int = 0,
134134
copy_threshold: int | None = None,
135135
):
136+
shadow_context: zmq.Context | None = None
136137
if isinstance(ctx_or_socket, zmq.Socket):
137138
# positional Socket(other_socket)
138139
shadow = ctx_or_socket
@@ -145,6 +146,8 @@ def __init__(
145146
# hold a reference to the shadow object
146147
self._shadow_obj = shadow
147148
if not isinstance(shadow, int):
149+
if isinstance(shadow, zmq.Socket):
150+
shadow_context = shadow.context
148151
try:
149152
shadow = cast(int, shadow.underlying)
150153
except AttributeError:
@@ -159,6 +162,9 @@ def __init__(
159162
shadow=shadow_address,
160163
copy_threshold=copy_threshold,
161164
)
165+
if self._shadow_obj and shadow_context:
166+
# keep self.context reference if shadowing a Socket object
167+
self.context = shadow_context
162168

163169
try:
164170
socket_type = cast(int, self.get(zmq.TYPE))

0 commit comments

Comments
 (0)