summaryrefslogtreecommitdiff
path: root/zmq/utils
diff options
context:
space:
mode:
Diffstat (limited to 'zmq/utils')
-rw-r--r--zmq/utils/__init__.py0
-rw-r--r--zmq/utils/buffers.pxd313
-rw-r--r--zmq/utils/constant_names.py309
-rw-r--r--zmq/utils/garbage.py166
-rw-r--r--zmq/utils/getpid_compat.h6
-rw-r--r--zmq/utils/interop.py33
-rw-r--r--zmq/utils/ipcmaxlen.h21
-rw-r--r--zmq/utils/jsonapi.py59
-rw-r--r--zmq/utils/monitor.py68
-rw-r--r--zmq/utils/pyversion_compat.h25
-rw-r--r--zmq/utils/sixcerpt.py52
-rw-r--r--zmq/utils/strtypes.py45
-rw-r--r--zmq/utils/z85.py56
-rw-r--r--zmq/utils/zmq_compat.h73
-rw-r--r--zmq/utils/zmq_constants.h545
15 files changed, 1771 insertions, 0 deletions
diff --git a/zmq/utils/__init__.py b/zmq/utils/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/zmq/utils/__init__.py
diff --git a/zmq/utils/buffers.pxd b/zmq/utils/buffers.pxd
new file mode 100644
index 0000000..998aa55
--- /dev/null
+++ b/zmq/utils/buffers.pxd
@@ -0,0 +1,313 @@
+"""Python version-independent methods for C/Python buffers.
+
+This file was copied and adapted from mpi4py.
+
+Authors
+-------
+* MinRK
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2010 Lisandro Dalcin
+# All rights reserved.
+# Used under BSD License: http://www.opensource.org/licenses/bsd-license.php
+#
+# Retrieval:
+# Jul 23, 2010 18:00 PST (r539)
+# http://code.google.com/p/mpi4py/source/browse/trunk/src/MPI/asbuffer.pxi
+#
+# Modifications from original:
+# Copyright (c) 2010-2012 Brian Granger, Min Ragan-Kelley
+#
+# Distributed under the terms of the New BSD License. The full license is in
+# the file COPYING.BSD, distributed as part of this software.
+#-----------------------------------------------------------------------------
+
+
+#-----------------------------------------------------------------------------
+# Python includes.
+#-----------------------------------------------------------------------------
+
+# get version-independent aliases:
+cdef extern from "pyversion_compat.h":
+ pass
+
+# Python 3 buffer interface (PEP 3118)
+cdef extern from "Python.h":
+ int PY_MAJOR_VERSION
+ int PY_MINOR_VERSION
+ ctypedef int Py_ssize_t
+ ctypedef struct PyMemoryViewObject:
+ pass
+ ctypedef struct Py_buffer:
+ void *buf
+ Py_ssize_t len
+ int readonly
+ char *format
+ int ndim
+ Py_ssize_t *shape
+ Py_ssize_t *strides
+ Py_ssize_t *suboffsets
+ Py_ssize_t itemsize
+ void *internal
+ cdef enum:
+ PyBUF_SIMPLE
+ PyBUF_WRITABLE
+ PyBUF_FORMAT
+ PyBUF_ANY_CONTIGUOUS
+ int PyObject_CheckBuffer(object)
+ int PyObject_GetBuffer(object, Py_buffer *, int) except -1
+ void PyBuffer_Release(Py_buffer *)
+
+ int PyBuffer_FillInfo(Py_buffer *view, object obj, void *buf,
+ Py_ssize_t len, int readonly, int infoflags) except -1
+ object PyMemoryView_FromBuffer(Py_buffer *info)
+
+ object PyMemoryView_FromObject(object)
+
+# Python 2 buffer interface (legacy)
+cdef extern from "Python.h":
+ ctypedef void const_void "const void"
+ Py_ssize_t Py_END_OF_BUFFER
+ int PyObject_CheckReadBuffer(object)
+ int PyObject_AsReadBuffer (object, const_void **, Py_ssize_t *) except -1
+ int PyObject_AsWriteBuffer(object, void **, Py_ssize_t *) except -1
+
+ object PyBuffer_FromMemory(void *ptr, Py_ssize_t s)
+ object PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t s)
+
+ object PyBuffer_FromObject(object, Py_ssize_t offset, Py_ssize_t size)
+ object PyBuffer_FromReadWriteObject(object, Py_ssize_t offset, Py_ssize_t size)
+
+
+#-----------------------------------------------------------------------------
+# asbuffer: C buffer from python object
+#-----------------------------------------------------------------------------
+
+
+cdef inline int memoryview_available():
+ return PY_MAJOR_VERSION >= 3 or (PY_MAJOR_VERSION >=2 and PY_MINOR_VERSION >= 7)
+
+cdef inline int oldstyle_available():
+ return PY_MAJOR_VERSION < 3
+
+
+cdef inline int check_buffer(object ob):
+ """Version independent check for whether an object is a buffer.
+
+ Parameters
+ ----------
+ object : object
+ Any Python object
+
+ Returns
+ -------
+ int : 0 if no buffer interface, 3 if newstyle buffer interface, 2 if oldstyle.
+ """
+ if PyObject_CheckBuffer(ob):
+ return 3
+ if oldstyle_available():
+ return PyObject_CheckReadBuffer(ob) and 2
+ return 0
+
+
+cdef inline object asbuffer(object ob, int writable, int format,
+ void **base, Py_ssize_t *size,
+ Py_ssize_t *itemsize):
+ """Turn an object into a C buffer in a Python version-independent way.
+
+ Parameters
+ ----------
+ ob : object
+ The object to be turned into a buffer.
+ Must provide a Python Buffer interface
+ writable : int
+ Whether the resulting buffer should be allowed to write
+ to the object.
+ format : int
+ The format of the buffer. See Python buffer docs.
+ base : void **
+ The pointer that will be used to store the resulting C buffer.
+ size : Py_ssize_t *
+ The size of the buffer(s).
+ itemsize : Py_ssize_t *
+ The size of an item, if the buffer is non-contiguous.
+
+ Returns
+ -------
+ An object describing the buffer format. Generally a str, such as 'B'.
+ """
+
+ cdef void *bptr = NULL
+ cdef Py_ssize_t blen = 0, bitemlen = 0
+ cdef Py_buffer view
+ cdef int flags = PyBUF_SIMPLE
+ cdef int mode = 0
+
+ bfmt = None
+
+ mode = check_buffer(ob)
+ if mode == 0:
+ raise TypeError("%r does not provide a buffer interface."%ob)
+
+ if mode == 3:
+ flags = PyBUF_ANY_CONTIGUOUS
+ if writable:
+ flags |= PyBUF_WRITABLE
+ if format:
+ flags |= PyBUF_FORMAT
+ PyObject_GetBuffer(ob, &view, flags)
+ bptr = view.buf
+ blen = view.len
+ if format:
+ if view.format != NULL:
+ bfmt = view.format
+ bitemlen = view.itemsize
+ PyBuffer_Release(&view)
+ else: # oldstyle
+ if writable:
+ PyObject_AsWriteBuffer(ob, &bptr, &blen)
+ else:
+ PyObject_AsReadBuffer(ob, <const_void **>&bptr, &blen)
+ if format:
+ try: # numpy.ndarray
+ dtype = ob.dtype
+ bfmt = dtype.char
+ bitemlen = dtype.itemsize
+ except AttributeError:
+ try: # array.array
+ bfmt = ob.typecode
+ bitemlen = ob.itemsize
+ except AttributeError:
+ if isinstance(ob, bytes):
+ bfmt = b"B"
+ bitemlen = 1
+ else:
+ # nothing found
+ bfmt = None
+ bitemlen = 0
+ if base: base[0] = <void *>bptr
+ if size: size[0] = <Py_ssize_t>blen
+ if itemsize: itemsize[0] = <Py_ssize_t>bitemlen
+
+ if PY_MAJOR_VERSION >= 3 and bfmt is not None:
+ return bfmt.decode('ascii')
+ return bfmt
+
+
+cdef inline object asbuffer_r(object ob, void **base, Py_ssize_t *size):
+ """Wrapper for standard calls to asbuffer with a readonly buffer."""
+ asbuffer(ob, 0, 0, base, size, NULL)
+ return ob
+
+
+cdef inline object asbuffer_w(object ob, void **base, Py_ssize_t *size):
+ """Wrapper for standard calls to asbuffer with a writable buffer."""
+ asbuffer(ob, 1, 0, base, size, NULL)
+ return ob
+
+#------------------------------------------------------------------------------
+# frombuffer: python buffer/view from C buffer
+#------------------------------------------------------------------------------
+
+
+cdef inline object frombuffer_3(void *ptr, Py_ssize_t s, int readonly):
+ """Python 3 version of frombuffer.
+
+ This is the Python 3 model, but will work on Python >= 2.6. Currently,
+ we use it only on >= 3.0.
+ """
+ cdef Py_buffer pybuf
+ cdef Py_ssize_t *shape = [s]
+ cdef str astr=""
+ PyBuffer_FillInfo(&pybuf, astr, ptr, s, readonly, PyBUF_SIMPLE)
+ pybuf.format = "B"
+ pybuf.shape = shape
+ return PyMemoryView_FromBuffer(&pybuf)
+
+
+cdef inline object frombuffer_2(void *ptr, Py_ssize_t s, int readonly):
+ """Python 2 version of frombuffer.
+
+ This must be used for Python <= 2.6, but we use it for all Python < 3.
+ """
+
+ if oldstyle_available():
+ if readonly:
+ return PyBuffer_FromMemory(ptr, s)
+ else:
+ return PyBuffer_FromReadWriteMemory(ptr, s)
+ else:
+ raise NotImplementedError("Old style buffers not available.")
+
+
+cdef inline object frombuffer(void *ptr, Py_ssize_t s, int readonly):
+ """Create a Python Buffer/View of a C array.
+
+ Parameters
+ ----------
+ ptr : void *
+ Pointer to the array to be copied.
+ s : size_t
+ Length of the buffer.
+ readonly : int
+ whether the resulting object should be allowed to write to the buffer.
+
+ Returns
+ -------
+ Python Buffer/View of the C buffer.
+ """
+ # oldstyle first priority for now
+ if oldstyle_available():
+ return frombuffer_2(ptr, s, readonly)
+ else:
+ return frombuffer_3(ptr, s, readonly)
+
+
+cdef inline object frombuffer_r(void *ptr, Py_ssize_t s):
+ """Wrapper for readonly view frombuffer."""
+ return frombuffer(ptr, s, 1)
+
+
+cdef inline object frombuffer_w(void *ptr, Py_ssize_t s):
+ """Wrapper for writable view frombuffer."""
+ return frombuffer(ptr, s, 0)
+
+#------------------------------------------------------------------------------
+# viewfromobject: python buffer/view from python object, refcounts intact
+# frombuffer(asbuffer(obj)) would lose track of refs
+#------------------------------------------------------------------------------
+
+cdef inline object viewfromobject(object obj, int readonly):
+ """Construct a Python Buffer/View object from another Python object.
+
+ This work in a Python version independent manner.
+
+ Parameters
+ ----------
+ obj : object
+ The input object to be cast as a buffer
+ readonly : int
+ Whether the result should be prevented from overwriting the original.
+
+ Returns
+ -------
+ Buffer/View of the original object.
+ """
+ if not memoryview_available():
+ if readonly:
+ return PyBuffer_FromObject(obj, 0, Py_END_OF_BUFFER)
+ else:
+ return PyBuffer_FromReadWriteObject(obj, 0, Py_END_OF_BUFFER)
+ else:
+ return PyMemoryView_FromObject(obj)
+
+
+cdef inline object viewfromobject_r(object obj):
+ """Wrapper for readonly viewfromobject."""
+ return viewfromobject(obj, 1)
+
+
+cdef inline object viewfromobject_w(object obj):
+ """Wrapper for writable viewfromobject."""
+ return viewfromobject(obj, 0)
diff --git a/zmq/utils/constant_names.py b/zmq/utils/constant_names.py
new file mode 100644
index 0000000..fe3bdbc
--- /dev/null
+++ b/zmq/utils/constant_names.py
@@ -0,0 +1,309 @@
+"""0MQ Constant names"""
+
+# Copyright (C) PyZMQ Developers
+# Distributed under the terms of the Modified BSD License.
+
+#-----------------------------------------------------------------------------
+# Python module level constants
+#-----------------------------------------------------------------------------
+
+# dictionaries of constants new or removed in particular versions
+
+new_in = {
+ (2,2,0) : [
+ 'RCVTIMEO',
+ 'SNDTIMEO',
+ ],
+ (3,2,2) : [
+ # errnos
+ 'EMSGSIZE',
+ 'EAFNOSUPPORT',
+ 'ENETUNREACH',
+ 'ECONNABORTED',
+ 'ECONNRESET',
+ 'ENOTCONN',
+ 'ETIMEDOUT',
+ 'EHOSTUNREACH',
+ 'ENETRESET',
+
+ 'IO_THREADS',
+ 'MAX_SOCKETS',
+ 'IO_THREADS_DFLT',
+ 'MAX_SOCKETS_DFLT',
+
+ 'ROUTER_BEHAVIOR',
+ 'ROUTER_MANDATORY',
+ 'FAIL_UNROUTABLE',
+ 'TCP_KEEPALIVE',
+ 'TCP_KEEPALIVE_CNT',
+ 'TCP_KEEPALIVE_IDLE',
+ 'TCP_KEEPALIVE_INTVL',
+ 'DELAY_ATTACH_ON_CONNECT',
+ 'XPUB_VERBOSE',
+
+ 'EVENT_CONNECTED',
+ 'EVENT_CONNECT_DELAYED',
+ 'EVENT_CONNECT_RETRIED',
+ 'EVENT_LISTENING',
+ 'EVENT_BIND_FAILED',
+ 'EVENT_ACCEPTED',
+ 'EVENT_ACCEPT_FAILED',
+ 'EVENT_CLOSED',
+ 'EVENT_CLOSE_FAILED',
+ 'EVENT_DISCONNECTED',
+ 'EVENT_ALL',
+ ],
+ (4,0,0) : [
+ # socket types
+ 'STREAM',
+
+ # socket opts
+ 'IMMEDIATE',
+ 'ROUTER_RAW',
+ 'IPV6',
+ 'MECHANISM',
+ 'PLAIN_SERVER',
+ 'PLAIN_USERNAME',
+ 'PLAIN_PASSWORD',
+ 'CURVE_SERVER',
+ 'CURVE_PUBLICKEY',
+ 'CURVE_SECRETKEY',
+ 'CURVE_SERVERKEY',
+ 'PROBE_ROUTER',
+ 'REQ_RELAXED',
+ 'REQ_CORRELATE',
+ 'CONFLATE',
+ 'ZAP_DOMAIN',
+
+ # security
+ 'NULL',
+ 'PLAIN',
+ 'CURVE',
+
+ # events
+ 'EVENT_MONITOR_STOPPED',
+ ],
+ (4,1,0) : [
+ # socket opts
+ 'ROUTER_HANDOVER',
+ 'TOS',
+ 'IPC_FILTER_PID',
+ 'IPC_FILTER_UID',
+ 'IPC_FILTER_GID',
+ 'CONNECT_RID',
+ ],
+}
+
+
+removed_in = {
+ (3,2,2) : [
+ 'UPSTREAM',
+ 'DOWNSTREAM',
+
+ 'HWM',
+ 'SWAP',
+ 'MCAST_LOOP',
+ 'RECOVERY_IVL_MSEC',
+ ]
+}
+
+# collections of zmq constant names based on their role
+# base names have no specific use
+# opt names are validated in get/set methods of various objects
+
+base_names = [
+ # base
+ 'VERSION',
+ 'VERSION_MAJOR',
+ 'VERSION_MINOR',
+ 'VERSION_PATCH',
+ 'NOBLOCK',
+ 'DONTWAIT',
+
+ 'POLLIN',
+ 'POLLOUT',
+ 'POLLERR',
+
+ 'SNDMORE',
+
+ 'STREAMER',
+ 'FORWARDER',
+ 'QUEUE',
+
+ 'IO_THREADS_DFLT',
+ 'MAX_SOCKETS_DFLT',
+
+ # socktypes
+ 'PAIR',
+ 'PUB',
+ 'SUB',
+ 'REQ',
+ 'REP',
+ 'DEALER',
+ 'ROUTER',
+ 'PULL',
+ 'PUSH',
+ 'XPUB',
+ 'XSUB',
+ 'UPSTREAM',
+ 'DOWNSTREAM',
+ 'STREAM',
+
+ # events
+ 'EVENT_CONNECTED',
+ 'EVENT_CONNECT_DELAYED',
+ 'EVENT_CONNECT_RETRIED',
+ 'EVENT_LISTENING',
+ 'EVENT_BIND_FAILED',
+ 'EVENT_ACCEPTED',
+ 'EVENT_ACCEPT_FAILED',
+ 'EVENT_CLOSED',
+ 'EVENT_CLOSE_FAILED',
+ 'EVENT_DISCONNECTED',
+ 'EVENT_ALL',
+ 'EVENT_MONITOR_STOPPED',
+
+ # security
+ 'NULL',
+ 'PLAIN',
+ 'CURVE',
+
+ ## ERRNO
+ # Often used (these are alse in errno.)
+ 'EAGAIN',
+ 'EINVAL',
+ 'EFAULT',
+ 'ENOMEM',
+ 'ENODEV',
+ 'EMSGSIZE',
+ 'EAFNOSUPPORT',
+ 'ENETUNREACH',
+ 'ECONNABORTED',
+ 'ECONNRESET',
+ 'ENOTCONN',
+ 'ETIMEDOUT',
+ 'EHOSTUNREACH',
+ 'ENETRESET',
+
+ # For Windows compatability
+ 'HAUSNUMERO',
+ 'ENOTSUP',
+ 'EPROTONOSUPPORT',
+ 'ENOBUFS',
+ 'ENETDOWN',
+ 'EADDRINUSE',
+ 'EADDRNOTAVAIL',
+ 'ECONNREFUSED',
+ 'EINPROGRESS',
+ 'ENOTSOCK',
+
+ # 0MQ Native
+ 'EFSM',
+ 'ENOCOMPATPROTO',
+ 'ETERM',
+ 'EMTHREAD',
+]
+
+int64_sockopt_names = [
+ 'AFFINITY',
+ 'MAXMSGSIZE',
+
+ # sockopts removed in 3.0.0
+ 'HWM',
+ 'SWAP',
+ 'MCAST_LOOP',
+ 'RECOVERY_IVL_MSEC',
+]
+
+bytes_sockopt_names = [
+ 'IDENTITY',
+ 'SUBSCRIBE',
+ 'UNSUBSCRIBE',
+ 'LAST_ENDPOINT',
+ 'TCP_ACCEPT_FILTER',
+
+ 'PLAIN_USERNAME',
+ 'PLAIN_PASSWORD',
+
+ 'CURVE_PUBLICKEY',
+ 'CURVE_SECRETKEY',
+ 'CURVE_SERVERKEY',
+ 'ZAP_DOMAIN',
+ 'CONNECT_RID',
+]
+
+int_sockopt_names = [
+ # sockopts
+ 'RECONNECT_IVL_MAX',
+
+ # sockopts new in 2.2.0
+ 'SNDTIMEO',
+ 'RCVTIMEO',
+
+ # new in 3.x
+ 'SNDHWM',
+ 'RCVHWM',
+ 'MULTICAST_HOPS',
+ 'IPV4ONLY',
+
+ 'ROUTER_BEHAVIOR',
+ 'TCP_KEEPALIVE',
+ 'TCP_KEEPALIVE_CNT',
+ 'TCP_KEEPALIVE_IDLE',
+ 'TCP_KEEPALIVE_INTVL',
+ 'DELAY_ATTACH_ON_CONNECT',
+ 'XPUB_VERBOSE',
+
+ 'FD',
+ 'EVENTS',
+ 'TYPE',
+ 'LINGER',
+ 'RECONNECT_IVL',
+ 'BACKLOG',
+
+ 'ROUTER_MANDATORY',
+ 'FAIL_UNROUTABLE',
+
+ 'ROUTER_RAW',
+ 'IMMEDIATE',
+ 'IPV6',
+ 'MECHANISM',
+ 'PLAIN_SERVER',
+ 'CURVE_SERVER',
+ 'PROBE_ROUTER',
+ 'REQ_RELAXED',
+ 'REQ_CORRELATE',
+ 'CONFLATE',
+ 'ROUTER_HANDOVER',
+ 'TOS',
+ 'IPC_FILTER_PID',
+ 'IPC_FILTER_UID',
+ 'IPC_FILTER_GID',
+]
+
+switched_sockopt_names = [
+ 'RATE',
+ 'RECOVERY_IVL',
+ 'SNDBUF',
+ 'RCVBUF',
+ 'RCVMORE',
+]
+
+ctx_opt_names = [
+ 'IO_THREADS',
+ 'MAX_SOCKETS',
+]
+
+msg_opt_names = [
+ 'MORE',
+]
+
+all_names = (
+ base_names + ctx_opt_names + msg_opt_names +
+ bytes_sockopt_names + int_sockopt_names + int64_sockopt_names + switched_sockopt_names
+)
+
+def no_prefix(name):
+ """does the given constant have a ZMQ_ prefix?"""
+ return name.startswith('E') and not name.startswith('EVENT')
+
diff --git a/zmq/utils/garbage.py b/zmq/utils/garbage.py
new file mode 100644
index 0000000..e77cb00
--- /dev/null
+++ b/zmq/utils/garbage.py
@@ -0,0 +1,166 @@
+"""Garbage collection thread for representing zmq refcount of Python objects
+used in zero-copy sends.
+"""
+
+# Copyright (C) PyZMQ Developers
+# Distributed under the terms of the Modified BSD License.
+
+
+import atexit
+import struct
+
+from os import getpid
+from collections import namedtuple
+from threading import Thread, Event, Lock
+import warnings
+
+import zmq
+
+
+gcref = namedtuple('gcref', ['obj', 'event'])
+
+class GarbageCollectorThread(Thread):
+ """Thread in which garbage collection actually happens."""
+ def __init__(self, gc):
+ super(GarbageCollectorThread, self).__init__()
+ self.gc = gc
+ self.daemon = True
+ self.pid = getpid()
+ self.ready = Event()
+
+ def run(self):
+ s = self.gc.context.socket(zmq.PULL)
+ s.linger = 0
+ s.bind(self.gc.url)
+ self.ready.set()
+
+ while True:
+ # detect fork
+ if getpid is None or getpid() != self.pid:
+ return
+ msg = s.recv()
+ if msg == b'DIE':
+ break
+ fmt = 'L' if len(msg) == 4 else 'Q'
+ key = struct.unpack(fmt, msg)[0]
+ tup = self.gc.refs.pop(key, None)
+ if tup and tup.event:
+ tup.event.set()
+ del tup
+ s.close()
+
+
+class GarbageCollector(object):
+ """PyZMQ Garbage Collector
+
+ Used for representing the reference held by libzmq during zero-copy sends.
+ This object holds a dictionary, keyed by Python id,
+ of the Python objects whose memory are currently in use by zeromq.
+
+ When zeromq is done with the memory, it sends a message on an inproc PUSH socket
+ containing the packed size_t (32 or 64-bit unsigned int),
+ which is the key in the dict.
+ When the PULL socket in the gc thread receives that message,
+ the reference is popped from the dict,
+ and any tracker events that should be signaled fire.
+ """
+
+ refs = None
+ _context = None
+ _lock = None
+ url = "inproc://pyzmq.gc.01"
+
+ def __init__(self, context=None):
+ super(GarbageCollector, self).__init__()
+ self.refs = {}
+ self.pid = None
+ self.thread = None
+ self._context = context
+ self._lock = Lock()
+ self._stay_down = False
+ atexit.register(self._atexit)
+
+ @property
+ def context(self):
+ if self._context is None:
+ self._context = zmq.Context()
+ return self._context
+
+ @context.setter
+ def context(self, ctx):
+ if self.is_alive():
+ if self.refs:
+ warnings.warn("Replacing gc context while gc is running", RuntimeWarning)
+ self.stop()
+ self._context = ctx
+
+ def _atexit(self):
+ """atexit callback
+
+ sets _stay_down flag so that gc doesn't try to start up again in other atexit handlers
+ """
+ self._stay_down = True
+ self.stop()
+
+ def stop(self):
+ """stop the garbage-collection thread"""
+ if not self.is_alive():
+ return
+ push = self.context.socket(zmq.PUSH)
+ push.connect(self.url)
+ push.send(b'DIE')
+ push.close()
+ self.thread.join()
+ self.context.term()
+ self.refs.clear()
+
+ def start(self):
+ """Start a new garbage collection thread.
+
+ Creates a new zmq Context used for garbage collection.
+ Under most circumstances, this will only be called once per process.
+ """
+ self.pid = getpid()
+ self.refs = {}
+ self.thread = GarbageCollectorThread(self)
+ self.thread.start()
+ self.thread.ready.wait()
+
+ def is_alive(self):
+ """Is the garbage collection thread currently running?
+
+ Includes checks for process shutdown or fork.
+ """
+ if (getpid is None or
+ getpid() != self.pid or
+ self.thread is None or
+ not self.thread.is_alive()
+ ):
+ return False
+ return True
+
+ def store(self, obj, event=None):
+ """store an object and (optionally) event for zero-copy"""
+ if not self.is_alive():
+ if self._stay_down:
+ return 0
+ # safely start the gc thread
+ # use lock and double check,
+ # so we don't start multiple threads
+ with self._lock:
+ if not self.is_alive():
+ self.start()
+ tup = gcref(obj, event)
+ theid = id(tup)
+ self.refs[theid] = tup
+ return theid
+
+ def __del__(self):
+ if not self.is_alive():
+ return
+ try:
+ self.stop()
+ except Exception as e:
+ raise (e)
+
+gc = GarbageCollector()
diff --git a/zmq/utils/getpid_compat.h b/zmq/utils/getpid_compat.h
new file mode 100644
index 0000000..47ce90f
--- /dev/null
+++ b/zmq/utils/getpid_compat.h
@@ -0,0 +1,6 @@
+#ifdef _WIN32
+ #include <process.h>
+ #define getpid _getpid
+#else
+ #include <unistd.h>
+#endif
diff --git a/zmq/utils/interop.py b/zmq/utils/interop.py
new file mode 100644
index 0000000..26c0196
--- /dev/null
+++ b/zmq/utils/interop.py
@@ -0,0 +1,33 @@
+"""Utils for interoperability with other libraries.
+
+Just CFFI pointer casting for now.
+"""
+
+# Copyright (C) PyZMQ Developers
+# Distributed under the terms of the Modified BSD License.
+
+
+try:
+ long
+except NameError:
+ long = int # Python 3
+
+
+def cast_int_addr(n):
+ """Cast an address to a Python int
+
+ This could be a Python integer or a CFFI pointer
+ """
+ if isinstance(n, (int, long)):
+ return n
+ try:
+ import cffi
+ except ImportError:
+ pass
+ else:
+ # from pyzmq, this is an FFI void *
+ ffi = cffi.FFI()
+ if isinstance(n, ffi.CData):
+ return int(ffi.cast("size_t", n))
+
+ raise ValueError("Cannot cast %r to int" % n)
diff --git a/zmq/utils/ipcmaxlen.h b/zmq/utils/ipcmaxlen.h
new file mode 100644
index 0000000..7218db7
--- /dev/null
+++ b/zmq/utils/ipcmaxlen.h
@@ -0,0 +1,21 @@
+/*
+
+Platform-independant detection of IPC path max length
+
+Copyright (c) 2012 Godefroid Chapelle
+
+Distributed under the terms of the New BSD License. The full license is in
+the file COPYING.BSD, distributed as part of this software.
+ */
+
+#if defined(HAVE_SYS_UN_H)
+#include "sys/un.h"
+int get_ipc_path_max_len(void) {
+ struct sockaddr_un *dummy;
+ return sizeof(dummy->sun_path) - 1;
+}
+#else
+int get_ipc_path_max_len(void) {
+ return 0;
+}
+#endif
diff --git a/zmq/utils/jsonapi.py b/zmq/utils/jsonapi.py
new file mode 100644
index 0000000..865ca6d
--- /dev/null
+++ b/zmq/utils/jsonapi.py
@@ -0,0 +1,59 @@
+"""Priority based json library imports.
+
+Always serializes to bytes instead of unicode for zeromq compatibility
+on Python 2 and 3.
+
+Use ``jsonapi.loads()`` and ``jsonapi.dumps()`` for guaranteed symmetry.
+
+Priority: ``simplejson`` > ``jsonlib2`` > stdlib ``json``
+
+``jsonapi.loads/dumps`` provide kwarg-compatibility with stdlib json.
+
+``jsonapi.jsonmod`` will be the module of the actual underlying implementation.
+"""
+
+# Copyright (C) PyZMQ Developers
+# Distributed under the terms of the Modified BSD License.
+
+from zmq.utils.strtypes import bytes, unicode
+
+jsonmod = None
+
+priority = ['simplejson', 'jsonlib2', 'json']
+for mod in priority:
+ try:
+ jsonmod = __import__(mod)
+ except ImportError:
+ pass
+ else:
+ break
+
+def dumps(o, **kwargs):
+ """Serialize object to JSON bytes (utf-8).
+
+ See jsonapi.jsonmod.dumps for details on kwargs.
+ """
+
+ if 'separators' not in kwargs:
+ kwargs['separators'] = (',', ':')
+
+ s = jsonmod.dumps(o, **kwargs)
+
+ if isinstance(s, unicode):
+ s = s.encode('utf8')
+
+ return s
+
+def loads(s, **kwargs):
+ """Load object from JSON bytes (utf-8).
+
+ See jsonapi.jsonmod.loads for details on kwargs.
+ """
+
+ if str is unicode and isinstance(s, bytes):
+ s = s.decode('utf8')
+
+ return jsonmod.loads(s, **kwargs)
+
+__all__ = ['jsonmod', 'dumps', 'loads']
+
diff --git a/zmq/utils/monitor.py b/zmq/utils/monitor.py
new file mode 100644
index 0000000..734d54b
--- /dev/null
+++ b/zmq/utils/monitor.py
@@ -0,0 +1,68 @@
+# -*- coding: utf-8 -*-
+"""Module holding utility and convenience functions for zmq event monitoring."""
+
+# Copyright (C) PyZMQ Developers
+# Distributed under the terms of the Modified BSD License.
+
+import struct
+import zmq
+from zmq.error import _check_version
+
+def parse_monitor_message(msg):
+ """decode zmq_monitor event messages.
+
+ Parameters
+ ----------
+ msg : list(bytes)
+ zmq multipart message that has arrived on a monitor PAIR socket.
+
+ First frame is::
+
+ 16 bit event id
+ 32 bit event value
+ no padding
+
+ Second frame is the endpoint as a bytestring
+
+ Returns
+ -------
+ event : dict
+ event description as dict with the keys `event`, `value`, and `endpoint`.
+ """
+
+ if len(msg) != 2 or len(msg[0]) != 6:
+ raise RuntimeError("Invalid event message format: %s" % msg)
+ event = {}
+ event['event'], event['value'] = struct.unpack("=hi", msg[0])
+ event['endpoint'] = msg[1]
+ return event
+
+def recv_monitor_message(socket, flags=0):
+ """Receive and decode the given raw message from the monitoring socket and return a dict.
+
+ Requires libzmq ≥ 4.0
+
+ The returned dict will have the following entries:
+ event : int, the event id as described in libzmq.zmq_socket_monitor
+ value : int, the event value associated with the event, see libzmq.zmq_socket_monitor
+ endpoint : string, the affected endpoint
+
+ Parameters
+ ----------
+ socket : zmq PAIR socket
+ The PAIR socket (created by other.get_monitor_socket()) on which to recv the message
+ flags : bitfield (int)
+ standard zmq recv flags
+
+ Returns
+ -------
+ event : dict
+ event description as dict with the keys `event`, `value`, and `endpoint`.
+ """
+ _check_version((4,0), 'libzmq event API')
+ # will always return a list
+ msg = socket.recv_multipart(flags)
+ # 4.0-style event API
+ return parse_monitor_message(msg)
+
+__all__ = ['parse_monitor_message', 'recv_monitor_message']
diff --git a/zmq/utils/pyversion_compat.h b/zmq/utils/pyversion_compat.h
new file mode 100644
index 0000000..fac0904
--- /dev/null
+++ b/zmq/utils/pyversion_compat.h
@@ -0,0 +1,25 @@
+#include "Python.h"
+
+#if PY_VERSION_HEX < 0x02070000
+ #define PyMemoryView_FromBuffer(info) (PyErr_SetString(PyExc_NotImplementedError, \
+ "new buffer interface is not available"), (PyObject *)NULL)
+ #define PyMemoryView_FromObject(object) (PyErr_SetString(PyExc_NotImplementedError, \
+ "new buffer interface is not available"), (PyObject *)NULL)
+#endif
+
+#if PY_VERSION_HEX >= 0x03000000
+ // for buffers
+ #define Py_END_OF_BUFFER ((Py_ssize_t) 0)
+
+ #define PyObject_CheckReadBuffer(object) (0)
+
+ #define PyBuffer_FromMemory(ptr, s) (PyErr_SetString(PyExc_NotImplementedError, \
+ "old buffer interface is not available"), (PyObject *)NULL)
+ #define PyBuffer_FromReadWriteMemory(ptr, s) (PyErr_SetString(PyExc_NotImplementedError, \
+ "old buffer interface is not available"), (PyObject *)NULL)
+ #define PyBuffer_FromObject(object, offset, size) (PyErr_SetString(PyExc_NotImplementedError, \
+ "old buffer interface is not available"), (PyObject *)NULL)
+ #define PyBuffer_FromReadWriteObject(object, offset, size) (PyErr_SetString(PyExc_NotImplementedError, \
+ "old buffer interface is not available"), (PyObject *)NULL)
+
+#endif
diff --git a/zmq/utils/sixcerpt.py b/zmq/utils/sixcerpt.py
new file mode 100644
index 0000000..5492fd5
--- /dev/null
+++ b/zmq/utils/sixcerpt.py
@@ -0,0 +1,52 @@
+"""Excerpts of six.py"""
+
+# Copyright (C) 2010-2014 Benjamin Peterson
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+import sys
+
+# Useful for very coarse version differentiation.
+PY2 = sys.version_info[0] == 2
+PY3 = sys.version_info[0] == 3
+
+if PY3:
+
+ def reraise(tp, value, tb=None):
+ if value.__traceback__ is not tb:
+ raise value.with_traceback(tb)
+ raise value
+
+else:
+ def exec_(_code_, _globs_=None, _locs_=None):
+ """Execute code in a namespace."""
+ if _globs_ is None:
+ frame = sys._getframe(1)
+ _globs_ = frame.f_globals
+ if _locs_ is None:
+ _locs_ = frame.f_locals
+ del frame
+ elif _locs_ is None:
+ _locs_ = _globs_
+ exec("""exec _code_ in _globs_, _locs_""")
+
+
+ exec_("""def reraise(tp, value, tb=None):
+ raise tp, value, tb
+""")
diff --git a/zmq/utils/strtypes.py b/zmq/utils/strtypes.py
new file mode 100644
index 0000000..548410d
--- /dev/null
+++ b/zmq/utils/strtypes.py
@@ -0,0 +1,45 @@
+"""Declare basic string types unambiguously for various Python versions.
+
+Authors
+-------
+* MinRK
+"""
+
+# Copyright (C) PyZMQ Developers
+# Distributed under the terms of the Modified BSD License.
+
+import sys
+
+if sys.version_info[0] >= 3:
+ bytes = bytes
+ unicode = str
+ basestring = (bytes, unicode)
+else:
+ unicode = unicode
+ bytes = str
+ basestring = basestring
+
+def cast_bytes(s, encoding='utf8', errors='strict'):
+ """cast unicode or bytes to bytes"""
+ if isinstance(s, bytes):
+ return s
+ elif isinstance(s, unicode):
+ return s.encode(encoding, errors)
+ else:
+ raise TypeError("Expected unicode or bytes, got %r" % s)
+
+def cast_unicode(s, encoding='utf8', errors='strict'):
+ """cast bytes or unicode to unicode"""
+ if isinstance(s, bytes):
+ return s.decode(encoding, errors)
+ elif isinstance(s, unicode):
+ return s
+ else:
+ raise TypeError("Expected unicode or bytes, got %r" % s)
+
+# give short 'b' alias for cast_bytes, so that we can use fake b('stuff')
+# to simulate b'stuff'
+b = asbytes = cast_bytes
+u = cast_unicode
+
+__all__ = ['asbytes', 'bytes', 'unicode', 'basestring', 'b', 'u', 'cast_bytes', 'cast_unicode']
diff --git a/zmq/utils/z85.py b/zmq/utils/z85.py
new file mode 100644
index 0000000..1bb1784
--- /dev/null
+++ b/zmq/utils/z85.py
@@ -0,0 +1,56 @@
+"""Python implementation of Z85 85-bit encoding
+
+Z85 encoding is a plaintext encoding for a bytestring interpreted as 32bit integers.
+Since the chunks are 32bit, a bytestring must be a multiple of 4 bytes.
+See ZMQ RFC 32 for details.
+
+
+"""
+
+# Copyright (C) PyZMQ Developers
+# Distributed under the terms of the Modified BSD License.
+
+import sys
+import struct
+
+PY3 = sys.version_info[0] >= 3
+# Z85CHARS is the base 85 symbol table
+Z85CHARS = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
+# Z85MAP maps integers in [0,84] to the appropriate character in Z85CHARS
+Z85MAP = dict([(c, idx) for idx, c in enumerate(Z85CHARS)])
+
+_85s = [ 85**i for i in range(5) ][::-1]
+
+def encode(rawbytes):
+ """encode raw bytes into Z85"""
+ # Accepts only byte arrays bounded to 4 bytes
+ if len(rawbytes) % 4:
+ raise ValueError("length must be multiple of 4, not %i" % len(rawbytes))
+
+ nvalues = len(rawbytes) / 4
+
+ values = struct.unpack('>%dI' % nvalues, rawbytes)
+ encoded = []
+ for v in values:
+ for offset in _85s:
+ encoded.append(Z85CHARS[(v // offset) % 85])
+
+ # In Python 3, encoded is a list of integers (obviously?!)
+ if PY3:
+ return bytes(encoded)
+ else:
+ return b''.join(encoded)
+
+def decode(z85bytes):
+ """decode Z85 bytes to raw bytes"""
+ if len(z85bytes) % 5:
+ raise ValueError("Z85 length must be multiple of 5, not %i" % len(z85bytes))
+
+ nvalues = len(z85bytes) / 5
+ values = []
+ for i in range(0, len(z85bytes), 5):
+ value = 0
+ for j, offset in enumerate(_85s):
+ value += Z85MAP[z85bytes[i+j]] * offset
+ values.append(value)
+ return struct.pack('>%dI' % nvalues, *values)
diff --git a/zmq/utils/zmq_compat.h b/zmq/utils/zmq_compat.h
new file mode 100644
index 0000000..a949e95
--- /dev/null
+++ b/zmq/utils/zmq_compat.h
@@ -0,0 +1,73 @@
+//-----------------------------------------------------------------------------
+// Copyright (c) 2010 Brian Granger, Min Ragan-Kelley
+//
+// Distributed under the terms of the New BSD License. The full license is in
+// the file COPYING.BSD, distributed as part of this software.
+//-----------------------------------------------------------------------------
+
+#if defined(_MSC_VER)
+#define pyzmq_int64_t __int64
+#else
+#include <stdint.h>
+#define pyzmq_int64_t int64_t
+#endif
+
+
+#include "zmq.h"
+// version compatibility for constants:
+#include "zmq_constants.h"
+
+#define _missing (-1)
+
+
+// define fd type (from libzmq's fd.hpp)
+#ifdef _WIN32
+ #ifdef _MSC_VER && _MSC_VER <= 1400
+ #define ZMQ_FD_T UINT_PTR
+ #else
+ #define ZMQ_FD_T SOCKET
+ #endif
+#else
+ #define ZMQ_FD_T int
+#endif
+
+// use unambiguous aliases for zmq_send/recv functions
+
+#if ZMQ_VERSION_MAJOR >= 4
+// nothing to remove
+#else
+ #define zmq_curve_keypair(z85_public_key, z85_secret_key) _missing
+#endif
+
+#if ZMQ_VERSION_MAJOR >= 3
+ #define zmq_sendbuf zmq_send
+ #define zmq_recvbuf zmq_recv
+
+ // 3.x deprecations - these symbols haven't been removed,
+ // but let's protect against their planned removal
+ #define zmq_device(device_type, isocket, osocket) _missing
+ #define zmq_init(io_threads) ((void*)NULL)
+ #define zmq_term zmq_ctx_destroy
+#else
+ #define zmq_ctx_set(ctx, opt, val) _missing
+ #define zmq_ctx_get(ctx, opt) _missing
+ #define zmq_ctx_destroy zmq_term
+ #define zmq_ctx_new() ((void*)NULL)
+
+ #define zmq_proxy(a,b,c) _missing
+
+ #define zmq_disconnect(s, addr) _missing
+ #define zmq_unbind(s, addr) _missing
+
+ #define zmq_msg_more(msg) _missing
+ #define zmq_msg_get(msg, opt) _missing
+ #define zmq_msg_set(msg, opt, val) _missing
+ #define zmq_msg_send(msg, s, flags) zmq_send(s, msg, flags)
+ #define zmq_msg_recv(msg, s, flags) zmq_recv(s, msg, flags)
+
+ #define zmq_sendbuf(s, buf, len, flags) _missing
+ #define zmq_recvbuf(s, buf, len, flags) _missing
+
+ #define zmq_socket_monitor(s, addr, flags) _missing
+
+#endif
diff --git a/zmq/utils/zmq_constants.h b/zmq/utils/zmq_constants.h
new file mode 100644
index 0000000..830ac58
--- /dev/null
+++ b/zmq/utils/zmq_constants.h
@@ -0,0 +1,545 @@
+#ifndef _PYZMQ_CONSTANT_DEFS
+#define _PYZMQ_CONSTANT_DEFS
+
+#ifndef ZMQ_VERSION
+ #define ZMQ_VERSION (-1)
+#endif
+
+#ifndef ZMQ_VERSION_MAJOR
+ #define ZMQ_VERSION_MAJOR (-1)
+#endif
+
+#ifndef ZMQ_VERSION_MINOR
+ #define ZMQ_VERSION_MINOR (-1)
+#endif
+
+#ifndef ZMQ_VERSION_PATCH
+ #define ZMQ_VERSION_PATCH (-1)
+#endif
+
+#ifndef ZMQ_NOBLOCK
+ #define ZMQ_NOBLOCK (-1)
+#endif
+
+#ifndef ZMQ_DONTWAIT
+ #define ZMQ_DONTWAIT (-1)
+#endif
+
+#ifndef ZMQ_POLLIN
+ #define ZMQ_POLLIN (-1)
+#endif
+
+#ifndef ZMQ_POLLOUT
+ #define ZMQ_POLLOUT (-1)
+#endif
+
+#ifndef ZMQ_POLLERR
+ #define ZMQ_POLLERR (-1)
+#endif
+
+#ifndef ZMQ_SNDMORE
+ #define ZMQ_SNDMORE (-1)
+#endif
+
+#ifndef ZMQ_STREAMER
+ #define ZMQ_STREAMER (-1)
+#endif
+
+#ifndef ZMQ_FORWARDER
+ #define ZMQ_FORWARDER (-1)
+#endif
+
+#ifndef ZMQ_QUEUE
+ #define ZMQ_QUEUE (-1)
+#endif
+
+#ifndef ZMQ_IO_THREADS_DFLT
+ #define ZMQ_IO_THREADS_DFLT (-1)
+#endif
+
+#ifndef ZMQ_MAX_SOCKETS_DFLT
+ #define ZMQ_MAX_SOCKETS_DFLT (-1)
+#endif
+
+#ifndef ZMQ_PAIR
+ #define ZMQ_PAIR (-1)
+#endif
+
+#ifndef ZMQ_PUB
+ #define ZMQ_PUB (-1)
+#endif
+
+#ifndef ZMQ_SUB
+ #define ZMQ_SUB (-1)
+#endif
+
+#ifndef ZMQ_REQ
+ #define ZMQ_REQ (-1)
+#endif
+
+#ifndef ZMQ_REP
+ #define ZMQ_REP (-1)
+#endif
+
+#ifndef ZMQ_DEALER
+ #define ZMQ_DEALER (-1)
+#endif
+
+#ifndef ZMQ_ROUTER
+ #define ZMQ_ROUTER (-1)
+#endif
+
+#ifndef ZMQ_PULL
+ #define ZMQ_PULL (-1)
+#endif
+
+#ifndef ZMQ_PUSH
+ #define ZMQ_PUSH (-1)
+#endif
+
+#ifndef ZMQ_XPUB
+ #define ZMQ_XPUB (-1)
+#endif
+
+#ifndef ZMQ_XSUB
+ #define ZMQ_XSUB (-1)
+#endif
+
+#ifndef ZMQ_UPSTREAM
+ #define ZMQ_UPSTREAM (-1)
+#endif
+
+#ifndef ZMQ_DOWNSTREAM
+ #define ZMQ_DOWNSTREAM (-1)
+#endif
+
+#ifndef ZMQ_STREAM
+ #define ZMQ_STREAM (-1)
+#endif
+
+#ifndef ZMQ_EVENT_CONNECTED
+ #define ZMQ_EVENT_CONNECTED (-1)
+#endif
+
+#ifndef ZMQ_EVENT_CONNECT_DELAYED
+ #define ZMQ_EVENT_CONNECT_DELAYED (-1)
+#endif
+
+#ifndef ZMQ_EVENT_CONNECT_RETRIED
+ #define ZMQ_EVENT_CONNECT_RETRIED (-1)
+#endif
+
+#ifndef ZMQ_EVENT_LISTENING
+ #define ZMQ_EVENT_LISTENING (-1)
+#endif
+
+#ifndef ZMQ_EVENT_BIND_FAILED
+ #define ZMQ_EVENT_BIND_FAILED (-1)
+#endif
+
+#ifndef ZMQ_EVENT_ACCEPTED
+ #define ZMQ_EVENT_ACCEPTED (-1)
+#endif
+
+#ifndef ZMQ_EVENT_ACCEPT_FAILED
+ #define ZMQ_EVENT_ACCEPT_FAILED (-1)
+#endif
+
+#ifndef ZMQ_EVENT_CLOSED
+ #define ZMQ_EVENT_CLOSED (-1)
+#endif
+
+#ifndef ZMQ_EVENT_CLOSE_FAILED
+ #define ZMQ_EVENT_CLOSE_FAILED (-1)
+#endif
+
+#ifndef ZMQ_EVENT_DISCONNECTED
+ #define ZMQ_EVENT_DISCONNECTED (-1)
+#endif
+
+#ifndef ZMQ_EVENT_ALL
+ #define ZMQ_EVENT_ALL (-1)
+#endif
+
+#ifndef ZMQ_EVENT_MONITOR_STOPPED
+ #define ZMQ_EVENT_MONITOR_STOPPED (-1)
+#endif
+
+#ifndef ZMQ_NULL
+ #define ZMQ_NULL (-1)
+#endif
+
+#ifndef ZMQ_PLAIN
+ #define ZMQ_PLAIN (-1)
+#endif
+
+#ifndef ZMQ_CURVE
+ #define ZMQ_CURVE (-1)
+#endif
+
+#ifndef EAGAIN
+ #define EAGAIN (-1)
+#endif
+
+#ifndef EINVAL
+ #define EINVAL (-1)
+#endif
+
+#ifndef EFAULT
+ #define EFAULT (-1)
+#endif
+
+#ifndef ENOMEM
+ #define ENOMEM (-1)
+#endif
+
+#ifndef ENODEV
+ #define ENODEV (-1)
+#endif
+
+#ifndef EMSGSIZE
+ #define EMSGSIZE (-1)
+#endif
+
+#ifndef EAFNOSUPPORT
+ #define EAFNOSUPPORT (-1)
+#endif
+
+#ifndef ENETUNREACH
+ #define ENETUNREACH (-1)
+#endif
+
+#ifndef ECONNABORTED
+ #define ECONNABORTED (-1)
+#endif
+
+#ifndef ECONNRESET
+ #define ECONNRESET (-1)
+#endif
+
+#ifndef ENOTCONN
+ #define ENOTCONN (-1)
+#endif
+
+#ifndef ETIMEDOUT
+ #define ETIMEDOUT (-1)
+#endif
+
+#ifndef EHOSTUNREACH
+ #define EHOSTUNREACH (-1)
+#endif
+
+#ifndef ENETRESET
+ #define ENETRESET (-1)
+#endif
+
+#ifndef ZMQ_HAUSNUMERO
+ #define ZMQ_HAUSNUMERO (-1)
+#endif
+
+#ifndef ENOTSUP
+ #define ENOTSUP (-1)
+#endif
+
+#ifndef EPROTONOSUPPORT
+ #define EPROTONOSUPPORT (-1)
+#endif
+
+#ifndef ENOBUFS
+ #define ENOBUFS (-1)
+#endif
+
+#ifndef ENETDOWN
+ #define ENETDOWN (-1)
+#endif
+
+#ifndef EADDRINUSE
+ #define EADDRINUSE (-1)
+#endif
+
+#ifndef EADDRNOTAVAIL
+ #define EADDRNOTAVAIL (-1)
+#endif
+
+#ifndef ECONNREFUSED
+ #define ECONNREFUSED (-1)
+#endif
+
+#ifndef EINPROGRESS
+ #define EINPROGRESS (-1)
+#endif
+
+#ifndef ENOTSOCK
+ #define ENOTSOCK (-1)
+#endif
+
+#ifndef EFSM
+ #define EFSM (-1)
+#endif
+
+#ifndef ENOCOMPATPROTO
+ #define ENOCOMPATPROTO (-1)
+#endif
+
+#ifndef ETERM
+ #define ETERM (-1)
+#endif
+
+#ifndef EMTHREAD
+ #define EMTHREAD (-1)
+#endif
+
+#ifndef ZMQ_IO_THREADS
+ #define ZMQ_IO_THREADS (-1)
+#endif
+
+#ifndef ZMQ_MAX_SOCKETS
+ #define ZMQ_MAX_SOCKETS (-1)
+#endif
+
+#ifndef ZMQ_MORE
+ #define ZMQ_MORE (-1)
+#endif
+
+#ifndef ZMQ_IDENTITY
+ #define ZMQ_IDENTITY (-1)
+#endif
+
+#ifndef ZMQ_SUBSCRIBE
+ #define ZMQ_SUBSCRIBE (-1)
+#endif
+
+#ifndef ZMQ_UNSUBSCRIBE
+ #define ZMQ_UNSUBSCRIBE (-1)
+#endif
+
+#ifndef ZMQ_LAST_ENDPOINT
+ #define ZMQ_LAST_ENDPOINT (-1)
+#endif
+
+#ifndef ZMQ_TCP_ACCEPT_FILTER
+ #define ZMQ_TCP_ACCEPT_FILTER (-1)
+#endif
+
+#ifndef ZMQ_PLAIN_USERNAME
+ #define ZMQ_PLAIN_USERNAME (-1)
+#endif
+
+#ifndef ZMQ_PLAIN_PASSWORD
+ #define ZMQ_PLAIN_PASSWORD (-1)
+#endif
+
+#ifndef ZMQ_CURVE_PUBLICKEY
+ #define ZMQ_CURVE_PUBLICKEY (-1)
+#endif
+
+#ifndef ZMQ_CURVE_SECRETKEY
+ #define ZMQ_CURVE_SECRETKEY (-1)
+#endif
+
+#ifndef ZMQ_CURVE_SERVERKEY
+ #define ZMQ_CURVE_SERVERKEY (-1)
+#endif
+
+#ifndef ZMQ_ZAP_DOMAIN
+ #define ZMQ_ZAP_DOMAIN (-1)
+#endif
+
+#ifndef ZMQ_CONNECT_RID
+ #define ZMQ_CONNECT_RID (-1)
+#endif
+
+#ifndef ZMQ_RECONNECT_IVL_MAX
+ #define ZMQ_RECONNECT_IVL_MAX (-1)
+#endif
+
+#ifndef ZMQ_SNDTIMEO
+ #define ZMQ_SNDTIMEO (-1)
+#endif
+
+#ifndef ZMQ_RCVTIMEO
+ #define ZMQ_RCVTIMEO (-1)
+#endif
+
+#ifndef ZMQ_SNDHWM
+ #define ZMQ_SNDHWM (-1)
+#endif
+
+#ifndef ZMQ_RCVHWM
+ #define ZMQ_RCVHWM (-1)
+#endif
+
+#ifndef ZMQ_MULTICAST_HOPS
+ #define ZMQ_MULTICAST_HOPS (-1)
+#endif
+
+#ifndef ZMQ_IPV4ONLY
+ #define ZMQ_IPV4ONLY (-1)
+#endif
+
+#ifndef ZMQ_ROUTER_BEHAVIOR
+ #define ZMQ_ROUTER_BEHAVIOR (-1)
+#endif
+
+#ifndef ZMQ_TCP_KEEPALIVE
+ #define ZMQ_TCP_KEEPALIVE (-1)
+#endif
+
+#ifndef ZMQ_TCP_KEEPALIVE_CNT
+ #define ZMQ_TCP_KEEPALIVE_CNT (-1)
+#endif
+
+#ifndef ZMQ_TCP_KEEPALIVE_IDLE
+ #define ZMQ_TCP_KEEPALIVE_IDLE (-1)
+#endif
+
+#ifndef ZMQ_TCP_KEEPALIVE_INTVL
+ #define ZMQ_TCP_KEEPALIVE_INTVL (-1)
+#endif
+
+#ifndef ZMQ_DELAY_ATTACH_ON_CONNECT
+ #define ZMQ_DELAY_ATTACH_ON_CONNECT (-1)
+#endif
+
+#ifndef ZMQ_XPUB_VERBOSE
+ #define ZMQ_XPUB_VERBOSE (-1)
+#endif
+
+#ifndef ZMQ_FD
+ #define ZMQ_FD (-1)
+#endif
+
+#ifndef ZMQ_EVENTS
+ #define ZMQ_EVENTS (-1)
+#endif
+
+#ifndef ZMQ_TYPE
+ #define ZMQ_TYPE (-1)
+#endif
+
+#ifndef ZMQ_LINGER
+ #define ZMQ_LINGER (-1)
+#endif
+
+#ifndef ZMQ_RECONNECT_IVL
+ #define ZMQ_RECONNECT_IVL (-1)
+#endif
+
+#ifndef ZMQ_BACKLOG
+ #define ZMQ_BACKLOG (-1)
+#endif
+
+#ifndef ZMQ_ROUTER_MANDATORY
+ #define ZMQ_ROUTER_MANDATORY (-1)
+#endif
+
+#ifndef ZMQ_FAIL_UNROUTABLE
+ #define ZMQ_FAIL_UNROUTABLE (-1)
+#endif
+
+#ifndef ZMQ_ROUTER_RAW
+ #define ZMQ_ROUTER_RAW (-1)
+#endif
+
+#ifndef ZMQ_IMMEDIATE
+ #define ZMQ_IMMEDIATE (-1)
+#endif
+
+#ifndef ZMQ_IPV6
+ #define ZMQ_IPV6 (-1)
+#endif
+
+#ifndef ZMQ_MECHANISM
+ #define ZMQ_MECHANISM (-1)
+#endif
+
+#ifndef ZMQ_PLAIN_SERVER
+ #define ZMQ_PLAIN_SERVER (-1)
+#endif
+
+#ifndef ZMQ_CURVE_SERVER
+ #define ZMQ_CURVE_SERVER (-1)
+#endif
+
+#ifndef ZMQ_PROBE_ROUTER
+ #define ZMQ_PROBE_ROUTER (-1)
+#endif
+
+#ifndef ZMQ_REQ_RELAXED
+ #define ZMQ_REQ_RELAXED (-1)
+#endif
+
+#ifndef ZMQ_REQ_CORRELATE
+ #define ZMQ_REQ_CORRELATE (-1)
+#endif
+
+#ifndef ZMQ_CONFLATE
+ #define ZMQ_CONFLATE (-1)
+#endif
+
+#ifndef ZMQ_ROUTER_HANDOVER
+ #define ZMQ_ROUTER_HANDOVER (-1)
+#endif
+
+#ifndef ZMQ_TOS
+ #define ZMQ_TOS (-1)
+#endif
+
+#ifndef ZMQ_IPC_FILTER_PID
+ #define ZMQ_IPC_FILTER_PID (-1)
+#endif
+
+#ifndef ZMQ_IPC_FILTER_UID
+ #define ZMQ_IPC_FILTER_UID (-1)
+#endif
+
+#ifndef ZMQ_IPC_FILTER_GID
+ #define ZMQ_IPC_FILTER_GID (-1)
+#endif
+
+#ifndef ZMQ_AFFINITY
+ #define ZMQ_AFFINITY (-1)
+#endif
+
+#ifndef ZMQ_MAXMSGSIZE
+ #define ZMQ_MAXMSGSIZE (-1)
+#endif
+
+#ifndef ZMQ_HWM
+ #define ZMQ_HWM (-1)
+#endif
+
+#ifndef ZMQ_SWAP
+ #define ZMQ_SWAP (-1)
+#endif
+
+#ifndef ZMQ_MCAST_LOOP
+ #define ZMQ_MCAST_LOOP (-1)
+#endif
+
+#ifndef ZMQ_RECOVERY_IVL_MSEC
+ #define ZMQ_RECOVERY_IVL_MSEC (-1)
+#endif
+
+#ifndef ZMQ_RATE
+ #define ZMQ_RATE (-1)
+#endif
+
+#ifndef ZMQ_RECOVERY_IVL
+ #define ZMQ_RECOVERY_IVL (-1)
+#endif
+
+#ifndef ZMQ_SNDBUF
+ #define ZMQ_SNDBUF (-1)
+#endif
+
+#ifndef ZMQ_RCVBUF
+ #define ZMQ_RCVBUF (-1)
+#endif
+
+#ifndef ZMQ_RCVMORE
+ #define ZMQ_RCVMORE (-1)
+#endif
+
+
+#endif // ifndef _PYZMQ_CONSTANT_DEFS