
    \eY                     D    d dl Z e j                  ZddZ	 	 ddZd Zd Zy)    Nc                 N   t        | t        t        f      st        dt	        |       z        t        | t              r| S |s1t        t        j                  dd      xs t        j                         }	 | j                  ||      S # t        $ r | j                  d|      cY S w xY w)a  Decodes incoming text/bytes string using `incoming` if they're not
       already unicode.

    :param incoming: Text's current encoding
    :param errors: Errors handling policy. See here for valid
        values http://docs.python.org/2/library/codecs.html
    :returns: text or a unicode `incoming` encoded
                representation of it.
    :raises TypeError: If text is not an instance of str
    z%s can't be decodedencodingNutf-8)
isinstancestrbytes	TypeErrortypegetattrsysstdingetdefaultencodingdecodeUnicodeDecodeError)textincomingerrorss      8/usr/lib/python3/dist-packages/oslo_utils/encodeutils.pysafe_decoder      s     dS%L)-T
:;;$CIIz48 -**, 	,{{8V,, , {{7F++,s   4B B$#B$c                    t        | t        t        f      st        dt	        |       z        |s1t        t        j                  dd      xs t        j                         }t        |d      r|j                         }t        |d      r|j                         }t        | t              r| j                  ||      S | r$||k7  rt        | ||      } | j                  ||      S | S )a  Encodes incoming text/bytes string using `encoding`.

    If incoming is not specified, text is expected to be encoded with
    current python's default encoding. (`sys.getdefaultencoding`)

    :param incoming: Text's current encoding
    :param encoding: Expected encoding for text (Default UTF-8)
    :param errors: Errors handling policy. See here for valid
        values http://docs.python.org/2/library/codecs.html
    :returns: text or a bytestring `encoding` encoded
                representation of it.
    :raises TypeError: If text is not an instance of str

    See also to_utf8() function which is simpler and don't depend on
    the locale encoding.
    z%s can't be encodedr   Nlower)r   r   r   r	   r
   r   r   r   r   hasattrr   encoder   )r   r   r   r   s       r   safe_encoder   @   s    $ dS%L)-T
:;;CIIz48 -**, 	 x!>>#x!>>#${{8V,,	(h&462{{8V,,    c                     t        | t              r| S t        | t              r| j                  d      S t	        dt        |       j                  z        )zEncode Unicode to UTF-8, return bytes unchanged.

    Raise TypeError if text is not a bytes string or a Unicode string.

    .. versionadded:: 3.5
    r   z!bytes or Unicode expected, got %s)r   r   r   r   r	   r
   __name__)r   s    r   to_utf8r   i   sN     $	D#	{{7##;t*--. / 	/r   c                    d}|| j                         }t        |t              r|S 	 |j                  d      S # t        $ r Y nw xY wt               }	 |j                  |      S # t        $ r Y nw xY w|j                  d      S )a  Get the message of an exception as a Unicode string.

    On Python 3, the exception message is always a Unicode string. On
    Python 2, the exception message is a bytes string *most* of the time.

    If the exception message is a bytes strings, try to decode it from UTF-8
    (superset of ASCII), from the locale encoding, or fallback to decoding it
    from ISO-8859-1 (which never fails).

    .. versionadded:: 1.6
    Nr   latin1)__str__r   r   r   r   _getfilesystemencoding)excmsgr   s      r   exception_to_unicoder%   y   s     C
{ kkm#s
 
 zz'"" 
 &'Hzz(##  ::hs!   9 	AAA$ $	A0/A0)Nstrict)Nr   r&   )r   getfilesystemencodingr"   r   r   r   r%    r   r   <module>r)      s6      22 $,N  $)1&R/ / r   