~ubuntu-branches/ubuntu/precise/pyzmq/precise

« back to all changes in this revision

Viewing changes to zmq/core/error.pyx

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ożarowski
  • Date: 2011-02-15 09:08:36 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110215090836-phh4slym1g6muucn
Tags: 2.0.10.1-2
* Team upload.
* Upload to unstable
* Add Breaks: ${python:Breaks}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""0MQ Error classes and functions."""
 
2
 
 
3
#
 
4
#    Copyright (c) 2010 Brian E. Granger
 
5
#
 
6
#    This file is part of pyzmq.
 
7
#
 
8
#    pyzmq is free software; you can redistribute it and/or modify it under
 
9
#    the terms of the Lesser GNU General Public License as published by
 
10
#    the Free Software Foundation; either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    pyzmq is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    Lesser GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the Lesser GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
 
 
22
#-----------------------------------------------------------------------------
 
23
# Imports
 
24
#-----------------------------------------------------------------------------
 
25
 
 
26
from czmq cimport zmq_strerror, zmq_errno
 
27
 
 
28
from zmq.utils.strtypes import bytes
 
29
 
 
30
def strerror(errnum):
 
31
    """strerror(errnum)
 
32
 
 
33
    Return the error string given the error number.
 
34
    """
 
35
    cdef object str_e
 
36
    # char * will be a bytes object:
 
37
    str_e = zmq_strerror(errnum)
 
38
    if str is bytes:
 
39
        # Python 2: str is bytes, so we already have the right type
 
40
        return str_e
 
41
    else:
 
42
        # Python 3: decode bytes to unicode str
 
43
        return str_e.decode()
 
44
 
 
45
 
 
46
class ZMQBaseError(Exception):
 
47
    pass
 
48
 
 
49
 
 
50
class ZMQError(ZMQBaseError):
 
51
    """Base exception class for 0MQ errors in Python."""
 
52
 
 
53
    def __init__(self, error=None):
 
54
        """Wrap an errno style error.
 
55
 
 
56
        Parameters
 
57
        ----------
 
58
        error : int
 
59
            The ZMQ errno or None.  If None, then zmq_errno() is called and
 
60
            used.
 
61
        """
 
62
        if error is None:
 
63
            error = zmq_errno()
 
64
        if type(error) == int:
 
65
            self.strerror = strerror(error)
 
66
            self.errno = error
 
67
        else:
 
68
            self.strerror = str(error)
 
69
            self.errno = None
 
70
 
 
71
    def __str__(self):
 
72
        return self.strerror
 
73
 
 
74
 
 
75
class ZMQBindError(ZMQBaseError):
 
76
    """An error for bind_to_random_port."""
 
77
    pass
 
78
 
 
79
 
 
80
class NotDone(ZMQBaseError):
 
81
    """For raising in MessageTracker.wait"""
 
82
    pass
 
83
 
 
84
 
 
85
__all__ = ['strerror', 'ZMQBaseError', 'ZMQBindError', 'ZMQError', 'NotDone']