File tree Expand file tree Collapse file tree 4 files changed +52
-0
lines changed Expand file tree Collapse file tree 4 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,12 @@ CurrentModule = ZMQ
7
7
This documents notable changes in ZMQ.jl. The format is based on [ Keep a
8
8
Changelog] ( https://keepachangelog.com ) .
9
9
10
+ ## Unreleased
11
+
12
+ ### Changed
13
+ - Implemented ` Base.show() ` methods for [ ` Socket ` ] ( @ref ) and [ ` Context ` ] ( @ref )
14
+ for pretty-printing ([ #255 ] ).
15
+
10
16
## [ v1.4.0] - 2024-11-30
11
17
12
18
### Added
Original file line number Diff line number Diff line change @@ -38,6 +38,14 @@ function Context(f::Function, args...)
38
38
end
39
39
end
40
40
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
+
41
49
Base. unsafe_convert (:: Type{Ptr{Cvoid}} , c:: Context ) = getfield (c, :data )
42
50
43
51
# define a global context that is initialized lazily
Original file line number Diff line number Diff line change @@ -45,6 +45,30 @@ function Socket(f::Function, args...)
45
45
end
46
46
end
47
47
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
+
48
72
Base. unsafe_convert (:: Type{Ptr{Cvoid}} , s:: Socket ) = getfield (s, :data )
49
73
50
74
"""
Original file line number Diff line number Diff line change @@ -20,6 +20,12 @@ using ZMQ, Test
20
20
21
21
# try to create socket with expired context
22
22
@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)"
23
29
end
24
30
25
31
# This test is in its own function to keep it simple and try to trick Julia into
157
163
ZMQ. close (ZMQ. _context) # immediately close global context rather than waiting for exit
158
164
@test ! isopen (s1)
159
165
@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)"
160
174
end
161
175
162
176
@testset " Message" begin
You can’t perform that action at this time.
0 commit comments