a
    ze                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZG dd dZG dd dZG dd de	Z
G dd	 d	e	ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd dejZedkre  dS )    Nc                   @   s   e Zd Zdd Zdd ZdS )	PosReturnc                 C   s
   d| _ d S Nr   )posself r   ./usr/lib/python3.9/test/test_codeccallbacks.py__init__   s    zPosReturn.__init__c                 C   s>   | j }|}|dk r t|j| }||jkr6t|j| _ d|fS )Nr   <?>)r   lenobjectstart)r   excZoldposZrealposr   r   r   handle   s    
zPosReturn.handleN__name__
__module____qualname__r	   r   r   r   r   r   r   	   s   r   c                   @   s   e Zd ZdddZdd ZdS )RepeatedPosReturnr
   c                 C   s   || _ d| _d| _d S r   )replr   count)r   r   r   r   r   r	      s    zRepeatedPosReturn.__init__c                 C   s0   | j dkr$|  j d8  _ | j| jfS | j|jfS )Nr      )r   r   r   end)r   r   r   r   r   r       s    
zRepeatedPosReturn.handleN)r
   r   r   r   r   r   r      s   
r   c                   @   s   e Zd Zdd ZdS )BadStartUnicodeEncodeErrorc                 C   s   t | ddddd g | _d S Nascii r   r   bad)UnicodeEncodeErrorr	   r   r   r   r   r   r	   (   s    z#BadStartUnicodeEncodeError.__init__Nr   r   r   r	   r   r   r   r   r   '   s   r   c                   @   s   e Zd Zdd ZdS )BadObjectUnicodeEncodeErrorc                 C   s   t | ddddd g | _d S r   )r   r	   r   r   r   r   r   r	   .   s    z$BadObjectUnicodeEncodeError.__init__Nr   r   r   r   r   r    -   s   r    c                   @   s   e Zd Zdd ZdS )NoEndUnicodeDecodeErrorc                 C   s    t | dtdddd | `d S Nr       r   r   r   )UnicodeDecodeErrorr	   	bytearrayr   r   r   r   r   r	   4   s    z NoEndUnicodeDecodeError.__init__Nr   r   r   r   r   r!   3   s   r!   c                   @   s   e Zd Zdd ZdS )BadObjectUnicodeDecodeErrorc                 C   s"   t | dtdddd g | _d S r"   )r$   r	   r%   r   r   r   r   r   r	   :   s    z$BadObjectUnicodeDecodeError.__init__Nr   r   r   r   r   r&   9   s   r&   c                   @   s   e Zd Zdd ZdS )NoStartUnicodeTranslateErrorc                 C   s   t | dddd | `d S Nr   r   r   r   )UnicodeTranslateErrorr	   r   r   r   r   r   r	   @   s    z%NoStartUnicodeTranslateError.__init__Nr   r   r   r   r   r'   ?   s   r'   c                   @   s   e Zd Zdd ZdS )NoEndUnicodeTranslateErrorc                 C   s   t | dddd | `d S r(   )r)   r	   r   r   r   r   r   r	   F   s    z#NoEndUnicodeTranslateError.__init__Nr   r   r   r   r   r*   E   s   r*   c                   @   s   e Zd Zdd ZdS )NoObjectUnicodeTranslateErrorc                 C   s   t | dddd | `d S r(   )r)   r	   r   r   r   r   r   r	   L   s    z&NoObjectUnicodeTranslateError.__init__Nr   r   r   r   r   r+   K   s   r+   c                   @   sL  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQS )RCodecCallbackTestc                 C   s0   d}|  |ddd |  |ddd d S )Nu   スパモ änd eggsr   xmlcharrefreplaces&   &#12473;&#12497;&#12514; &#228;nd eggslatin-1s!   &#12473;&#12497;&#12514; nd eggsassertEqualencode)r   sr   r   r   test_xmlcharrefreplaceR   s    

z(CodecCallbackTest.test_xmlcharrefreplacec                 C   sd   dd }t d| d}d}| |dd| d}| |dd| d	}| |d
d| d S )Nc              	   S   s   t | tstd|  g }| j| j| j D ]H}z|dtjj	t
|   W q, tyr   |dt
|  Y q,0 q,d|| jfS )Ndon't know how to handle %rz&%s;&#%d;r   )
isinstancer   	TypeErrorr   r   r   appendhtmlZentitiesZcodepoint2nameordKeyErrorjoinr   lcr   r   r   xmlcharnamereplaced   s    
zECodecCallbackTest.test_xmlcharnamereplace.<locals>.xmlcharnamereplaceztest.xmlcharnamereplaceu   «ℜ» = 〈ሴ€〉s0   &laquo;&real;&raquo; = &lang;&#4660;&euro;&rang;r   s$   &real; = &lang;&#4660;&euro;&rang;r.   s   &real; = &lang;&#4660;&rang;iso-8859-15codecsregister_errorr0   r1   )r   r@   sinsoutr   r   r   test_xmlcharnamereplace`   s    z)CodecCallbackTest.test_xmlcharnamereplacec                 C   sd   dd }t d| d}d}| |dd| d}| |dd| d	}| |d
d| d S )Nc              	   S   s`   t | tstd|  g }| j| j| j D ]}|t|dt	|  q,dd
| | jfS )Nr4   z0x%xz
[1m%s[0mz, )r6   r   r7   r   r   r   r8   unicodedatanamer:   r<   r=   r   r   r   uninamereplace   s    
z=CodecCallbackTest.test_uninamereplace.<locals>.uninamereplaceztest.uninamereplaceu   ¬ሴ€耀sN   [1mNOT SIGN, ETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000[0mr   sE   [1mETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000[0mr.   sA   [1mETHIOPIC SYLLABLE SEE[0m[1mCJK UNIFIED IDEOGRAPH-8000[0mrA   rB   )r   rJ   rE   rF   r   r   r   test_uninamereplacez   s    	z%CodecCallbackTest.test_uninamereplacec                 C   sP   d}d}|  |dd| d}|  |dd| d}|  |dd| d S )	N   a¬ሴ€耀􏿿s!   a\xac\u1234\u20ac\u8000\U0010ffffr   backslashreplaces   a\u1234\u20ac\u8000\U0010ffffr.   s   a\u1234\u8000\U0010ffffrA   r/   r   rE   rF   r   r   r   test_backslashescape   s    z&CodecCallbackTest.test_backslashescapec                 C   sP   d}d}|  |dd| d}|  |dd| d}|  |dd| d S )	NrL   s[   a\N{NOT SIGN}\N{ETHIOPIC SYLLABLE SEE}\N{EURO SIGN}\N{CJK UNIFIED IDEOGRAPH-8000}\U0010ffffr   namereplacesP   a\N{ETHIOPIC SYLLABLE SEE}\N{EURO SIGN}\N{CJK UNIFIED IDEOGRAPH-8000}\U0010ffffr.   sD   a\N{ETHIOPIC SYLLABLE SEE}\N{CJK UNIFIED IDEOGRAPH-8000}\U0010ffffrA   r/   rN   r   r   r   test_nameescape   s    z!CodecCallbackTest.test_nameescapec                 C   sJ   dd }t d| d}d}| |dd| d}| t|jdd d S )Nc                 S   sF   t | tstd|  | j| j| jd  dkr>d| jd fS | d S )Nr4      s    )r6   r$   r7   r   r   r   r   r   r   relaxedutf8   s
    
z>CodecCallbackTest.test_decoding_callbacks.<locals>.relaxedutf8ztest.relaxedutf8s   a bcüu	   a b cü  utf-8s   )rC   rD   r0   decodeassertRaisesr$   )r   rU   rE   rF   r   r   r   test_decoding_callbacks   s    
z)CodecCallbackTest.test_decoding_callbacksc                 C   s   t dd dD }d}d}| t|d|d | d}| ttj|d| d	|td
< d}d}| t|d|d | d|td
< | ttj|d| d S )Nc                 s   s(   | ] }t |td |  dfV  qdS )rR   r   N)r:   bytesupper.0r?   r   r   r   	<genexpr>   r#   z7CodecCallbackTest.test_charmapencode.<locals>.<genexpr>Zabcdefghabcs   AABBCCstrictr   ZabcAs   XYZ?ZabcDEFs   AABBCCXYZXYZXYZreplaceZXYZ)dictr0   rC   charmap_encoderX   UnicodeErrorr:   r7   )r   charmaprE   rF   r   r   r   test_charmapencode   s    z$CodecCallbackTest.test_charmapencodec                 C   s   dd }t d| dd }t d| d}| |ddd	 | |ddd
 | dddd | dddd | t ddtddid d | dddd | dddd d S )Nc                    sj   t  j j}t tr, fdd|D }n*t trJ fdd|D }ntd  dd|  jfS )Nc                    s   g | ]}d t  j|  qS z<%d>)r:   r   r]   r   rT   r   r   
<listcomp>   r#   zFCodecCallbackTest.test_callbacks.<locals>.handler1.<locals>.<listcomp>c                    s   g | ]}d  j |  qS rh   r   ri   rT   r   r   rj      r#   r4   [%s]r   )ranger   r   r6   r   r$   r7   r<   )r   rr>   r   rT   r   handler1   s    

z2CodecCallbackTest.test_callbacks.<locals>.handler1ztest.handler1c                    sJ   t  tstd   fddt j jD }dd|  jd fS )Nr4   c                    s   g | ]}d  j |  qS rh   rk   ri   rT   r   r   rj      r#   zFCodecCallbackTest.test_callbacks.<locals>.handler2.<locals>.<listcomp>rl   r   r   )r6   r$   r7   rm   r   r   r<   )r   r>   r   rT   r   handler2   s    
z2CodecCallbackTest.test_callbacks.<locals>.handler2ztest.handler2s    r   z [<129>][<128>][<255>]z [<129>][<128>]s   \u3042\u3xxxunicode-escapeu   あ[<92><117><51>]xxxs   \u3042\u3xxu   あ[<92><117><51>]xx   abcazr   zz[<98>][<99>]u   güßrks   g[<252><223>]rku   güßs   g[<252><223>])rC   rD   r0   rW   charmap_decoder:   r1   )r   ro   rp   r2   r   r   r   test_callbacks   sB    






z CodecCallbackTest.test_callbacksc                    s   g d}|D ]}t d| t | qd |dd |D 7 } fdddD D ]<}dD ]2}|D ](}z||| W q` ty   Y q`0 q`qXqPd S )	N)r`   ignorerb   r-   rM   rP   test.  c                 S   s   g | ]}d | qS )rx   r   )r]   errr   r   r   rj   %  r#   z6CodecCallbackTest.test_longstrings.<locals>.<listcomp>c                    s   g | ]}|  qS r   r   )r]   r2   r>   r   r   rj   &  r#   )x   あu   aä)r   r.   
iso-8859-1rA   rV   utf-7utf-16utf-32)rC   rD   lookup_errorr1   re   )r   errorsrz   Zuniencr   r{   r   test_longstrings  s    z"CodecCallbackTest.test_longstringsc           
      C   s   | j t|g|d d R   | j t|g|dg R   g d}tt|D ]p}|D ]f}t|t|| u rlqRg }tt|D ]&}||kr|| q||||  q|| j t|g|R   qRqJ|| }	| t|	| d S )Nztoo much)Zspams   eggss   spam*   g      ?N)rX   r7   rm   r   typer8   r0   str)
r   exctypeargsmsgZ	wrongargsiZwrongargZcallargsjr   r   r   r   check_exceptionobjectargs/  s    z+CodecCallbackTest.check_exceptionobjectargsc                 C   sp   |  tg dd |  tg dd |  tg dd |  tg dd |  tg d	d
 |  tg dd d S )N)r      gürkr   rR   ouchz?'ascii' codec can't encode character '\xfc' in position 1: ouch)r   r   r      r   z;'ascii' codec can't encode characters in position 1-3: ouch)r   u   üxr   r   r   z?'ascii' codec can't encode character '\xfc' in position 0: ouch)r   u   Āxr   r   r   zA'ascii' codec can't encode character '\u0100' in position 0: ouch)r   u   ￿xr   r   r   zA'ascii' codec can't encode character '\uffff' in position 0: ouch)r   u   𐀀xr   r   r   zE'ascii' codec can't encode character '\U00010000' in position 0: ouch)r   r   r   r   r   r   test_unicodeencodeerrorH  s<    z)CodecCallbackTest.test_unicodeencodeerrorc                 C   s<   |  tdtddddgd |  tdtddddgd d S )	Nr   s   grkr   rR   r   z8'ascii' codec can't decode byte 0xfc in position 1: ouch   z6'ascii' codec can't decode bytes in position 1-2: ouch)r   r$   r%   r   r   r   r   test_unicodedecodeerrorh  s    z)CodecCallbackTest.test_unicodedecodeerrorc                 C   s^   |  tg dd |  tg dd |  tg dd |  tg dd |  tg d	d
 d S )N)r   r   rR   r   z4can't translate character '\xfc' in position 1: ouch)u   gĀrkr   rR   r   z6can't translate character '\u0100' in position 1: ouch)u   g￿rkr   rR   r   z6can't translate character '\uffff' in position 1: ouch)u   g𐀀rkr   rR   r   z:can't translate character '\U00010000' in position 1: ouch)r   r   r   r   z0can't translate characters in position 1-2: ouch)r   r)   r   r   r   r   test_unicodetranslateerrort  s2    z,CodecCallbackTest.test_unicodetranslateerrorc              
   C   s~   |  ttjd |  ttjtd |  ttjtddddd |  ttjtdtdddd |  ttjtdddd d S )Nr   r   r   r}   r   r      )	rX   r7   rC   strict_errors	Exceptionr   r$   r%   r)   r   r   r   r   test_badandgoodstrictexceptions  s2    z1CodecCallbackTest.test_badandgoodstrictexceptionsc              
   C   s   |  ttjd |  ttjtd | ttdddddd | ttdtddddd | tt	ddddd d S )	Nr   r   r      aあbr   rR   )r   rR      ab)
rX   r7   rC   ignore_errorsre   r0   r   r$   r%   r)   r   r   r   r   test_badandgoodignoreexceptions  s8    z1CodecCallbackTest.test_badandgoodignoreexceptionsc              
   C   s   |  ttjd |  ttjtd |  ttjt  |  ttjt  | ttdddddd | tt	dt
ddddd	 | ttddddd	 d S )
Nr   r   r   r   r   rR   )ra   rR   r   )   �rR   )rX   r7   rC   replace_errorsre   r    r&   r0   r   r$   r%   r)   r   r   r   r    test_badandgoodreplaceexceptions  sL    z2CodecCallbackTest.test_badandgoodreplaceexceptionsc                 C   s   |  ttjd |  ttjtd |  ttjtdtdddd |  ttjtdddd d}|d	7 }d
dd |D }| 	tt
dd| d ddt| dd
dd |D dt| f d S )Nr   r   r   r   r   r   r}   )r   r   	   
   c   d   i  ry   i'  '  i 順 i?B @B )i   i  r   c                 s   s   | ]}t |V  qd S Nchrr\   r   r   r   r^     r#   zOCodecCallbackTest.test_badandgoodxmlcharrefreplaceexceptions.<locals>.<genexpr>rs   bc                 s   s   | ]}d | V  qdS )r5   Nr   r\   r   r   r   r^     r#   )rX   r7   rC   xmlcharrefreplace_errorsre   r$   r%   r)   r<   r0   r   r   )r   csr2   r   r   r   *test_badandgoodxmlcharrefreplaceexceptions  s>    z<CodecCallbackTest.test_badandgoodxmlcharrefreplaceexceptionsc                 C   sN  |  ttjd |  ttjtd g d}|D ]\}}| j|d | ttdd| d ddt| d|dt| f | tt	d| d ddt| d|dt| f W d    q01 s0    Y  q0g d	}|D ]d\}}| j|d
> | tt
dtd| d ddd|df W d    q1 s>0    Y  qd S )Nr   r   ))r}   z\u3042)
\x0a)rs   \x61rS   \x00)   ÿ\xff)u   Āz\u0100)u   ￿z\uffff)u   𐀀z
\U00010000u   􏿿z
\U0010ffff   z\ud800   z\udfff   z\ud800\udfffr   r   rs   r   r   ))   ar   )   
r   )    r   )r   r   )rZ   r      brR   )rX   r7   rC   backslashreplace_errorsre   subTestr0   r   r   r)   r$   r%   )r   testsr2   rn   r   r   r   r   )test_badandgoodbackslashreplaceexceptions  sP    $z;CodecCallbackTest.test_badandgoodbackslashreplaceexceptionsc                 C   s   |  ttjd |  ttjtd |  ttjtdtdddd |  ttjtdddd g d}|D ]n\}}| j|d	J | 	tt
dd
| d ddt| d|dt| f W d    qj1 s0    Y  qjd S )Nr   r   r   r   r   r   r}   ))r}   z\N{HIRAGANA LETTER A}r   )u   ﯹzW\N{ARABIC LIGATURE UIGHUR KIRGHIZ YEH WITH HAMZA ABOVE WITH ALEF MAKSURA ISOLATED FORM})u   󠁿z\N{CANCEL TAG}r   r   r   r   r   rs   r   )rX   r7   rC   namereplace_errorsre   r$   r%   r)   r   r0   r   r   )r   r   r2   rn   r   r   r   $test_badandgoodnamereplaceexceptionsS  s>    z6CodecCallbackTest.test_badandgoodnamereplaceexceptionsc                 C   s   t d}| t|d | t|td | t|tdddd dD ]J}| j|d* | t|td	|ddd W d    qF1 s0    Y  qF| |td	d
dddd | t	|t	d	t
dddd | |t	d	t
ddddd d S )Nsurrogateescaper   r      r   r   )rs   u   u   r   r   u   abrR   )   rR   r   s   ab)r   rR   )rC   r   rX   r7   re   r)   r   r   r0   r$   r%   )r   Zsurrogateescape_errorsr2   r   r   r   (test_badandgoodsurrogateescapeexceptions  sP    
$z:CodecCallbackTest.test_badandgoodsurrogateescapeexceptionsc                 C   s  t d}| t|d | t|td | t|tdddd dD ]j}| j|dJ | t|t|d	ddd | t|t|d		|ddd W d    qF1 s0    Y  qFd
D ]N}| j|d. | t|td|dt
|d W d    q1 s0    Y  qg d}|D ]\}}}}| j|||d | |t|d	| d ddt
| d|dt
| f | |t|td|d |  d dd| d|d d d| f W d    n1 s0    Y  qd S )Nsurrogatepassr   r   r   r   r   )rV   utf-16leutf-16beutf-32leutf-32beencodingrs   )r   r   r   r   r   ))rV   r   s   r   )r   r   s    rR   )r   r   s    rR   )r   r   s      r   )r   r   s      r   )rV   r   s   r   )r   r   s   rR   )r   r   s   rR   )r   r   s     r   )r   r   s     r   )rV   r   s   r   )r   r   s    rR   )r   r   s    rR   )r   r   s        r   )r   r   s        r   )r   r   rZ   r   r   r   )rC   r   rX   r7   re   r)   r   r   r$   r1   r   r0   r%   )r   Zsurrogatepass_errorsr   r2   r   r   nr   r   r   &test_badandgoodsurrogatepassexceptions  sl    
$$
z8CodecCallbackTest.test_badandgoodsurrogatepassexceptionsc                    sf   d}d}|D ]T t d fdd |D ]}| tdj|d q(dD ]\}}| t|j|d qDqd S )N)	r   foo)r   rR   r   r   r   r   r   Nr   r   r   r   )r   r.   r~   rA   ztest.badhandlerc                    s    S r   r   )r|   resr   r   <lambda>  r#   z:CodecCallbackTest.test_badhandlerresults.<locals>.<lambda>r}   )r   r   rV   r   )r   s   +x-)rC   rD   rX   r7   r1   rW   )r   resultsZencsr   rZ   r   r   r   test_badhandlerresults  s$    z(CodecCallbackTest.test_badhandlerresultsc                 C   s|   |  tjtd |  tjtd |  tjtd |  tjtd |  tjtd |  tjtd d S )Nr`   rw   r-   rM   rP   )r0   rC   r   r   r   r   r   r   r   r   r   r   test_lookup  s    zCodecCallbackTest.test_lookupc              
      s    fdd}t d| dD ]0\}} ||d}| |d  d | qdD ]\}} | j|dv | t}||d W d    n1 s0    Y  |j}| |jd	 | |j	d
 | |j
