1aec3c8f4Simarom"""0MQ Error classes and functions."""
2fc46f261SDan Klein
3fc46f261SDan Klein#
4fc46f261SDan Klein#    Copyright (c) 2010-2011 Brian E. Granger & Min Ragan-Kelley
5fc46f261SDan Klein#
6fc46f261SDan Klein#    This file is part of pyzmq.
7fc46f261SDan Klein#
8fc46f261SDan Klein#    pyzmq is free software; you can redistribute it and/or modify it under
9fc46f261SDan Klein#    the terms of the Lesser GNU General Public License as published by
10fc46f261SDan Klein#    the Free Software Foundation; either version 3 of the License, or
11fc46f261SDan Klein#    (at your option) any later version.
12fc46f261SDan Klein#
13fc46f261SDan Klein#    pyzmq is distributed in the hope that it will be useful,
14fc46f261SDan Klein#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15fc46f261SDan Klein#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16fc46f261SDan Klein#    Lesser GNU General Public License for more details.
17fc46f261SDan Klein#
18fc46f261SDan Klein#    You should have received a copy of the Lesser GNU General Public License
19fc46f261SDan Klein#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20fc46f261SDan Klein#
21fc46f261SDan Klein
22fc46f261SDan Klein#-----------------------------------------------------------------------------
23aec3c8f4Simarom# Imports
24fc46f261SDan Klein#-----------------------------------------------------------------------------
25fc46f261SDan Klein
26aec3c8f4Simarom# allow const char*
27aec3c8f4Simaromcdef extern from *:
28aec3c8f4Simarom    ctypedef char* const_char_ptr "const char*"
29fc46f261SDan Klein
30aec3c8f4Simaromfrom libzmq cimport zmq_strerror, zmq_errno as zmq_errno_c
31aec3c8f4Simarom
32aec3c8f4Simaromfrom zmq.utils.strtypes import bytes
33aec3c8f4Simarom
34aec3c8f4Simaromdef strerror(int errno):
35aec3c8f4Simarom    """strerror(errno)
36aec3c8f4Simarom
37aec3c8f4Simarom    Return the error string given the error number.
38aec3c8f4Simarom    """
39aec3c8f4Simarom    cdef const_char_ptr str_e
40aec3c8f4Simarom    # char * will be a bytes object:
41aec3c8f4Simarom    str_e = zmq_strerror(errno)
42aec3c8f4Simarom    if str is bytes:
43aec3c8f4Simarom        # Python 2: str is bytes, so we already have the right type
44aec3c8f4Simarom        return str_e
45aec3c8f4Simarom    else:
46aec3c8f4Simarom        # Python 3: decode bytes to unicode str
47aec3c8f4Simarom        return str_e.decode()
48aec3c8f4Simarom
49aec3c8f4Simaromdef zmq_errno():
50aec3c8f4Simarom    """zmq_errno()
51fc46f261SDan Klein    
52aec3c8f4Simarom    Return the integer errno of the most recent zmq error.
53aec3c8f4Simarom    """
54aec3c8f4Simarom    return zmq_errno_c()
55fc46f261SDan Klein
56aec3c8f4Simarom__all__ = ['strerror', 'zmq_errno']
57