Skip to content

Commit 7e7f483

Browse files
committed
Update docs for constant enums
1 parent 1e30987 commit 7e7f483

File tree

9 files changed

+163
-25
lines changed

9 files changed

+163
-25
lines changed

docs/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cython>=0.29
2+
enum-tools[sphinx]>=0.9
23
gevent
3-
pygments==2.4.2
4+
pygments>=2.6
45
sphinx>=3.0.4
56
sphinx-rtd-theme==0.5.*
67
tornado

docs/source/api/zmq.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,42 @@ Polling
8888

8989
.. autofunction:: zmq.select
9090

91+
Constants
92+
---------
93+
94+
All libzmq constants are available as top-level attributes
95+
(``zmq.PUSH``, etc.),
96+
as well as via enums (``zmq.SocketType.PUSH``, etc.).
97+
98+
.. versionchanged:: 23
99+
100+
constants for unavailable socket types
101+
or draft features will always be defined in pyzmq,
102+
whether the features themselves are available or not.
103+
104+
.. versionadded:: 23
105+
106+
Each category of zmq constant is now available as an IntEnum.
107+
108+
.. autoenum:: SocketType
109+
110+
.. autoenum:: SocketOption
111+
112+
.. autoenum:: Flag
113+
114+
.. autoenum:: PollEvent
115+
116+
.. autoenum:: ContextOption
117+
118+
.. autoenum:: MessageOption
119+
120+
.. autoenum:: Event
121+
122+
.. autoenum:: SecurityMechanism
123+
124+
.. autoenum:: DeviceType
125+
126+
.. autoenum:: Errno
91127

92128
Exceptions
93129
----------

docs/source/changelog.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ Changes in PyZMQ
77
This is a coarse summary of changes in pyzmq versions.
88
For a full changelog, consult the `git log <https://github.com/zeromq/pyzmq/commits>`_.
99

10+
23.0.0
11+
======
12+
13+
Changes:
14+
15+
- all zmq constants are now available as Python enums
16+
(e.g. ``zmq.SocketType.PULL``, ``zmq.SocketOption.IDENTITY``),
17+
generated statically from zmq.h instead of at compile-time.
18+
This means that checks for the *presence* of a constant (`hasattr(zmq, 'RADIO')`)
19+
is not a valid check for the presence of a feature.
20+
This practice has never been robust, but it may have worked sometimes.
21+
Use direct checks via e.g. :func:`zmq.has` or :func:`zmq.zmq_version_info`.
22+
23+
Compatibility fixes:
24+
25+
- Remove all use of deprecated stdlib distutils
26+
1027
22.3.0
1128
======
1229

docs/source/conf.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
import string
1515
import sys
1616

17-
# If extensions (or modules to document with autodoc) are in another directory,
18-
# add these directories to sys.path here. If the directory is relative to the
19-
# documentation root, use os.path.abspath to make it absolute, like shown here.
20-
sys.path.insert(0, os.path.abspath('../sphinxext'))
21-
sys.path.append(os.path.abspath('../..'))
17+
# add repo root to sys.path
18+
here = os.path.dirname(__file__)
19+
sys.path.append(os.path.abspath(os.path.join(here, os.pardir, os.pardir)))
2220

2321
# set target libzmq version
2422
from buildutils.bundle import bundled_version
@@ -38,6 +36,7 @@
3836
'sphinx.ext.intersphinx',
3937
'sphinx.ext.napoleon',
4038
'sphinx_rtd_theme',
39+
'enum_tools.autoenum',
4140
]
4241

4342
# Add any paths that contain templates here, relative to this directory.

docs/source/eventloop.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Sockets created by this Context will return Futures from any would-be blocking m
4646
zmq.asyncio.install()
4747

4848
ctx = zmq.asyncio.Context()
49-
49+
5050
This step is no longer needed in pyzmq 17.
5151

5252

@@ -67,6 +67,12 @@ with :meth:`~.ZMQStream.on_send`.
6767
Futures and coroutines
6868
----------------------
6969

70+
.. note::
71+
72+
With recent Python (3.6) and tornado (5),
73+
there's no reason to use :mod:`zmq.eventloop.future`
74+
instead of the strictly-more-compatible :mod:`zmq.asyncio`.
75+
7076
PyZMQ 15 adds :mod:`zmq.eventloop.future`, containing a Socket subclass
7177
that returns :class:`~.tornado.concurrent.Future` objects for use in :mod:`tornado` coroutines.
7278
To use this API, import :class:`zmq.eventloop.future.Context`.
@@ -173,7 +179,7 @@ events off of the queue. You can specify to flush only recv events, only send ev
173179
any events, and you can specify a limit for how many events to flush in order to prevent
174180
starvation.
175181

