~ubuntu-branches/ubuntu/saucy/pyzmq/saucy-proposed

« back to all changes in this revision

Viewing changes to zmq/core/_poll.pyx

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-05-12 13:59:20 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20130512135920-1d2md9w425iq3sb6
Tags: 13.1.0-1
* New upstream release built with zeromq3 (Closes: #698830)
  - drop all patches
* workaround-gevent.patch: workaround issue with gevent < 1.0
* noncopysend-test.patch: avoid uninitialized values in tests
* update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
# Imports
24
24
#-----------------------------------------------------------------------------
25
25
 
26
 
from libzmq cimport zmq_poll, zmq_pollitem_t, allocate, ZMQ_VERSION_MAJOR
 
26
from libc.stdlib cimport free, malloc
 
27
 
 
28
from libzmq cimport zmq_pollitem_t, ZMQ_VERSION_MAJOR
 
29
from libzmq cimport zmq_poll as zmq_poll_c
27
30
from socket cimport Socket
28
31
 
29
32
import sys
30
 
from zmq.core.error import ZMQError
 
33
 
 
34
from zmq.core.checkrc cimport _check_rc
31
35
 
32
36
#-----------------------------------------------------------------------------
33
37
# Polling related methods
39
43
else:
40
44
    int_t = (int,long)
41
45
 
42
 
def _poll(sockets, long timeout=-1):
43
 
    """_poll(sockets, timeout=-1)
 
46
def zmq_poll(sockets, long timeout=-1):
 
47
    """zmq_poll(sockets, timeout=-1)
44
48
 
45
49
    Poll a set of 0MQ sockets, native file descs. or sockets.
46
50
 
57
61
    """
58
62
    cdef int rc, i
59
63
    cdef zmq_pollitem_t *pollitems = NULL
60
 
    cdef int nsockets = len(sockets)
 
64
    cdef int nsockets = <int>len(sockets)
61
65
    cdef Socket current_socket
62
 
    pollitems_o = allocate(nsockets*sizeof(zmq_pollitem_t),<void**>&pollitems)
 
66
    
 
67
    if nsockets == 0:
 
68
        return []
 
69
    
 
70
    pollitems = <zmq_pollitem_t *>malloc(nsockets*sizeof(zmq_pollitem_t))
 
71
    if pollitems == NULL:
 
72
        raise MemoryError("Could not allocate poll items")
 
73
        
63
74
    if ZMQ_VERSION_MAJOR < 3:
64
75
        # timeout is us in 2.x, ms in 3.x
65
76
        # expected input is ms (matches 3.x)
82
93
            try:
83
94
                fileno = int(s.fileno())
84
95
            except:
 
96
                free(pollitems)
85
97
                raise ValueError('fileno() must return an valid integer fd')
86
98
            else:
87
99
                pollitems[i].socket = NULL
89
101
                pollitems[i].events = events
90
102
                pollitems[i].revents = 0
91
103
        else:
 
104
            free(pollitems)
92
105
            raise TypeError(
93
106
                "Socket must be a 0MQ socket, an integer fd or have "
94
107
                "a fileno() method: %r" % s
95
108
            )
 
109
    
96
110
 
97
111
    with nogil:
98
 
        rc = zmq_poll(pollitems, nsockets, timeout)
99
 
    if rc == -1:
100
 
        raise ZMQError()
101
 
 
 
112
        rc = zmq_poll_c(pollitems, nsockets, timeout)
 
113
    
 
114
    if rc < 0:
 
115
        free(pollitems)
 
116
        _check_rc(rc)
 
117
    
102
118
    results = []
103
119
    for i in range(nsockets):
104
120
        s = sockets[i][0]
110
126
        if revents > 0:
111
127
            results.append((s, revents))
112
128
 
 
129
    free(pollitems)
113
130
    return results
114
131
 
115
132
#-----------------------------------------------------------------------------
116
133
# Symbols to export
117
134
#-----------------------------------------------------------------------------
118
135
 
119
 
__all__ = [ '_poll' ]
 
136
__all__ = [ 'zmq_poll' ]