| W d    qR1 s0    Y  qRd S )Nc                    s$   t | tr | jfS td|  d S Nr4   r6   r   r   r7   rT   r   r   r   r      s    

zBCodecCallbackTest.test_encode_nonascii_replacement.<locals>.handletest.replacing))r      [¤]r_   )r~      [€]u   ½¾)rA   r   u   œŸ[]))rV      []   🐍)r   r   r   )r   r   r   r   r   rR   )rC   rD   r1   r0   r   rX   r   	exceptionr   r   r   )r   r   r   inputr   cmr   r   r   r    test_encode_nonascii_replacement  s    *z2CodecCallbackTest.test_encode_nonascii_replacementc              
      s    fdd}t d| dD ]\}} | j|dv | t}||d W d    n1 s`0    Y  |j}| |jd | |j	d | |j
| W d    q1 s0    Y  qd S )Nc                    s$   t | tr | jfS td|  d S r   r   rT   r   r   r   	unencrepl<  s    

zHCodecCallbackTest.test_encode_unencodable_replacement.<locals>.unencreplztest.unencreplhandler))r   r      ½)r~   r   u   œ)rA   r   r   )rV   r      )r   r   r   )r   r   r   r   r   rR   )rC   rD   r   rX   r   r1   r   r0   r   r   r   )r   r   r   r   r   r   r   r   r   #test_encode_unencodable_replacement;  s    *z5CodecCallbackTest.test_encode_unencodable_replacementc              	      s    fdd}t d| dD ]b\}} | j|d< ||d}| |d|  d|  W d    q1 st0    Y  qd S )Nc                    s$   t | tr | jfS td|  d S r   r   rT   r   r   r   r   T  s    

z?CodecCallbackTest.test_encode_bytes_replacement.<locals>.handler   ))r   r      )r~   r   r   )rA   r   r   )rV   r   r   )r   r   r   )r   r   r   )r   r      )r   r   r   r   r   r   )rC   rD   r   r1   r0   )r   r   r   r   r   r   r   r   test_encode_bytes_replacementS  s    
z/CodecCallbackTest.test_encode_bytes_replacementc              
      s    fdd}t d| d}g tdddgtdg d	R D ]\} | j| d
 | t}||d W d    n1 s0    Y  |j}| 	|j
d | 	|jd | 	|j| | 	|jd W d    qB1 s0    Y  qBd S )Nc                    s$   t | tr | jfS td|  d S r   r   rT   r   r   r   r   j  s    

zCCodecCallbackTest.test_encode_odd_bytes_replacement.<locals>.handler   r   )r   r   r   rr   )r   r   )r   s   abrr   s   abcde)r   r   r   rR   zsurrogates not allowed)rC   rD   	itertoolsproductr   rX   r   r1   r   r0   r   r   r   reason)r   r   r   r   r   r   r   r   r   !test_encode_odd_bytes_replacementi  s(    *z3CodecCallbackTest.test_encode_odd_bytes_replacementc                 C   s&   |  ttjd |  ttjdd d S )Nr   z
test.dummy)rX   r7   rC   rD   r   r   r   r   test_badregistercall  s    z&CodecCallbackTest.test_badregistercallc                 C   s   |  ttj d S r   )rX   r7   rC   r   r   r   r   r   test_badlookupcall  s    z$CodecCallbackTest.test_badlookupcallc                 C   s   |  ttjd d S )Ntest.unknown)rX   LookupErrorrC   r   r   r   r   r   test_unknownhandler  s    z%CodecCallbackTest.test_unknownhandlerc                 C   sJ   d}d dd |D }tdtj dD ]}dD ]}||| q2q*d S )N)r      r   2   r   i  ry   i  r   iP  r   i  r   r   c                 S   s   g | ]}t |qS r   r   )r]   r|   r   r   r   rj     r#   z;CodecCallbackTest.test_xmlcharrefvalues.<locals>.<listcomp>test.xmlcharrefreplace)r   rA   )r-   r  )r<   rC   rD   r   r1   )r   vr2   r   rz   r   r   r   test_xmlcharrefvalues  s    z'CodecCallbackTest.test_xmlcharrefvaluesc              	   C   s  |  tdjdd dd }td| |  tdjdd |  tdjdd |  td	jdd |  td
jdd |  tdjdd |  tdjdd dd }td| |  tdjdd t }td|j d|_| 	dddd d|_| 	dddd d|_|  t
djdd d|_| 	dddd d|_| 	dddd d|_|  t
djdd d|_| 	dddd G dd dt}|  ttjdd d!d i |  ttjdd |  |  ttjdd d!tjd i d S )"Nr   r   r   c                 S   s   dS Nr   r   rT   r   r   r   baddecodereturn1  s    z=CodecCallbackTest.test_decodehelper.<locals>.baddecodereturn1ztest.baddecodereturn1   \rq   s   \x0s   \x0ys
   \Uffffeeees   \uyyyyraw-unicode-escapec                 S   s   dS N)ra   Nr   rT   r   r   r   baddecodereturn2  s    z=CodecCallbackTest.test_decodehelper.<locals>.baddecodereturn2ztest.baddecodereturn2test.posreturnr   s   0z<?>0z<?><?>r   rR   r
   r      s   \uyyyy0c                   @   s   e Zd Zdd ZdS )z.CodecCallbackTest.test_decodehelper.<locals>.Dc                 S   s   t d S r   
ValueErrorr   keyr   r   r   __getitem__  s    z:CodecCallbackTest.test_decodehelper.<locals>.D.__getitem__Nr   r   r   r  r   r   r   r   D  s   r  r`      )rX   r   rW   rC   rD   r7   r   r   r   r0   
IndexErrorrc   re   ru   r  sys
maxunicode)r   r  r
  handlerr  r   r   r   test_decodehelper  s@    z#CodecCallbackTest.test_decodehelperc              	   C   sv  |  tdjdd dd }td| |  tdjdd dd }td	| |  tdjdd	 t }td