176-
.. _Tornado: https://github.com/facebook/tornado
182+
.. _Tornado: https://github.com/tornadoweb/tornado
177183

178184
:func:`install()`
179185
-----------------

docs/source/morethanbindings.rst

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,46 @@ This affects the default options of any *new* sockets created after the assignme
8787
Socket options that do not apply to a socket (e.g. SUBSCRIBE on non-SUB sockets) will
8888
simply be ignored.
8989

90+
libzmq constants as Enums
91+
-------------------------
92+
93+
.. versionadded:: 23
94+
95+
libzmq constants are now available as Python enums,
96+
making it easier to enumerate socket options, etc.
97+
98+
Context managers
99+
----------------
100+
101+
.. versionadded:: 14
102+
Context/Sockets as context managers
103+
104+
.. versionadded:: 20
105+
bind/connect context managers
106+
107+
For more Pythonic resource management,
108+
contexts and sockets can be used as context managers.
109+
Just like standard-library socket and file methods,
110+
entering a context:
111+
112+
.. sourcecode:: python
113+
114+
import zmq
115+
with zmq.Context() as ctx:
116+
with ctx.socket(zmq.PUSH) as s:
117+
s.connect(url)
118+
s.send_multipart([b"message"])
119+
# exiting Socket context closes socket
120+
# exiting Context context terminates context
121+
122+
In addition, each bind/connect call may be used as a context:
123+
124+
.. sourcecode:: python
125+
126+
with socket.connect(url):
127+
s.send_multipart([b"message"])
128+
# exiting connect context calls socket.disconnect(url)
129+
90130

91131
Core Extensions
92132
---------------
@@ -105,12 +145,12 @@ and any object sent via those methods can be reconstructed with the
105145
:meth:`~.Socket.recv_json` and :meth:`~.Socket.recv_pyobj` methods. Unicode strings are
106146
other objects that are not unambiguously sendable over the wire, so we include
107147
:meth:`~.Socket.send_string` and :meth:`~.Socket.recv_string` that simply send bytes
108-
after encoding the message ('utf-8' is the default).
148+
after encoding the message ('utf-8' is the default).
109149

110150
.. seealso::
111151

112152
* :ref:`Further information <serialization>` on serialization in pyzmq.
113-
153+
114154
* :ref:`Our Unicode discussion <unicode>` for more information on the trials and
115155
tribulations of working with Unicode in a C extension while supporting Python 2 and 3.
116156

@@ -133,7 +173,7 @@ builtin :py:class:`~Queue.Queue` object), instantiating a MessageTracker takes a
133173
amount of time (10s of µs), so in situations instantiating many small messages, this can
134174
actually dominate performance. As a result, tracking is optional, via the ``track`` flag,
135175
which is optionally passed, always defaulting to ``False``, in each of the three places
136-
where a Frame object (the pyzmq object for wrapping a segment of a message) is
176+
where a Frame object (the pyzmq object for wrapping a segment of a message) is
137177
instantiated: The :class:`.Frame` constructor, and non-copying sends and receives.
138178

139179
A MessageTracker is very simple, and has just one method and one attribute. The property
@@ -156,9 +196,9 @@ included in PyZMQ itself:
156196

157197
* :ref:`zmq.log <logging>` : Logging handlers for hooking Python logging up to the
158198
network
159-
* :ref:`zmq.devices <devices>` : Custom devices and objects for running devices in the
199+
* :ref:`zmq.devices <devices>` : Custom devices and objects for running devices in the
160200
background
161-
* :ref:`zmq.eventloop <eventloop>` : The `Tornado`_ event loop, adapted for use
201+
* :ref:`zmq.eventloop <eventloop>` : The `Tornado`_ event loop, adapted for use
162202
with ØMQ sockets.
163203
* :ref:`zmq.ssh <ssh>` : Simple tools for tunneling zeromq connections via ssh.
164204

docs/source/serialization.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ methods can be reconstructed with the :meth:`~.Socket.recv_json` and
2222
:meth:`~.Socket.recv_pyobj` methods.
2323

2424

25-
These methods designed for convenience, not for performance, so developers who do want
25+
These methods designed for convenience, not for performance, so developers who want
2626
to emphasize performance should use their own serialized send/recv methods.
2727

2828
Using your own serialization
@@ -41,15 +41,16 @@ The following will send *compressed* pickles over the wire:
4141

4242
.. sourcecode:: python
4343

44-
import zlib, cPickle as pickle
44+
import pickle
45+
import zlib
4546

