Skip to content

Commit b4f30ae

Browse files
authored
Implement Base.show() methods for Socket and Context (#255)
* Implement `Base.show()` methods for Socket and Context Socket example: ```julia-repl julia> x = ZMQ.Socket(ZMQ.REP) ZMQ.Socket(REP) julia> x ZMQ.Socket(REP) julia> ZMQ.bind(x, "tcp://localhost:*") julia> x ZMQ.Socket(REP, tcp://127.0.0.1:42099) julia> close(x) julia> x ZMQ.Socket([closed]) ``` Context example: ```julia-repl julia> x = ZMQ.Context() ZMQ.Context(WeakRef[]) julia> close(x) julia> x ZMQ.Context([closed]) ``` * fixup! Implement `Base.show()` methods for Socket and Context
1 parent 5c5a234 commit b4f30ae

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

docs/src/_changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ CurrentModule = ZMQ
77
This documents notable changes in ZMQ.jl. The format is based on [Keep a
88
Changelog](https://keepachangelog.com).
99

10+
## Unreleased
11+
12+
### Changed
13+
- Implemented `Base.show()` methods for [`Socket`](@ref) and [`Context`](@ref)
14+
for pretty-printing ([#255]).
15+
1016
## [v1.4.0] - 2024-11-30
1117

1218
### Added

src/context.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ function Context(f::Function, args...)
3838
end
3939
end
4040

41+
function Base.show(io::IO, ctx::Context)
42+
if isopen(ctx)
43+
print(io, Context, "($(getfield(ctx, :sockets)))")
44+
else
45+
print(io, Context, "() (closed)")
46+
end
47+
end
48+
4149
Base.unsafe_convert(::Type{Ptr{Cvoid}}, c::Context) = getfield(c, :data)
4250

4351
# define a global context that is initialized lazily

src/socket.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ function Socket(f::Function, args...)
4545
end
4646
end
4747

48+
const _socket_type_names = Dict(
49+
lib.ZMQ_PAIR => "PAIR",
50+
lib.ZMQ_PUB => "PUB",
51+
lib.ZMQ_SUB => "SUB",
52+
lib.ZMQ_REQ => "REQ",
53+
lib.ZMQ_REP => "REP",
54+
lib.ZMQ_DEALER => "DEALER",
55+
lib.ZMQ_ROUTER => "ROUTER",
56+
lib.ZMQ_PULL => "PULL",
57+
lib.ZMQ_PUSH => "PUSH",
58+
lib.ZMQ_XPUB => "XPUB",
59+
lib.ZMQ_XSUB => "XSUB"
60+
)
61+
62+
function Base.show(io::IO, socket::Socket)
63+
if isopen(socket)
64+
type_name = _socket_type_names[socket.type]
65+
last_endpoint = socket.last_endpoint == "\0" ? "" : ", $(socket.last_endpoint[1:end-1])"
66+
print(io, Socket, "($(type_name)$(last_endpoint))")
67+
else
68+
print(io, Socket, "() (closed)")
69+
end
70+
end
71+
4872
Base.unsafe_convert(::Type{Ptr{Cvoid}}, s::Socket) = getfield(s, :data)
4973

5074
"""

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ using ZMQ, Test
2020

2121
#try to create socket with expired context
2222
@test_throws StateError Socket(ctx, PUB)
23+
24+
# Smoke tests for Base.show()
25+
ctx = Context()
26+
@test repr(ctx) == "Context(WeakRef[])"
27+
close(ctx)
28+
@test repr(ctx) == "Context() (closed)"
2329
end
2430

2531
# This test is in its own function to keep it simple and try to trick Julia into
@@ -157,6 +163,14 @@ end
157163
ZMQ.close(ZMQ._context) # immediately close global context rather than waiting for exit
158164
@test !isopen(s1)
159165
@test !isopen(s2)
166+
167+
# Smoke tests for Base.show() in different Socket situations
168+
s1 = Socket(REP)
169+
@test repr(s1) == "Socket(REP)"
170+
ZMQ.bind(s1, "tcp://127.0.0.1:5555")
171+
@test repr(s1) == "Socket(REP, tcp://127.0.0.1:5555)"
172+
close(s1)
173+
@test repr(s1) == "Socket() (closed)"
160174
end
161175

162176
@testset "Message" begin

0 commit comments

Comments
 (0)