
v^s$                 @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l m	 Z	 d  d l
 m Z Gd d   d e  Z d d	 d
 d d  Z e d  e e e e j e j e j f Z d d   Z d d	 d
 d d  Z d d	 d
 d d  Z d d	 d
 d d  Z d d	 d
 d d  Z d d	 d
 d d  Z d d   Z d d d d g e d d   e d! d"  g Z d# d$   e D Z d% Z e j  d& d$   e d' d  D  d( d)   Z! d* d+   Z" d, d-   Z# d. d/   Z$ d0 d1   Z% d2 d3   Z& e&   Z' d S)4    N)Decimal)quote)RemovedInDjango40Warning)Promisec                   s4   e  Z d  Z   f d d   Z   f d d   Z   S)DjangoUnicodeDecodeErrorc                s   | |  _  t   j |   d  S)N)objsuper__init__)selfr   args)	__class__ 7/tmp/pip-build-8lau8j11/django/django/utils/encoding.pyr	      s    	z!DjangoUnicodeDecodeError.__init__c                s&   d t    j   |  j t |  j  f S)Nz%s. You passed in %r (%s))r   __str__r   type)r
   )r   r   r   r      s    z DjangoUnicodeDecodeError.__str__)__name__
__module____qualname__r	   r   r   r   )r   r   r      s   r   zutf-8Fstrictc             C   s&   t  |  t  r |  St |  | | |  S)z
    Return a string representing 's'. Treat bytestrings using the 'encoding'
    codec.

    If strings_only is True, don't convert (some) non-string-like objects.
    )
isinstancer   	force_str)sencodingstrings_onlyerrorsr   r   r   	smart_str   s    r   c             C   s   t  |  t  S)zDetermine if the object instance is of a protected type.

    Objects of protected types are preserved as-is when passed to
    force_str(strings_only=True).
    )r   _PROTECTED_TYPES)r   r   r   r   is_protected_type'   s    r   c             C   s   t  t |   t  r |  S| r/ t |   r/ |  Sy4 t |  t  rV t |  | |  }  n t |   }  Wn7 t k
 r } z t |  | j   WYd d } ~ Xn X|  S)z
    Similar to smart_str(), except that lazy instances are resolved to
    strings, rather than kept as lazy objects.

    If strings_only is True, don't convert (some) non-string-like objects.
    N)	
issubclassr   strr   r   bytesUnicodeDecodeErrorr   r   )r   r   r   r   er   r   r   r   0   s    %r   c             C   s&   t  |  t  r |  St |  | | |  S)z
    Return a bytestring version of 's', encoded as specified in 'encoding'.

    If strings_only is True, don't convert (some) non-string-like objects.
    )r   r   force_bytes)r   r   r   r   r   r   r   smart_bytesF   s    r$   c             C   s   t  |  t  r; | d k r |  S|  j d |  j | |  S| rQ t |   rQ |  St  |  t  rj t |   St |   j | |  S)z
    Similar to smart_bytes, except that lazy instances are resolved to
    strings, rather than kept as lazy objects.

    If strings_only is True, don't convert (some) non-string-like objects.
    zutf-8)r   r    decodeencoder   
memoryviewr   )r   r   r   r   r   r   r   r#   R   s    
r#   c             C   s)   t  j d t d d t |  | | |  S)Nz3smart_text() is deprecated in favor of smart_str().
stacklevel   )warningswarnr   r   )r   r   r   r   r   r   r   
smart_textf   s    r,   c             C   s)   t  j d t d d t |  | | |  S)Nz3force_text() is deprecated in favor of force_str().r(   r)   )r*   r+   r   r   )r   r   r   r   r   r   r   
force_textn   s    r-   c             C   s;   |  d k r |  St  |  t  r+ t |   }  t |  d d S)u  
    Convert an Internationalized Resource Identifier (IRI) portion to a URI
    portion that is suitable for inclusion in a URL.

    This is the algorithm from section 3.1 of RFC 3987, slightly simplified
    since the input is assumed to be a string rather than an arbitrary byte
    stream.

    Take an IRI (string or UTF-8 bytes, e.g. '/I ♥ Django/' or
    b'/I â¥ Django/') and return a string containing the encoded
    result with ASCII chars only (e.g. '/I%20%E2%99%A5%20Django/').
    Nsafez/#%[]=:;$&()+,!?*@'~)r   r   r   r   )irir   r   r   