|j d|_| 	ddd
d d|_| 	ddd
d d|_|  t
djdd
 d|_| 	ddd
d d|_| 	ddd
d d|_|  t
djdd
 d|_G dd dt}dD ]L}|  ttjd|dd i |  ttjd||  |  ttjd|ddi q$d S )Nr   r   r   c                 S   s   dS r  r   rT   r   r   r   badencodereturn1  s    z=CodecCallbackTest.test_encodehelper.<locals>.badencodereturn1ztest.badencodereturn1c                 S   s   dS r	  r   rT   r   r   r   badencodereturn2  s    z=CodecCallbackTest.test_encodehelper.<locals>.badencodereturn2ztest.badencodereturn2r  r   u   ÿ0s   <?>0r  s   <?><?>r  r   rR   s   <?>r   r   c                   @   s   e Zd Zdd ZdS )z.CodecCallbackTest.test_encodehelper.<locals>.Dc                 S   s   t d S r   r  r  r   r   r   r    s    z:CodecCallbackTest.test_encodehelper.<locals>.D.__getitem__Nr  r   r   r   r   r    s   r  )r`   rb   r-   rM   rP   r  r  i,  )rX   r   r1   rC   rD   r7   r   r   r   r0   r  rc   re   rd   r  )r   r  r  r  r  rz   r   r   r   test_encodehelper  s6    z#CodecCallbackTest.test_encodehelperc              	   C   s   t d}td|j g d}|D ]`\}}d|| }| j|d. d|_||d}| |d W d    q"1 sx0    Y  q"d S )Nr|   test.bug36819)r   r   )r   s   ܀)r   s     ܀)z