46-
def send_zipped_pickle(socket, obj, flags=0, protocol=-1):
47+
def send_zipped_pickle(socket, obj, flags=0, protocol=pickle.HIGHEST_PROTOCOL):
4748
"""pickle an object, and zip the pickle before sending it"""
4849
p = pickle.dumps(obj, protocol)
4950
z = zlib.compress(p)
5051
return socket.send(z, flags=flags)
5152

52-
def recv_zipped_pickle(socket, flags=0, protocol=-1):
53+
def recv_zipped_pickle(socket, flags=0):
5354
"""inverse of send_zipped_pickle"""
5455
z = socket.recv(flags)
5556
p = zlib.decompress(z)

zmq/constants.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99

1010
class Errno(IntEnum):
11+
"""libzmq error codes
12+
13+
.. versionadded:: 23
14+
"""
15+
1116
EAGAIN = errno.EAGAIN
1217
EFAULT = errno.EFAULT
1318
EINVAL = errno.EINVAL
@@ -65,7 +70,10 @@ class Errno(IntEnum):
6570

6671

6772
class ContextOption(IntEnum):
68-
"""Options for Context.get/set"""
73+
"""Options for Context.get/set
74+
75+
.. versionadded:: 23
76+
"""
6977

7078
IO_THREADS = 1
7179
MAX_SOCKETS = 2
@@ -80,7 +88,10 @@ class ContextOption(IntEnum):
8088

8189

8290
class SocketType(IntEnum):
83-
"""zmq socket types"""
91+
"""zmq socket types
92+
93+
.. versionadded:: 23
94+
"""
8495

8596
PAIR = 0
8697
PUB = 1
@@ -119,7 +130,10 @@ class _OptType(Enum):
119130

120131

121132
class SocketOption(IntEnum):
122-
"""Options for Socket.get/set"""
133+
"""Options for Socket.get/set
134+
135+
.. versionadded:: 23
136+
"""
123137

124138
_opt_type: str
125139

@@ -245,22 +259,33 @@ def __new__(cls, value, opt_type=_OptType.int):
245259

246260

247261
class MessageOption(IntEnum):
262+
"""Options on zmq.Frame objects
263+
264+
.. versionadded:: 23
265+
"""
266+
248267
MORE = 1
249268
SHARED = 3
250269
# Deprecated message options
251270
SRCFD = 2
252271

253272

254273
class Flag(IntFlag):
255-
"""Send/recv options"""
274+
"""Send/recv flags
275+
276+
.. versionadded:: 23
277+
"""
256278

257279
DONTWAIT = 1
258280
SNDMORE = 2
259281
NOBLOCK = DONTWAIT
260282

261283

262284
class SecurityMechanism(IntEnum):
263-
"""Security mechanisms (socket.get(zmq.MECHANISM))"""
285+
"""Security mechanisms (as returned by ``socket.get(zmq.MECHANISM)``)
286+
287+
.. versionadded:: 23
288+
"""
264289

265290
NULL = 0
266291
PLAIN = 1
@@ -269,7 +294,10 @@ class SecurityMechanism(IntEnum):
269294

270295

271296
class Event(IntFlag):
272-
"""Socket monitoring events"""
297+
"""Socket monitoring events
298+
299+
.. versionadded:: 23
300+
"""
273301

274302
@staticmethod
275303
def _global_name(name):
@@ -323,17 +351,27 @@ def _global_name(name):
323351
# DRAFT Socket monitoring events
324352
PIPES_STATS = 0x10000
325353
ALL_V1 = ALL
326-
ALL_V2 = ALL | PIPES_STATS
354+
ALL_V2 = ALL_V1 | PIPES_STATS
327355

328356

329357
class PollEvent(IntFlag):
358+
"""Which events to poll for in poll methods
359+
360+
.. versionadded: 23
361+
"""
362+
330363
POLLIN = 1
331364
POLLOUT = 2
332365
POLLERR = 4
333366
POLLPRI = 8
334367

335368

336369
class DeviceType(IntEnum):
370+
"""Device type constants for zmq.device
371+
372+
.. versionadded: 23
373+
"""
374+
337375
STREAMER = 1
338376
FORWARDER = 2
339377
QUEUE = 3

zmq/utils/jsonapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""JSON serialize to/from utf8 bytes
22
3-
..versionchanged:: 22.2
3+
.. versionchanged:: 22.2
44
Remove optional imports of different JSON implementations.
55
Now that we require recent Python, unconditionally use the standard library.
66
Custom JSON libraries can be used via custom serialization functions.

0 commit comments

Comments
 (0)