iri_to_uriv   s
    r0   -   .   _   ~   A   [   a   {   c             C   sL   i  |  ]B } | D]5 } d  d g D]" } t  | f  | | j    q  q q S)z%02xz%02X)r    r&   ).0Zascii_rangecharfmtr   r   r   
<dictcomp>   s   	
r<   0123456789ABCDEFabcdefc             C   s=   i  |  ]3 } t  D]& } t j | |  | | j    q q Sr   )_hexdigr    fromhexr&   )r9   abr   r   r   r<      s   	   c             C   s   |  d k r |  St  |   }  |  j d  } t |  d k rF |  } n | d g } | j } t } xw | d d  D]e } | d d  } | | k r | | | d d   | | d d   qs | d  | |  qs Wd j |  } t |  j   S)uL  
    Convert a Uniform Resource Identifier(URI) into an Internationalized
    Resource Identifier(IRI).

    This is the algorithm from section 3.2 of RFC 3987, excluding step 4.

    Take an URI in ASCII bytes (e.g. '/I%20%E2%99%A5%20Django/') and return
    a string containing the encoded result (e.g. '/I%20♥%20Django/').
    N   %   r   r)       )r#   splitlenappend
_hextobytejoinrepercent_broken_unicoder%   )uribitsr/   partsrH   Z	hextobyteitemhexr   r   r   
uri_to_iri   s$    
		
rQ   c             C   s   t  |  d d S)zh
    Escape the unsafe characters from the path portion of a Uniform Resource
    Identifier (URI).
    r.   z/:@&+$,-_.!~*'())r   )pathr   r   r   escape_uri_path   s    rS   c             C   s   |  j  d  j d  S)z:Return the Punycode of the given domain if it's non-ASCII.idnaascii)r&   r%   )domainr   r   r   punycode   s    rW   c             C   s   x y |  j    Wnu t k
 r } zU t |  | j | j  d d } |  d | j  | j   |  | j d  }  WYd d } ~ Xq X|  Sq Wd S)z
    As per section 3.2 of RFC 3987, step three of converting a URI into an IRI,
    repercent-encode any octet produced that is not part of a strictly legal
    UTF-8 octet sequence.
    r.   s   /#%[]=:;$&()+,!?*@'~N)r%   r!   r   startendr&   )rR   r"   Z	repercentr   r   r   rK      s    "ArK   c             C   s,   |  d k r |  St  |  j d d  d d S)aA  Convert a file system path to a URI portion that is suitable for
    inclusion in a URL.

    Encode certain chars that would normally be recognized as special chars
    for URIs. Do not encode the ' character, as it is a valid character
    within URIs. See the encodeURIComponent() JavaScript function for details.
    N\/r.   z/~!*()')r   replace)rR   r   r   r   filepath_to_uri   s    r]   c              C   sF   y' t  j   d p d }  t j |   Wn t k
 rA d }  Yn X|  S)z
    The encoding of the default system locale. Fallback to 'ascii' if the
    #encoding is unsupported by Python or could not be determined. See tickets
    #10335 and #5846.
    rD   rU   )localegetdefaultlocalecodecslookup	Exception)r   r   r   r   get_system_encoding  s    rc   )(r`   datetimer^   r*   decimalr   urllib.parser   Zdjango.utils.deprecationr   Zdjango.utils.functionalr   r!   r   r   r   intfloatdatetimer   r   r   r$   r#   r,   r-   r0   rangeZ_ascii_rangesrI   r>   updaterQ   rS   rW   rK   r]   rc   ZDEFAULT_LOCALE_ENCODINGr   r   r   r   <module>   s<   	*	"-	
$