iso-8859-6r   Zabcdr   r  abcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdxabcdx)	r   rC   rD   r   r1   r   r   rW   r0   )r   r  Z	testcasesr   r   r   Zdecodedr   r   r   test_decodehelper_bug36819  s    z,CodecCallbackTest.test_decodehelper_bug36819c              
   C   s  t  }td|j d}g d}|dg7 }tjdkr<ddg}d|_|D ]}| j|d	| d
|_| 	t
}||d W d    n1 s0    Y  |j}| |jd | |jd | |j| W d    qF1 s0    Y  qFtjdkr`d
|_| 	t
}td|d W d    n1 s&0    Y  |j}| |jd | |jd | |j| d|_|D ]X}| j|d	4 d
|_||d}| ||d W d    n1 s0    Y  qjtjdkrd
|_td|d}| |d  d | |d t| d S )Nr  u   abcd)r   latin1rV   r   r   rA   win32mbcsZoemr   r   r  r   r   i  r|   r   r   r   )r   rC   rD   r   r  platformr   r   r   rX   r   r1   r   r0   r   r   r   Zcode_page_encoderW   r   )r   r  r   	encodingsr   r   r   encodedr   r   r   test_encodehelper_bug36819"  sH    

*..6z,CodecCallbackTest.test_encodehelper_bug36819c                 C   sB   G dd dt }| tdjdtjd i | tdjddi d S )Nc                   @   s   e Zd Zdd ZdS )z1CodecCallbackTest.test_translatehelper.<locals>.Dc                 S   s   t d S r   r  r  r   r   r   r  U  s    z=CodecCallbackTest.test_translatehelper.<locals>.D.__getitem__Nr  r   r   r   r   r  T  s   r  r   r  r   r   )rc   rX   r  	translater  r  r7   )r   r  r   r   r   test_translatehelperN  s    z&CodecCallbackTest.test_translatehelperc                 C   sD   t ddt ddt ddt ddi}d	D ]}d
| }|| q(d S )N&z&amp;<z&lt;>z&gt;"z&quot;)r   r   r   ry   zabc<def>ghi)r:   r)  )r   rf   r   textr   r   r   test_bug828737[  s    z CodecCallbackTest.test_bug828737c              	   C   s   g d}dd }t d| |D ]>\}}| t ||d W d    q 1 sT0    Y  q dd }t d| |D ]\}}| ||dd qxd S )	N)r   )r   s   ++r   )r   r   )r   r   )rq      \u123g)r  r1  c                 S   s$   t | trd| _dS td|  d S )Nr      䉂r   r4   r6   r$   r   r7   rT   r   r   r   	replacingr  s    
z?CodecCallbackTest.test_mutatingdecodehandler.<locals>.replacingr   c                 S   s$   t | trd| _dS td|  d S )Nr#   r2  r4   r4  rT   r   r   r   mutating~  s    
z>CodecCallbackTest.test_mutatingdecodehandler.<locals>.mutatingztest.mutatingr3  )rC   rD   rX   r7   rW   r0   )r   Zbaddatar5  r   datar6  r   r   r   test_mutatingdecodehandlerg  s    
,z,CodecCallbackTest.test_mutatingdecodehandlerc                 C   s   dd }t d| | dddd | dddd | d	d
dd | dddd dd }t d| | dddd | dddd d S )Nc                 S   s(   t | trd| jd fS td|  d S )Nr   r   r4   )r6   r$   r   r7   rT   r   r   r   forward_shorter_than_end  s    
zPCodecCallbackTest.test_crashing_decode_handler.<locals>.forward_shorter_than_endztest.forward_shorter_than_ends      z	utf-16-leu   ����Ø s      z	utf-16-bes         z	utf-32-leu   ���ᄑ s         z	utf-32-bec                 S   s*   t | trd| _d| jfS td|  d S )Ns           r   r4   )r6   r$   r   r   r7   rT   r   r   r   replace_with_long  s    

zICodecCallbackTest.test_crashing_decode_handler.<locals>.replace_with_longztest.replace_with_longr   r   u   �    r   u   �  )rC   rD   r0   rW   )r   r9  r:  r   r   r   test_crashing_decode_handler  sL    

z.CodecCallbackTest.test_crashing_decode_handlerc                    s"  t jt jt jt jt jt jt dt dg}tt	t
fD ] G  fdddt}|D ]B}| j| d  | t||  W d    qR1 s0    Y  qRG  fdddt}|D ]l}| j| dH | t|f ||  W d    n1 s0    Y  W d    q1 s0    Y  qq6d S )Nr   r   c                       s   e Zd Z ZdS zACodecCallbackTest.test_fake_error_class.<locals>.FakeUnicodeErrorNr   r   r   	__class__r   clsr   r   FakeUnicodeError  s   rA  )r  Zerror_classc                       s   e Zd Z ZdS r<  r=  r   r?  r   r   rA    s   )rC   r   r   r   r   r   r   r   r   r$   r)   r   r   rX   r7   r   )r   handlersrA  r  r   r?  r   test_fake_error_class  s&    
0z'CodecCallbackTest.test_fake_error_classN)+r   r   r   r3   rG   rK   rO   rQ   rY   rg   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r!  r(  r*  r0  r8  r;  rC  r   r   r   r   r,   P   sP   9 (%:.,H;6,$3r,   __main__)rC   Zhtml.entitiesr9   r   r  rH   Zunittestr   r   r   r   r    r$   r!   r&   r)   r'   r*   r+   ZTestCaser,   r   mainr   r   r   r   <module>   s4            