~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to zmq/core/error.pyx

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""0MQ Error classes and functions."""
2
2
 
3
3
#
4
 
#    Copyright (c) 2010 Brian E. Granger
 
4
#    Copyright (c) 2010-2011 Brian E. Granger & Min Ragan-Kelley
5
5
#
6
6
#    This file is part of pyzmq.
7
7
#
23
23
# Imports
24
24
#-----------------------------------------------------------------------------
25
25
 
26
 
from czmq cimport zmq_strerror, zmq_errno
 
26
# allow const char*
 
27
cdef extern from *:
 
28
    ctypedef char* const_char_ptr "const char*"
 
29
 
 
30
from cpython cimport PyErr_CheckSignals
 
31
 
 
32
from libzmq cimport zmq_strerror, zmq_errno
27
33
 
28
34
from zmq.utils.strtypes import bytes
29
35
 
30
 
def strerror(errnum):
31
 
    """strerror(errnum)
 
36
def strerror(int errno):
 
37
    """strerror(errno)
32
38
 
33
39
    Return the error string given the error number.
34
40
    """
35
 
    cdef object str_e
 
41
    cdef const_char_ptr str_e
36
42
    # char * will be a bytes object:
37
 
    str_e = zmq_strerror(errnum)
 
43
    str_e = zmq_strerror(errno)
38
44
    if str is bytes:
39
45
        # Python 2: str is bytes, so we already have the right type
40
46
        return str_e
44
50
 
45
51
 
46
52
class ZMQBaseError(Exception):
 
53
    """Base exception class for 0MQ errors in Python."""
47
54
    pass
48
55
 
49
56
 
50
57
class ZMQError(ZMQBaseError):
51
 
    """Base exception class for 0MQ errors in Python."""
52
 
 
53
 
    def __init__(self, error=None):
 
58
    """Wrap an errno style error.
 
59
 
 
60
    Parameters
 
61
    ----------
 
62
    errno : int
 
63
        The ZMQ errno or None.  If None, then ``zmq_errno()`` is called and
 
64
        used.
 
65
    msg : string
 
66
        Description of the error or None.
 
67
    """
 
68
    errno = None
 
69
 
 
70
    def __init__(self, errno=None, msg=None):
54
71
        """Wrap an errno style error.
55
72
 
56
73
        Parameters
57
74
        ----------
58
 
        error : int
59
 
            The ZMQ errno or None.  If None, then zmq_errno() is called and
 
75
        errno : int
 
76
            The ZMQ errno or None.  If None, then ``zmq_errno()`` is called and
60
77
            used.
 
78
        msg : string
 
79
            Description of the error or None.
61
80
        """
62
 
        if error is None:
63
 
            error = zmq_errno()
64
 
        if type(error) == int:
65
 
            self.strerror = strerror(error)
66
 
            self.errno = error
 
81
        if errno is None:
 
82
            errno = zmq_errno()
 
83
            error = errno
 
84
        if type(errno) == int:
 
85
            self.errno = errno
 
86
            if msg is None:
 
87
                self.strerror = strerror(errno)
 
88
            else:
 
89
                self.strerror = msg
67
90
        else:
68
 
            self.strerror = str(error)
69
 
            self.errno = None
 
91
            if msg is None:
 
92
                self.strerror = str(errno)
 
93
            else:
 
94
                self.strerror = msg
 
95
        # flush signals, because there could be a SIGINT
 
96
        # waiting to pounce, resulting in uncaught exceptions.
 
97
        # Doing this here means getting SIGINT during a blocking
 
98
        # libzmq call will raise a *catchable* KeyboardInterrupt
 
99
        PyErr_CheckSignals()
70
100
 
71
101
    def __str__(self):
72
102
        return self.strerror
 
103
    
 
104
    def __repr__(self):
 
105
        return "ZMQError('%s')"%self.strerror
73
106
 
74
107
 
75
108
class ZMQBindError(ZMQBaseError):
76
 
    """An error for bind_to_random_port."""
 
109
    """An error for ``Socket.bind_to_random_port()``.
 
110
    
 
111
    See Also
 
112
    --------
 
113
    .Socket.bind_to_random_port
 
114
    """
77
115
    pass
78
116
 
79
117
 
80
118
class NotDone(ZMQBaseError):
81
 
    """For raising in MessageTracker.wait"""
 
119
    """Raised when timeout is reached while waiting for 0MQ to finish with a Message
 
120
    
 
121
    See Also
 
122
    --------
 
123
    .MessageTracker.wait : object for tracking when ZeroMQ is done
 
124
    """
82
125
    pass
83
126
 
84
127