B
    u9a                @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
ZddlZddlZddl
mZmZ ejjrdd Zndd ZG dd dZG d	d
 d
ZG dd dee	jZG dd dee	jZG dd de	jZG dd de	jZG dd dejjZG dd dee	jZG dd dee	j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e	jZ%G d#d$ d$ee	jZ&e'd%kre	(  dS )&zUnit tests for the bytes and bytearray types.

XXX This is a mess.  Common tests should be unified with string_tests.py (and
the latter should be modernized).
    N)bigaddrspacetestMAX_Py_ssize_tc                s   t   fdd}|S )Nc           	      s&   t jdtf  | |S Q R X d S )N )testsupportcheck_warningsBytesWarning)argskw)func  /usr/lib/python3.7/test_bytes.pywrapper   s    z%check_bytes_warnings.<locals>.wrapper)	functoolswraps)r   r   r   )r   r   check_bytes_warnings   s    r   c             C   s   | S )Nr   )r   r   r   r   r       s    c               @   s   e Zd ZdddZdd ZdS )	Indexabler   c             C   s
   || _ d S )N)value)selfr   r   r   r   __init__%   s    zIndexable.__init__c             C   s   | j S )N)r   )r   r   r   r   	__index__'   s    zIndexable.__index__N)r   )__name__
__module____qualname__r   r   r   r   r   r   r   $   s   
r   c               @   s  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edd Zdd Zdd Ze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,dQdR Z-dSdT Z.dUdV Z/dWdX Z0dYdZ Z1d[d\ Z2d]d^ Z3d_d` Z4dadb Z5dcdd Z6dedf Z7dgdh Z8didj Z9dkdl Z:dmdn Z;dodp Z<dqdr Z=dsdt Z>dudv Z?dwdx Z@dydz ZAd{d| ZBd}d~ ZCdd ZDdd ZEdS )BaseBytesTestc             C   s.   |   }| t|| j  | |j| j  d S )N)	type2testassertEqualtype	__class__)r   br   r   r   test_basics-   s    zBaseBytesTest.test_basicsc             C   sL   |  d}x<tjtjfD ],}||}| || | t|t| qW d S )Ns   abcd)r   copydeepcopyr   r   )r   acopy_methodr   r   r   r   	test_copy2   s
    
zBaseBytesTest.test_copyc                s   |    | t d | t fdd | t fdd | t fdd | t fdd | t fdd | t fdd | t fd	d | t fd
d | t fdd | t fdd | t fdd d S )Nr   c                  s    d S )Nr   r   r   )r   r   r   <lambda><       z3BaseBytesTest.test_empty_sequence.<locals>.<lambda>c                  s    d S )N   r   r   )r   r   r   r&   =   r'   c                  s
    t j S )N)sysmaxsizer   )r   r   r   r&   >   r'   c                  s    t jd  S )Nr(   )r)   r*   r   )r   r   r   r&   ?   r'   c                  s    dd  S )N
   d   r   r   )r   r   r   r&   @   r'   c                  s    d S )Nr   r   )r   r   r   r&   A   r'   c                  s    d S )Nr   r   )r   r   r   r&   B   r'   c                  s    t j  S )N)r)   r*   r   )r   r   r   r&   C   r'   c                  s    t j d  S )Nr(   )r)   r*   r   )r   r   r   r&   D   r'   c                  s    t j d  S )N   )r)   r*   r   )r   r   r   r&   E   r'   c                  s    dd   S )Nr+   r,   r   r   )r   r   r   r&   F   r'   )r   r   lenassertRaises
IndexError)r   r   )r   r   test_empty_sequence9   s    z!BaseBytesTest.test_empty_sequencec             C   s  |  td}| t|d | t|ttd |  dh}| |d |  ddh}| t|ddh |  ttd}| t|d | t|ttd |  dd	 tdD }| t|d
 | t|ttddd d  G dd d}|  | }| |d d S )N   *      *+   -   )r7   r8   )r8   r7   c             s   s   | ]}|d  r|V  qdS )r/   Nr   ).0ir   r   r   	<genexpr>Y   s    z3BaseBytesTest.test_from_iterable.<locals>.<genexpr>   r(   r/   c               @   s   e Zd Zdd ZdS )z+BaseBytesTest.test_from_iterable.<locals>.Sc             S   s   d| S )N)r(   r/      r   )r   r:   r   r   r   __getitem___   s    z7BaseBytesTest.test_from_iterable.<locals>.S.__getitem__N)r   r   r   r>   r   r   r   r   S^   s   r?   s   )r   ranger   r0   listassertIntupleiter)r   r   r?   r   r   r   test_from_iterableH   s     "z BaseBytesTest.test_from_iterablec             C   sT   |  ttd}| t|d | t|ttd |  d}| |d d S )Nr4   )r(   r/   r=   s   )r   rC   r@   r   r0   rA   )r   r   r   r   r   test_from_tupled   s
    
zBaseBytesTest.test_from_tuplec             C   sZ   |  ttd}| t|d | t|ttd |  dddg}| |d d S )Nr4   r(   r/   r=   s   )r   rA   r@   r   r0   )r   r   r   r   r   test_from_listl   s
    zBaseBytesTest.test_from_listc                s\   G  fddd}| | g |  t d G  fddd}| g |  t d d S )Nc                   s   e Zd Z fddZdS )z0BaseBytesTest.test_from_mutating_list.<locals>.Xc                s       dS )Nr5   )clear)r   )r#   r   r   r   w   s    z:BaseBytesTest.test_from_mutating_list.<locals>.X.__index__N)r   r   r   r   r   )r#   r   r   Xv   s   rI   r6   c                   s   e Zd Z fddZdS )z0BaseBytesTest.test_from_mutating_list.<locals>.Yc                s   t  dk r |  dS )Ni  r5   )r0   append)r   )r#   r   r   r   ~   s    
z:BaseBytesTest.test_from_mutating_list.<locals>.Y.__index__N)r   r   r   r   r   )r#   r   r   Y}   s   rK   s  ****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************)r   bytes)r   rI   rK   r   )r#   r   test_from_mutating_listt   s    z%BaseBytesTest.test_from_mutating_listc             C   sh   |  t tdtdtdg}| t|ddddg | t| j tdg | t| j tdg d S )Nr(         r   r-   r4   )r   r   r   rA   r1   
ValueError)r   r   r   r   r   test_from_index   s
    zBaseBytesTest.test_from_indexc             C   sd   |  tddddg}| |d |  d}| |d G dd dt}| |  |dd d S )NBr(   r/   r=   s   c               @   s   e Zd Zdd ZdS )z)BaseBytesTest.test_from_buffer.<locals>.Bc             S   s   t d S )N)	TypeError)r   r   r   r   r      s    z3BaseBytesTest.test_from_buffer.<locals>.B.__index__N)r   r   r   r   r   r   r   r   rR      s   s   foobar)r   arrayr   rL   )r   r#   rR   r   r   r   test_from_buffer   s    
zBaseBytesTest.test_from_bufferc             C   s   |  | dd |  | dd |  | dd | t| jd |  | dd	d
 |  | d
d
 | t| jtjd  d S )Nr   r'   r(          s        r-   0ascii   0)r   r   r1   rP   OverflowErrorr)   r*   )r   r   r   r   test_from_ssize   s    zBaseBytesTest.test_from_ssizec             C   s  |  t| jd G dd d}|  t| jdg |  t| jdg |  t| jd g |  t| j| g |  t| jdd |  t| jdd | j t| jddd	 | j t| jddd	 |  t| jd
 | j t| jd
dd	 |  t| jd
d |  t| jd
dd d S )Ng        c               @   s   e Zd ZdS )z5BaseBytesTest.test_constructor_type_errors.<locals>.CN)r   r   r   r   r   r   r   C   s   r]   rX   r   rY   r'   ignore)errorsr   s   asciis   ignore)r1   rS   r   )r   r]   r   r   r   test_constructor_type_errors   s    z*BaseBytesTest.test_constructor_type_errorsc             C   s   |  t| jdg |  t| jtj g |  t| jtj d g |  t| jtj d g |  t| jdd  g |  t| jdg |  t| jdg |  t| jtjg |  t| jtjd g |  t| jdd g d S )Nr-   r(   r/   r+   r,   r4   i  )r1   rP   r   r)   r*   )r   r   r   r   test_constructor_value_errors   s    z+BaseBytesTest.test_constructor_value_errorsc          	   C   sF   t }| ttf| j| yt|d  W n ttfk
r@   Y nX d S )N   )r   r1   r[   MemoryErrorr   	bytearray)r   sizer   r   r   test_constructor_overflow   s    z'BaseBytesTest.test_constructor_overflowc             C   sX   G dd d}|  t| j|  |  t| j| g G dd d}|  t| j|  d S )Nc               @   s   e Zd Zdd ZdS )z9BaseBytesTest.test_constructor_exceptions.<locals>.BadIntc             S   s   dd  d S )Nr(   r   r   )r   r   r   r   r      s    zCBaseBytesTest.test_constructor_exceptions.<locals>.BadInt.__index__N)r   r   r   r   r   r   r   r   BadInt   s   rg   c               @   s   e Zd Zdd ZdS )z>BaseBytesTest.test_constructor_exceptions.<locals>.BadIterablec             S   s   dd  d S )Nr(   r   r   )r   r   r   r   __iter__   s    zGBaseBytesTest.test_constructor_exceptions.<locals>.BadIterable.__iter__N)r   r   r   rh   r   r   r   r   BadIterable   s   ri   )r1   ZeroDivisionErrorr   )r   rg   ri   r   r   r   test_constructor_exceptions   s
    z)BaseBytesTest.test_constructor_exceptionsc             C   s  |  dddg}|  dddg}|  ddg}| || | ||k | ||k | ||k | ||k  | ||k | ||k | ||k | ||k | ||k | ||k | ||k | ||k | ||k  | ||k  | ||k d S )Nr(   r/   r=   )r   r   
assertTrueassertFalse)r   b1b2Zb3r   r   r   test_compare   s&    zBaseBytesTest.test_comparec             C   s   |  | ddkd |  | ddkd |  | ddkd |  | ddkd |  |  t kd |  |  t kd d S )Ns    a b cabcFs      a   b   cs   a b c s   a   b   c   T)r   r   str)r   r   r   r   test_compare_to_str   s    z!BaseBytesTest.test_compare_to_strc             C   s<   t ttd}| |}t t|}|  | || d S )NHello)rA   mapordr   reversedreverser   )r   inputr   outputr   r   r   test_reversed   s
    
zBaseBytesTest.test_reversedc                sP   fdd}|d}  |d d |d   |dd |d   |dd |d	   |dd  |d
   |dd |d
   |dd |d
   |d d |d   |dd |d   |dd |d	   |dd  |d
   |dd |d
   |dd |d
   |dd |d d S )Nc                s     tt| S )N)r   ru   rv   )s)r   r   r   by  s    z'BaseBytesTest.test_getslice.<locals>.byzHello, worldrW   rt   r(   Zello   z, world   r,   iii)r   )r   r}   r   r   )r   r   test_getslice  s    zBaseBytesTest.test_getslicec             C   s   t td}| |}dd ddddtjddd	d
f}xV|D ]N}xH|D ]@}x:|dd  D ]*}| |||| | ||||  qTW qBW q8W d S )NrO   r   r(   r=      r,   r-   r.   ii)rA   r@   r   r)   r*   r   )r   Lr   indicesstartstopstepr   r   r   test_extended_getslice  s    


z$BaseBytesTest.test_extended_getslicec          	   C   sv   d}x0dD ](}|  ||}| ||  || q
W | t| j |d |  |dd}| ||  |d d d d S )Nu   Hello world
ሴ噸骼)zutf-8zutf-16zlatin-1r^   zutf-8)r   r   encoder1   UnicodeEncodeError)r   sampleencr   r   r   r   test_encoding%  s    
zBaseBytesTest.test_encodingc             C   s   d}x*dD ]"}|  ||}| ||| q
W d}|  |d}| t|jd | |ddd | |jdddd | |  d	 d
 d S )Nu   Hello world
ሴ噸骼)zutf-8zutf-16u   Hello world
þÿzlatin-1zutf-8r^   zHello world
)r_   encodings   ☃u   ☃)r   r   decoder1   UnicodeDecodeError)r   r   r   r   r   r   r   test_decode.  s    
zBaseBytesTest.test_decodec             C   sb   |  d}| ||    |  d}| ||  dgd  |  d}| ||  dgd  d S )Nr   r+   i'  )r   r   )r   r   r   r   r   test_from_int<  s    


zBaseBytesTest.test_from_intc                sx   |  d |  d|   d |  td d | td  d | t fdd | tfdd d S )Ns   abcs   defs   abcdefs   defabcc                  s    d S )Ndefr   r   )rn   r   r   r&   J  r'   z+BaseBytesTest.test_concat.<locals>.<lambda>c                  s   d  S )Nrq   r   r   )ro   r   r   r&   K  r'   )r   r   rL   r1   rS   )r   r   )rn   ro   r   test_concatD  s    

zBaseBytesTest.test_concatc          
      s   xd|  dfD ] |  d d |  d d |  d d | t fdd | t fd	d | ttf  tj }W d Q R X | ttf  tj9  W d Q R X qW d S )
Ns   abcr=   s	   abcabcabcr   r'   r-   c                  s    d S )NgQ	@r   r   )r   r   r   r&   R  r'   z+BaseBytesTest.test_repeat.<locals>.<lambda>c                  s   d  S )NgQ	@r   r   )r   r   r   r&   S  r'   )r   r   r1   rS   r[   rc   r)   r*   )r   cr   )r   r   test_repeatM  s    zBaseBytesTest.test_repeatc             C   s*   |  | dd | tdgd  d S )N   xr,   x)r   r   rv   )r   r   r   r   test_repeat_1charZ  s    zBaseBytesTest.test_repeat_1charc                sx  |  d | td  | ttd  | d  | t fdd | t fdd | t fdd | t fdd | t fd	d | t fd
d xtt	fD ]}| |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  qW d S )Ns   abcr#      c                  s   d kS )Ni,  r   r   )r   r   r   r&   b  r'   z-BaseBytesTest.test_contains.<locals>.<lambda>c                  s   d kS )Nr-   r   r   )r   r   r   r&   c  r'   c                  s   t jd  kS )Nr(   )r)   r*   r   )r   r   r   r&   d  r'   c                  s   d  kS )Nr   r   )r   r   r   r&   e  r'   c                  s   t td kS )Nr#   )floatrv   r   )r   r   r   r&   f  r'   c                  s   d kS )Nr#   r   r   )r   r   r   r&   g  r'   r'      a   b   cs   abs   bcs   ac   ds   dabs   abd)
r   rB   rv   intZassertNotInr1   rP   rS   rL   rd   )r   fr   )r   r   test_contains]  s,    
zBaseBytesTest.test_containsc          
   C   s  |  t| jj |  t| jjd | | jd|   tdddg}| | jd| | | jd| | | jd| x$d	D ]}| | j||   qW xd
D ]}|  t| jj| qW | | jdd |  t| jjd |  t| jjd |  t| jjd |  t| jjd |  t| jjd |  t| jjd xJdD ]B\}}|  t}| j| W d Q R X | d| t|j	 qPW d S )Nr(   r      r7   0   1a2B30z  1A 2B  30   z
 1A
2B	30z	
 u       Z0000s     s   1Br#   Zrtz1a b cd z12       34))z12 x4 56r=   )z12 3x 56rb   )z12 xy 56r=   )u	   12 3ÿ 56rb   zat position %s)
r1   rS   r   fromhexr   rd   rP   rB   rr   	exception)r   r   r   dataposcmr   r   r   test_fromhexu  s.    

zBaseBytesTest.test_fromhexc             C   s   |  t| jj |  t| jjd | | d d | tdddg d | | d d | td d d S )	Nr(   r'   r   r   r7   r   Z1a2b30s   +0)r1   rS   r   hexr   rd   
memoryview)r   r   r   r   test_hex  s    zBaseBytesTest.test_hexc          	   C   s  |  | dg d |  | ddgd xdgddgddgdddggD ]d}tt| j|}|  | d|d |  | dt|d |  | dt|d qPW | dj}|  |dd	gd
 |  |tdd	gd
 |  |dtd	gd
 |  |tdd	gd
 |  |dtd	gd
 dgd }ddd  }|  ||| | 	t
| djd  | 	t
 |tdddg W d Q R X | 	t
 |tdddg W d Q R X d S )Nr'   s   abcr   s   bcs   abr   r   s   .:s   cds   ab.:cdi  s   .:abci      Zcds   ef)r   r   joinrA   ru   rC   rD   r   rd   r1   rS   )r   ZlstZdot_joinseqZexpectedr   r   r   	test_join  s*    " 
zBaseBytesTest.test_joinc             C   s  |  d}d}d}d}| |dd | |dd | |d	d
 | ||d | ||d
 | |ddd | |ddd | |dddd | |dddd | ||dd | ||dd | ||ddd | ||ddd d S )Ns   mississippii   p   w      irb   s   ssr/      wr         pr(   r=   r~   	   )r   r   count)r   r   r:   pwr   r   r   
test_count  s"    
zBaseBytesTest.test_countc          	   C   s   |  d}| |   d | |d | |d | |d | |d | |d | t}|dg W d Q R X t|j}| d| | d| d S )	Ns   hellos   anythings   hel   hs   hellows   harL   rC   )	r   rm   
startswithrl   r1   rS   rr   r   rB   )r   r   r   excr   r   r   test_startswith  s    

zBaseBytesTest.test_startswithc          	   C   s   |  d}| t d | |d | |d | |d | |d | |d | t}|dg W d Q R X t|j}| 	d| | 	d| d S )	Ns   hellos   anythings   llo   os   whellos   norL   rC   )
r   rm   rd   endswithrl   r1   rS   rr   r   rB   )r   r   r   r   r   r   r   test_endswith  s    

zBaseBytesTest.test_endswithc             C   s  |  d}d}d}| |dd | |dd | |dd | ||d	 | ||d | |dd
d | |dd	dd | |dd	d
d | ||dd | ||d	d
d	 | ||d	d
d x*ddtjd	 fD ]}| td|j| qW d S )Ns   mississippir   r   s   ssr/   r   r-   s   mississippianr(   r=   rW   r~   r   r4   zbyte must be in range\(0, 256\))r   r   findr)   r*   assertRaisesRegexrP   )r   r   r:   r   indexr   r   r   	test_find  s$    
zBaseBytesTest.test_findc             C   s   |  d}d}d}| |dd | |dd | |dd | ||d	 | ||d | |dd
d | |dddd | ||dd
d | ||d
dd | ||dd
d d S )Ns   mississippir   r   s   ssrW   r   r-   s   mississippianr+   r=   r   r   r/   r(   r   r~   )r   r   rfind)r   r   r:   r   r   r   r   
test_rfind  s    
zBaseBytesTest.test_rfindc             C   s   |  d}d}d}| |dd | t|jd | t|jd | ||d | t|j| | |dd	d
 | |dddd | t|jddd	 | ||dd | ||dd	d | t|j|dd	 d S )Ns   mississippir   r   s   ssr/   r   s   mississippianr(   r=   rW   r~   r   )r   r   r   r1   rP   )r   r   r:   r   r   r   r   
test_index  s    
zBaseBytesTest.test_indexc             C   s   |  d}d}d}| |dd | t|jd | t|jd | ||d | t|j| | |dd	d | |dd
dd | ||dd	d | ||d	dd | t|j|dd	 d S )Ns   mississippir   r   s   ssrW   r   s   mississippianr+   r=   r   r   r/   r(   r   r~   )r   r   rindexr1   rP   )r   r   r:   r   r   r   r   test_rindex/  s    
zBaseBytesTest.test_rindexc             C   s   |  d}|}|d }| |d | |d | ||k |  d}|d }| |d | t|| j  |  d}|d }| |d | t|| j  d S )	Ns
   hello, %b!s   worlds   hello, world!s   %s / 100 = %d%%)s   seventy-nineO   s   seventy-nine / 100 = 79%s
   hello, %b!s   hello, world!)r   r   rm   assertIsr   )r   r   origr#   r   r   r   test_modB  s    


zBaseBytesTest.test_modc             C   s   |  d}|}|d; }| |d | |d | ||k |  d}|d; }| |d | t|| j  |  d}|d; }| |d | t|| j  d S )	Ns
   hello, %b!s   worlds   hello, world!s   %s / 100 = %d%%)s   seventy-niner   s   seventy-nine / 100 = 79%s
   hello, %b!s   hello, world!)r   r   rm   r   r   )r   r   r   r   r   r   	test_imodS  s    


zBaseBytesTest.test_imodc          	   C   sB   |  t t | d  W d Q R X | | ddt d S )Ns   abcz%r)r1   rS   objectr   r   __rmod__NotImplemented)r   r   r   r   	test_rmodd  s    zBaseBytesTest.test_rmodc             C   s6   |  d}| |ddd | |ddd d S )Ns   mississippir   r   s   massassappas   ssr   s	   mixixippi)r   r   replace)r   r   r   r   r   test_replacei  s    
zBaseBytesTest.test_replacec             C   s   |  t| djdd d S )Ns   a b    r'   )r1   rS   r   r   )r   r   r   r   test_replace_int_errorn  s    z$BaseBytesTest.test_replace_int_errorc             C   s0   |  t| djd |  t| djd d S )Ns   a b )r1   rS   r   splitrsplit)r   r   r   r   test_split_string_errorq  s    z%BaseBytesTest.test_split_string_errorc             C   s0   |  t| djd |  t| djd d S )Ns   a br   )r1   rS   r   r   r   )r   r   r   r   test_split_int_erroru  s    z"BaseBytesTest.test_split_int_errorc             C   sJ   x(dD ] }|  |}| | |g qW |  d}| | dg d S )N)s   abs   abs   abs   abs	   	
s   )r   r   r   )r   r   r   r   r   test_split_unicodewhitespacey  s
    


z*BaseBytesTest.test_split_unicodewhitespacec             C   s    |  d}| | dg d S )Ns	   	
s   )r   r   r   )r   r   r   r   r   test_rsplit_unicodewhitespace  s    
z+BaseBytesTest.test_rsplit_unicodewhitespacec             C   s2   |  d}| |dd | |dd d S )Ns   mississippis   ss)s   mis   sss   issippir   )s   mississippir'   r'   )r   r   	partition)r   r   r   r   r   test_partition  s    
zBaseBytesTest.test_partitionc             C   sD   |  d}| |dd | |dd | |dd d S )Ns   mississippis   ss)s   missis   sss   ippir   )s
   mississippr   r'   r   )r'   r'   s   mississippi)r   r   
rpartition)r   r   r   r   r   test_rpartition  s    
zBaseBytesTest.test_rpartitionc             C   s0   |  t| djd |  t| djd d S )Ns   a br   )r1   rS   r   r   r   )r   r   r   r   test_partition_string_error  s    z)BaseBytesTest.test_partition_string_errorc             C   s0   |  t| djd |  t| djd d S )Ns   a br   )r1   rS   r   r   r   )r   r   r   r   test_partition_int_error  s    z&BaseBytesTest.test_partition_int_errorc             C   sV   xPt tjd D ]>}x8dD ]0}| |}t||}t|}| || qW qW d S )Nr(   )r'   r   s   abcs   abs       )r@   pickleHIGHEST_PROTOCOLr   dumpsloadsr   )r   protor   Zpsqr   r   r   test_pickling  s    


zBaseBytesTest.test_picklingc             C   s   xt tjd D ]}xdD ]}t| | }}t| |}t||}t|}| t	|t	| | t|| t|}|sqt
| t||}t|}| t||dd   qW qW d S )Nr(   )r'   r   s   abcs   abs       )r@   r   r   rD   r   rA   r   r   r   r   next)r   r   r   itZitorgr   dr   r   r   test_iterator_pickling  s    



z$BaseBytesTest.test_iterator_picklingc             C   sX   |  | dtdd |  | dtdd |  | dtdd d S )Ns   abcs   acr   s   bcs   ab)r   r   stripr   lstriprstrip)r   r   r   r   test_strip_bytearray  s    z"BaseBytesTest.test_strip_bytearrayc             C   sF   |  t| djd |  t| djd |  t| djd d S )Ns   abcZac)r1   rS   r   r   r   r   )r   r   r   r   test_strip_string_error  s    z%BaseBytesTest.test_strip_string_errorc             C   sF   |  t| djd |  t| djd |  t| djd d S )Ns    abc r   )r1   rS   r   r   r   r   )r   r   r   r   test_strip_int_error  s    z"BaseBytesTest.test_strip_int_errorc             C   s>   |  d}x.ttfD ]"}| |d|d|  d qW d S )Ns   abcr~      -s   --abc--)r   rL   rd   r   center)r   r   	fill_typer   r   r   test_center  s    
zBaseBytesTest.test_centerc             C   s>   |  d}x.ttfD ]"}| |d|d|  d qW d S )Ns   abcr~   r   s   abc----)r   rL   rd   r   ljust)r   r   r   r   r   r   
test_ljust  s    
zBaseBytesTest.test_ljustc             C   s>   |  d}x.ttfD ]"}| |d|d|  d qW d S )Ns   abcr~   r   s   ----abc)r   rL   rd   r   rjust)r   r   r   r   r   r   
test_rjust  s    
zBaseBytesTest.test_rjustc             C   sL   |  t| djdd |  t| djdd |  t| djdd d S )Ns   abcr~   r   )r1   rS   r   r   r  r  )r   r   r   r   test_xjust_int_error  s    z"BaseBytesTest.test_xjust_int_errorc                s:   |  d |  fddtt D dddddg d S )	Ns    Ac                s    g | ]}t  ||d   qS )r(   )rv   )r9   r:   )r   r   r   
<listcomp>  s    z*BaseBytesTest.test_ord.<locals>.<listcomp>r   A      r<   rO   )r   r   r@   r0   )r   r   )r   r   test_ord  s    
zBaseBytesTest.test_ordc             C   s`   d}|  | jdd| d}|  | jdd| | t| jjdd | t| jjdd d S )	Ns    	
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`xyzdefghijklmnopqrstuvwxyz{|}~s   abcs   xyzs    	
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~xyzs   s   xyzqrq   r   )r   r   	maketransr1   rP   rS   )r   Z
transtabler   r   r   test_maketrans  s    zBaseBytesTest.test_maketransc             C   s  |  d}|  d}|  d}|  d}|  d}| d||d  | d||dd  | d||d d | d	||d d  | d||d  | d||dd  | d||d d | d	||d d  | d||d  | d||dd  | d||d d | d	||d d  | d||d  | d||dd  | d||d d | d	||d d  | d||d  | d
||dd  | d
||d d | d	||d d  | d||d  | d||dd  | d||d d | d||d d  | d||d  | d||dd  | d||d d | d||d d  d S )Ns   hello   lr   r   r   r/   r=   r.   r   r(   TF)	r   r   r   r   r   r   r   r   r   )r   r   lhr   or   r   r   test_none_arguments  sB    




z!BaseBytesTest.test_none_argumentsc             C   sZ   |  d}xJ|j|j|j|j|jfD ].}| t|d | t|d | t|d q$W d S )Ns   hellor-   r4   i'  )r   r   r   r   r   r   r1   rP   )r   r   methodr   r   r   (test_integer_arguments_out_of_byte_range  s
    
z6BaseBytesTest.test_integer_arguments_out_of_byte_rangec          	   C   s   |  d}|  d}| td|j|d d d  | td|j|d d d  | td|j|d d d  | td|j|d d d  | td|j|d d d  | td|j|d d d  | td	|j	|d d d  d S )
Ns   hellor   z\bfind\bz	\brfind\bz	\bindex\bz
\brindex\bz	\bcount\bz\bstartswith\bz\bendswith\b)
r   r   rS   r   r   r   r   r   r   r   )r   r   r   r   r   r   *test_find_etc_raise_correct_error_messages  s     

z8BaseBytesTest.test_find_etc_raise_correct_error_messagesc             C   s(   t j| t| j t j| t| j d S )N)r   r   Zcheck_free_after_iteratingrD   r   rw   )r   r   r   r   test_free_after_iterating-  s    z'BaseBytesTest.test_free_after_iteratingc             C   s4  |  d}ttd}td|td< | t|j | t|jd d  | t|jttd ||d}| 	|d | 
|| j  ||}||d}| 	|| | 	|d ||d}| 	|d	 |d d
}| 	|d |j|dd}| 	|d |j|dd}| 	|d	 |jd d
d}| 	|d d S )Ns   hellor4   er  rO   r'   s   heller  s   hee   es   hllo)delete)r   rd   r@   rv   r1   rS   	translaterP   rL   r   assertIsInstance)r   r   Zrosettar   r   r   r   r   test_translate1  s.    

zBaseBytesTest.test_translateN)Fr   r   r   r    r%   r3   rE   rF   rG   rM   rQ   rU   r\   r`   ra   r   rf   rk   rp   r   rs   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   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   +   s   
		%+r   c               @   s8   e Zd ZeZdd Zdd Zdd Zdd Zd	d
 Z	dS )	BytesTestc          	   C   s(   d}|  t| dd  W d Q R X d S )Nz'byte indices must be integers or slicess   pythonr#   )r   rS   )r   msgr   r   r   test_getitem_errorT  s    zBytesTest.test_getitem_errorc          	   C   s<   t tt j}t|ddd}| t|jd W d Q R X d S )Nrbr   )	bufferingr'   )osopen__file__O_RDONLYr1   rS   readinto)r   fdr   r   r   r   test_buffer_is_readonlyY  s    z!BytesTest.test_buffer_is_readonlyc             C   s  G dd d}|  t| d G dd d}| tt|  G dd d}| tt|  G dd d}|  t| d G dd dt}|  t|d	d |  t|d	d
d G dd d}|  t| d | tt| t |  t| d | tt| t d S )Nc               @   s   e Zd Zdd ZdS )z BytesTest.test_custom.<locals>.Ac             S   s   dS )Ns   abcr   )r   r   r   r   	__bytes__`  s    z*BytesTest.test_custom.<locals>.A.__bytes__N)r   r   r   r'  r   r   r   r   A_  s   r(  s   abcc               @   s   e Zd ZdS )z BytesTest.test_custom.<locals>.AN)r   r   r   r   r   r   r   r(  c  s    c               @   s   e Zd Zdd ZdS )z BytesTest.test_custom.<locals>.Ac             S   s   d S )Nr   )r   r   r   r   r'  f  s    z*BytesTest.test_custom.<locals>.A.__bytes__N)r   r   r   r'  r   r   r   r   r(  e  s   c               @   s   e Zd Zdd Zdd ZdS )z BytesTest.test_custom.<locals>.Ac             S   s   dS )Nr   r   )r   r   r   r   r'  j  s    z*BytesTest.test_custom.<locals>.A.__bytes__c             S   s   dS )Nr5   r   )r   r   r   r   r   l  s    z*BytesTest.test_custom.<locals>.A.__index__N)r   r   r   r'  r   r   r   r   r   r(  i  s   r   c               @   s   e Zd Zdd ZdS )z BytesTest.test_custom.<locals>.Ac             S   s   dS )Ns   abcr   )r   r   r   r   r'  q  s    z*BytesTest.test_custom.<locals>.A.__bytes__N)r   r   r   r'  r   r   r   r   r(  p  s   u   €z
iso8859-15   c               @   s   e Zd Zdd ZdS )z BytesTest.test_custom.<locals>.Ac             S   s   t dS )Ns   abc)OtherBytesSubclass)r   r   r   r   r'  w  s    z*BytesTest.test_custom.<locals>.A.__bytes__N)r   r   r   r'  r   r   r   r   r(  v  s   )	r   rL   r1   rS   rr   r   r   r*  BytesSubclass)r   r(  r   r   r   test_custom^  s     zBytesTest.test_customc                st  t jd}t jd}ddlm}m} ddlm}m}m}m	}m
}	m}
m} |j}||_| |dd | |ddd	 | |d
|dd | |d
|tdd | |d
|dd | |d|d|d|	dd | |d|d|d|	dd | |d|d|d|	dd | |d|dd | |d|d d! | |d"|d#d$ ||}tjd%krd&d|   fd'd(}nd)d( }d*}| |d+||d,|| d- | |d.|d/d0 |	dj}xd1||jtfd1||jtfd2||jtfd2||jtfd3||jtfd4|
|jtfd4|
|jtfd5|	|tfd6|||ff	D ]0\}}}}| ||||||d-f qXW | |d7d8d8 | |d9d:d; | |d<d= | |d>d? | |d@|tdAdB | |dCdD | |d=d= | |dE|d|ddF | t |dG|d | t |dG|dH | |dIdI | |dDdIdI d S )JNctypes	_testcapir   )	pythonapi	py_object)c_intc_uintc_longc_ulongc_size_t	c_ssize_tc_char_ps   formats
   Hello %s !s   worlds   Hello world !s   c=%cs   c= @s   c=@rO   s   c=s   d=%d ld=%ld zd=%zdr(   r/   r=   s   d=1 ld=2 zd=3r-   r.   r   s   d=-1 ld=-2 zd=-3s   u=%u lu=%lu zu=%zu{   i  i  s   u=123 lu=456 zu=789s   i=%is   i=123is   i=-123s   x=%xi
  s   x=abcntz0x%0{}Xc                s    |  S )Nr   )ptr)
ptr_formatr   r   ptr_formatter  s    z1BytesTest.test_from_format.<locals>.ptr_formatterc             S   s   d|  S )Nz%#xr   )r;  r   r   r   r=    s    iͫ s   ptr=%pzptr=rY   s   s=%ss   cstrs   s=cstrs   %ds   %lds   %lus   %zds   %zus   %ps   %5sr   s   %.3ss   abcdefs   abcs   %%   %s   [%%]s   [%]s   %%%c_s   %_s   %%ss   %ss   x=%i y=%s   x=2 y=%s   %cr4   r'   )!r   r   import_moduler-  r/  r0  r1  r2  r3  r4  r5  r6  r7  PyBytes_FromFormatZrestyper   rv   Zsizeofr   nameformatr   r   ZINT_MINrr   ZINT_MAXZLONG_MINZLONG_MAXZ	ULONG_MAXZPY_SSIZE_T_MINZPY_SSIZE_T_MAXr1   r[   )r   r-  r.  r/  r0  r1  r2  r3  r4  r5  r6  r7  rA  Z
sizeof_ptrr=  r;  Zsize_maxZ	formatstrZctypes_typer   Zpy_formatterr   )r<  r   test_from_format  s    $







zBytesTest.test_from_formatc             C   s   G dd dt }ddddg}| t|d | tt|| G dd	 d	t}| tdd
 | tt|d G dd dt}| tdd | tt|d G dd dt}td|d }}| t|d | tt| d S )Nc               @   s   e Zd ZdZdS )z7BytesTest.test_bytes_blocking.<locals>.IterationBlockedN)r   r   r   r'  r   r   r   r   IterationBlocked  s   rE  r   r(   r/   r=   s    c               @   s   e Zd ZdZdS )z1BytesTest.test_bytes_blocking.<locals>.IntBlockedN)r   r   r   r'  r   r   r   r   
IntBlocked  s   rF  s      c               @   s   e Zd ZdZdS )z;BytesTest.test_bytes_blocking.<locals>.BytesSubclassBlockedN)r   r   r   r'  r   r   r   r   BytesSubclassBlocked  s   rG  s   abc               @   s   e Zd ZdZdS )z4BytesTest.test_bytes_blocking.<locals>.BufferBlockedN)r   r   r   r'  r   r   r   r   BufferBlocked  s   rH  )rA   r   rL   r1   rS   r   rd   )r   rE  r:   rF  rG  rH  babbr   r   r   test_bytes_blocking  s    zBytesTest.test_bytes_blockingN)
r   r   r   rL   r   r  r&  r,  rD  rK  r   r   r   r   r  Q  s   !qr  c               @   s*  e Zd Ze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"e#j$j%d=d> Z&d?d@ Z'e#j(j)j*Z*dAdB Z+dCS )DByteArrayTestc          	   C   s,   d}|  t| tdd  W d Q R X d S )Nz,bytearray indices must be integers or slicess   pythonr#   )r   rS   rd   )r   r  r   r   r   r    s    z ByteArrayTest.test_getitem_errorc          	   C   s0   d}|  t| td}d|d< W d Q R X d S )Nz,bytearray indices must be integers or slicess   pythonZpythonr#   )r   rS   rd   )r   r  r   r   r   r   test_setitem_error  s    z ByteArrayTest.test_setitem_errorc             C   s   |  ttt  d S )N)r1   rS   hashrd   )r   r   r   r   test_nohash  s    zByteArrayTest.test_nohashc             C   s  d}|ddt |   }t }zt|d}|| W d Q R X t|d}td}||}W d Q R X | |t | | t|t| t|d}|| W d Q R X t|d}| |	 | W d Q R X W d yt
| W n tk
r   Y nX X d S )Ns   Hello world
rV      wbr  )r0   tempfileZmktempr!  writerd   r$  r   rA   readr   removeOSError)r   Zshort_sampler   Ztfnr   r   nr   r   r   test_bytearray_api   s&    z ByteArrayTest.test_bytearray_apic             C   s\   t d}| | d  | |d t d}|  | |d t  }|  | | d S )Ns   hellos   ollehs   hello1s   1olleh)rd   r   rx   rm   )r   r   r   r   r   test_reverse:  s    zByteArrayTest.test_reversec             C   st   t d}|  | |d t d}|  | |d t d}|td |  |td | |d d S )Ns   pythonr'   rr   r   )rd   rH   r   rJ   rv   )r   r   r   r   r   
test_clearE  s    zByteArrayTest.test_clearc             C   s   t d}| }| |d t d}| }| |d t d}| }| || | || |td | |d | |d d S )Ns   abcr'   r   s   abcd)rd   r!   r   assertIsNotrJ   rv   )r   r   rJ  r   r   r   r%   T  s    zByteArrayTest.test_copyc             C   s4   dd }|d}|  td||d|dg d S )Nc             S   s   t tt| S )N)rd   ru   rv   )r|   r   r   r   r}   g  s    z&ByteArrayTest.test_regexps.<locals>.byzHello, worlds   \w+rt   r   )r   refindall)r   r}   r   r   r   r   test_regexpsf  s    zByteArrayTest.test_regexpsc             C   sT  t dddg}d|d< | |t dddg d|d< | |t dddg td|d< | |t dddg yd|d< | d	 W n tk
r   Y nX yd|d
< | d	 W n tk
r   Y nX yd|d< | d W n tk
r   Y nX ytd|d< | d W n tk
r    Y nX yd |d< | d W n tk
rN   Y nX d S )Nr(   r/   r=   r,   r   r-   r+   r   zDidn't raise IndexErrorir4   zDidn't raise ValueErrorzDidn't raise TypeError)rd   r   r   failr2   rP   rS   )r   r   r   r   r   test_setiteml  s@    zByteArrayTest.test_setitemc             C   sl   t td}|d= | |t tdd |d= | |t tdd |d= | |t ddddd	d
dg d S )Nr+   r   r(   r-   r   rb   r/   r=   r   r~      )rd   r@   r   )r   r   r   r   r   test_delitem  s    zByteArrayTest.test_delitemc             C   s\  t td}| t|ttd t dddddg|dd< | |t ddddddddddg
 |dd	= | |t dddddg t ddd
ddg|dd< | |t td t ddg|dd< | |t ddd
dddddg ddddg|dd< | |t td dddg|dd< | |t ddd
ddddddddddg d|dd < | |t ddd
dddg td|d d< | |t ddddddg g |dd< | |t dddddg xPdd	dtdddddgddgg gg	D ](}| t ||dd< W d Q R X qW x>dddgdd ggD ](}| t ||dd< W d Q R X q,W d S )!Nr+   r(   r   rW   r   r~   rb  r   r   r/   r=   rb   r,   e   ir   r5   s   foof   o   gPKDrr   gffffff@r#   r   r   r   rN   rO   r4   i i(#  )	rd   r@   r   rA   r   r   r1   rS   rP   )r   r   elemr   r   r   test_setslice  s6    $
 *zByteArrayTest.test_setslicec             C   sv   t td}| t|ttd |d d= | t|ttdd |tdd | t|ttdd d S )Nr,   r+   n   )rd   r@   r   rA   extend)r   r   r   r   r   test_setslice_extend  s    
z"ByteArrayTest.test_setslice_extendc             C   s,   t d}|  |d d= |td7 }~d S )Nr+   r(   r/   )rd   poprL   )r   r   r   r   r   test_fifo_overrun  s
    
zByteArrayTest.test_fifo_overrunc             C   s2   t d}t|}|d d= | t|| d S )Nr+   r(   )rd   r)   	getsizeofassertLessEqual)r   r   re   r   r   r   test_del_expand  s    

zByteArrayTest.test_del_expandc             C   s   dd dddddd> t jddd	d
f}x|D ]}x|D ]}x|dd  D ]}ttd}t|}|||| }|  |||||< |||||< | |t| ||||= ||||= | |t| qDW q2W q(W d S )Nr   r(   r=   r   i,  iM  r-   r.   iirO   )r)   r*   rA   r@   rd   rx   r   )r   r   r   r   r   r   r   r   r   r   r   test_extended_set_del_slice  s    

z)ByteArrayTest.test_extended_set_del_slicec             C   s@   t td}||dd < | |t ttdttd  d S )Nr4   rb  )rd   r@   r   rA   )r   r   r   r   r   test_setslice_trap  s    z ByteArrayTest.test_setslice_trapc             C   s|   t d}|}|d7 }| |d | || | || |d7 }| |d y|d7 }W n tk
rl   Y nX | d d S )Ns   abcs   defs   abcdefs   xyzs	   abcdefxyzr   z'bytes += unicode didn't raise TypeError)rd   r   r   rS   r`  )r   r   rn   r   r   r   test_iconcat  s    zByteArrayTest.test_iconcatc             C   s<   t d}|}|d9 }| |d | || | || d S )Ns   abcr=   s	   abcabcabc)rd   r   r   )r   r   rn   r   r   r   test_irepeat  s    zByteArrayTest.test_irepeatc             C   s<   t d}|}|d9 }| |d | || | || d S )Nr   r,   sd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)rd   r   r   )r   r   rn   r   r   r   test_irepeat_1char  s    z ByteArrayTest.test_irepeat_1charc             C   sh   t  }| }| |d |g}xBtdD ]6}|d7 }| }| |t| ||kr*|| q*W d S )Nr   r,   r   )rd   	__alloc__ZassertGreaterEqualr@   assertGreaterr0   rJ   )r   r   allocr   r:   r   r   r   
test_alloc  s    zByteArrayTest.test_allocc                sf   t    fdd} |  t ttdd t d   }|t  d S )Nc           	   3   s   xzt ddD ]l} | V  t }|tt dt|d  t t| t |    }|t  qW d S )Nr(   r,   )r@   rA   r   r0   ro  rv  rw  )r:   r#   rx  )r   r   r   r   g"  s    z(ByteArrayTest.test_init_alloc.<locals>.gr(   r,   c   )rd   r   r   rA   r@   r0   rv  rw  )r   rz  rx  r   )r   r   r   test_init_alloc   s    	zByteArrayTest.test_init_allocc             C   s  d}t |}|| | |||  | |dd  | t d}|tt|d  |dd |d D  | ||d  | |dd  | t d}|ttt|d  | ||d  | |dd  | t d}|ttt|d  | ||d  | |dd  | t d}| t|jd	d
ddg | t|jd	d
ddg | t	|d	 t d}|t
tdg | |d d S )Ns   hellorW   r'      c             s   s   | ]}t |V  qd S )N)r   )r9   r   r   r   r   r;   :  s    z,ByteArrayTest.test_extend.<locals>.<genexpr>2   r   r   r(   r/   r4   r-   r#   r   )rd   rj  r   ru   r   rD   rA   r1   rP   r0   r   rv   )r   r   r#   r   r   r   test_extend1  s2    
zByteArrayTest.test_extendc                s$  t d  td |  d  td |  d | t fdd | t fdd | t fdd  td	  td
 |  d | t fdd  ttd |  d t ddddg}|d | |tdddg |d | |tddg d S )Ns   hellor  s   helos   heoc                  s     tdS )Nr  )rU  rv   r   )r   r   r   r&   S  r'   z+ByteArrayTest.test_remove.<locals>.<lambda>c                  s
     dS )Ni  )rU  r   )r   r   r   r&   T  r'   c                  s
     dS )Nr  )rU  r   )r   r   r   r&   U  r'   r  r  r  c                  s
     dS )Nr  )rU  r   )r   r   r   r&   Z  r'   r  r'   ~   r  r<      )	rd   rU  rv   r   r1   rP   rS   r   rL   )r   r   r   )r   r   test_removeM  s&    

zByteArrayTest.test_removec                s   t d |   td |  dtd |  dtd | t fdd | td	d  | t d
 d d S )Ns   worldr   r   r   r.   rZ  c                  s
     dS )Nr+   )rl  r   )r   r   r   r&   j  r'   z(ByteArrayTest.test_pop.<locals>.<lambda>c               S   s
   t   S )N)rd   rl  r   r   r   r   r&   k  r'      rO   )rd   r   rl  rv   r1   r2   )r   r   )r   r   test_pope  s    zByteArrayTest.test_popc             C   s   |  tdd  d S )Nc               S   s
   t   S )N)rd   sortr   r   r   r   r&   p  r'   z+ByteArrayTest.test_nosort.<locals>.<lambda>)r1   AttributeError)r   r   r   r   test_nosorto  s    zByteArrayTest.test_nosortc                s   t d  td |  d |  dd  t    td | t d | t fdd t    ttd |  d	 d S )
Ns   hellr  s   hellor,   r(  r(   c                  s
     dS )Nr   )rJ   r   )r   r   r   r&   z  r'   z+ByteArrayTest.test_append.<locals>.<lambda>   A)rd   rJ   rv   r   r0   r1   rS   r   )r   r   )r   r   test_appendr  s    zByteArrayTest.test_appendc                s   t d  dtd  dtd  dtd  dtd |  d | t fdd	 t    d
ttd |  d d S )Ns   mssssppr(   r:   rb   r.   i  s   mississippic                  s     ddS )Nr      1)insertr   )r   r   r   r&     r'   z+ByteArrayTest.test_insert.<locals>.<lambda>r   r(  r  )rd   r  rv   r   r1   rS   r   )r   r   )r   r   test_insert  s    zByteArrayTest.test_insertc             C   sR   t d}| ||ddd t dd tdD }t d}| ||| d S )Ns   abcs   cder   c             S   s   g | ]}|qS r   r   )r9   r:   r   r   r   r    s    z-ByteArrayTest.test_copied.<locals>.<listcomp>r4   r'   )rd   r\  r   r@   r  )r   r   tr   r   r   r   test_copied  s
    zByteArrayTest.test_copiedc             C   s   t dd\}}}| |d | |d | || |d7 }| |d t dd\}}}| |d | |d t dd\}}}| |d | |d | || |d7 }| |d t dd\}}}| |d | |d d S )Nr      yr'      !)rd   r   r   r\  r   )r   r#   r   r   r   r   r   0test_partition_bytearray_doesnt_share_nullstring  s$    z>ByteArrayTest.test_partition_bytearray_doesnt_share_nullstringc                s
  t td t } fdd}|d  d d  }| t|d |  | | t|d |  | | t|d |  | | t jd |  | | t j d  |  |  fdd	}| t| |  |  fd
d}| t| |  | d S )Nr+   c                s"   t | d d|  d  dd< d S )Nr(   r/   r-   )r@   )rW  )r   r   r   resize  s    z3ByteArrayTest.test_resize_forbidden.<locals>.resize   r   r   r(   c                  s
    d= d S )Nr(   r   r   )r   r   r   delitem  s    z4ByteArrayTest.test_resize_forbidden.<locals>.delitemc                  s   d ddd< d S )Nr'   r(   r-   r/   r   r   )r   r   r   delslice  s    z5ByteArrayTest.test_resize_forbidden.<locals>.delslice)rd   r@   r   r1   BufferErrorr   rl  rU  )r   vr  r   r  r  r   )r   r   test_resize_forbidden  s*    z#ByteArrayTest.test_resize_forbiddenc             C   s    ddl m} | t|t  d S )Nr   )getbuffer_with_null_view)r.  r  r1   r  rd   )r   r  r   r   r   test_obsolete_write_lock  s    z&ByteArrayTest.test_obsolete_write_lockc       	      C   s  t d}td}xttjd D ]z}t|}t||f|}t|\}}||d d < | t	|t	| | t|| t
| t||f|}t|\}}||d d < | t	|t	| | t||dd   xtdt|D ]}t
| qW t||f|}t|\}}||d d < | t	|t	| | t||t|d   | tt
| t||f|}t|\}}||d d < | t|g  q"W d S )Ns   abcs   qwertyr(   )rd   rA   r@   r   r   rD   r   r   r   r   r   r0   r1   StopIteration)	r   r   r   r   Zitorigr   r   r   r:   r   r   r   test_iterator_pickling2  s6    z%ByteArrayTest.test_iterator_pickling2c             C   s4   t d}t|}t| |  | t|g  d S )Ns   ab)rd   rD   r   rH   r   rA   )r   rI  r   r   r   r   test_iterator_length_hint  s
    z'ByteArrayTest.test_iterator_length_hintN),r   r   r   rd   r   r  rM  rO  rX  rY  r[  r%   r_  ra  rc  rh  rk  rm  rp  rq  rr  rs  rt  ru  ry  r|  r  r  r  r  r  r  r  r  r  r   r   Zcpython_onlyr  r  Z
list_testsZ
CommonTestZtest_exhausted_iteratorr  r   r   r   r   rL    sF   "	(		

 $
rL  c               @   s   e Zd Zedd Zedd Zdd Zejj	dd Z
d	d
 Zedd Zdd Zdd Zdd Zdd Zeejjddd ZdS )AssortedBytesTestc          	   C   s   xt tfD ]x}| |t d | |tdgd | |tddddgd | |dd	 | |d
d | |dd q
W d S )Nzbytearray(b'')r   zbytearray(b'\x00')r(   rN   rO   zbytearray(b'\x00\x01\xfe\xff')s   abczb'abc'   'zb"'"s   '"zb'\'"')rr   reprr   rd   )r   r   r   r   r   test_repr_str  s    zAssortedBytesTest.test_repr_strc          
   C   sv   xpdt dfD ]`}| t|t| | t|dt| | tdtt|j	  t|d W d Q R X qW d S )Ns   abcr   z\b%s\br|   )
rd   r   rC  rr   r   rS   r]  escaper   r   )r   r   r   r   r   test_format  s    zAssortedBytesTest.test_formatc             C   s  |  dtdkd |  dtdkd |  dtdkd |  dtdk d |  dtdkd |  dtdkd |  dtdkd |  dtdkd |  dtdkd |  dtdkd |  dtdk d |  dtdkd |  tddkd |  tddkd |  tddkd |  tddk d |  tddkd |  tddkd |  tddkd |  tddkd |  tddkd |  tddkd |  tddk d |  tddkd d S )Ns   abcTs   abF)r   rL   )r   r   r   r   test_compare_bytes_to_bytearray  s0    z1AssortedBytesTest.test_compare_bytes_to_bytearrayc             C   sH   |  tj | tjdtj |  tj | tjdtj d S )Nz
bytearray(zbytes()ZassertIsNotNonerd   __doc__rl   r   rL   )r   r   r   r   test_doc5  s    zAssortedBytesTest.test_docc             C   s,   t d}t|}t|}| |t| d S )Ns   Hello world
)rL   r   rd   r   )r   r   bufr   r   r   r   test_from_bytearray<  s    z%AssortedBytesTest.test_from_bytearrayc             C   sp   |  tdd |  tdd |  tdd |  ttdd |  ttdd |  ttdd	 d S )
Nr'   zb''r   zb'x'   zb'\x80'zbytearray(b'')zbytearray(b'x')zbytearray(b'\x80'))r   rr   rd   )r   r   r   r   test_to_strB  s    zAssortedBytesTest.test_to_strc             C   s^   ddddg}x"|D ]\}}|  |t|d qW x(tddD ]}| ttdt|  q<W d S )	N)s   Wonderful spamzWonderful spam)s   Wonderful spam toozWonderful spam too)s     u   ª  )s   \xaa\x00\000\200z\xaa\x00\000\200zlatin-1r<   r4   zb"%s")r   rd   r@   r1   SyntaxErrorevalchr)r   Ztestsr   r|   r   r   r   r   test_literalK  s    zAssortedBytesTest.test_literalc             C   s   |  dtdddg d S )Ns   a br   r   r   )r   r   r   )r   r   r   r   test_split_bytearrayX  s    z&AssortedBytesTest.test_split_bytearrayc             C   s   |  dtdddg d S )Ns   a br   r   r   )r   r   r   )r   r   r   r   test_rsplit_bytearray[  s    z'AssortedBytesTest.test_rsplit_bytearrayc             C   s   t  }| |dd| d S )Nr'   )rd   r\  r   )r   r   r   r   r   test_return_self^  s    z"AssortedBytesTest.test_return_selfz4BytesWarning is needed for this test: use -bb optionc          	   C   sT  dd }|  ddk W d Q R X |  ddk W d Q R X |  ddk W d Q R X |  ddk W d Q R X |  t ddk W d Q R X |  dt dk W d Q R X |  t ddk W d Q R X |  dt dk W d Q R X |  ddk W d Q R X |  ddk W d Q R X |  ddk W d Q R X |  ddk W d Q R X d S )Nc               S   s   t jdtfS )Nr   )r   r   r   r   r   r   r   r   bytes_warningf  s    z5AssortedBytesTest.test_compare.<locals>.bytes_warningr'   r   rV   r   )rd   )r   r  r   r   r   rp   c  s2    zAssortedBytesTest.test_compareN)r   r   r   r   r  r  r  r   r   Zrequires_docstringsr  r  r  r  r  r  r  unittestZ
skipUnlessr)   flagsr  rp   r   r   r   r   r    s   		
r  c               @   s   e Zd Zdd Zdd ZdS )BytearrayPEP3137Testc             C   s   t |S )N)rd   )r   r   r   r   r   marshal  s    zBytearrayPEP3137Test.marshalc             C   s   |  d}x<dD ]4}t||}|d}| || | |||d  qW x2dD ]*}t|}| || | |||d  qNW |  d}||g}| || | || d S )Ns   1234)zfillr  r  r   r=   z" returned self on a mutable object)zval.split()[0]zval.rsplit()[0]zval.partition(b".")[0]zval.rpartition(b".")[2]zval.splitlines()[0]zval.replace(b"", b"")z! returned val on a mutable objectr'   )r  getattrr   r\  r  r   )r   valZmethnamer  Znewvalexprsepr   r   r   test_returns_new_copy  s     




z*BytearrayPEP3137Test.test_returns_new_copyN)r   r   r   r  r  r   r   r   r   r    s   r  c                   s    e Zd Z fddZdZ  ZS )FixedStringTestc                s&   t |tr| |dS t |S )Nzutf-8)
isinstancerr   r   r   superfixtype)r   obj)r   r   r   r    s    
zFixedStringTest.fixtypeT)r   r   r   r  Zcontains_bytes__classcell__r   r   )r   r   r    s   r  c               @   s   e Zd ZeZdS )ByteArrayAsStringTestN)r   r   r   rd   r   r   r   r   r   r    s   r  c               @   s   e Zd ZeZdS )BytesAsStringTestN)r   r   r   rL   r   r   r   r   r   r    s   r  c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )SubclassTestc             C   s   |  t| j| j | |  | j d\}}| || | }}|  ||k |  ||k |  ||k  |  ||k |  ||k |  ||k | || | || ||  | || ||  | || ||  |  |d |d k d S )N)s   abcds   efghrW   )rl   
issubclassr   basetyper  r\  r   )r   r#   r   Z_aZ_br   r   r   
test_basic  s    zSubclassTest.test_basicc             C   s`   |  d}|  |g}| || | t|| jt| |dg}| t|| j d S )Ns   abcd)r   r  r   r\  r   r   )r   s1s2Zs3r   r   r   r     s    
zSubclassTest.test_joinc             C   s   |  d}d|_|  d|_xttjd D ]}tt||}| t	|t	| | 
|| | 
|j|j | 
|j|j | 
t|t| | 
t|jt|j q,W d S )Ns   abcdr+   s   efghr(   )r   r   yr@   r   r   r   r   assertNotEqualidr   r   )r   r#   r   r   r   r   r   test_pickle  s    
zSubclassTest.test_picklec             C   s   |  d}d|_|  d|_xtjtjfD ]x}||}| t|t| | || | |j|j | |j|j | t|t| | t|jt|j q*W d S )Ns   abcdr+   s   efgh)	r   r   r  r!   r"   r  r  r   r   )r   r#   r$   r   r   r   r   r%     s    
zSubclassTest.test_copyc                s    j d} |d  t| j  G  fddd j}|d} |d  t||  |jd G  fddd j}|d} |d  t||  |jd d S )Nr   s   +0c                   s   e Zd Z fddZdS )z%SubclassTest.test_fromhex.<locals>.B1c                s    j | |}d|_|S )Nbar)r  __new__foo)clsr   me)r   r   r   r    s    z-SubclassTest.test_fromhex.<locals>.B1.__new__N)r   r   r   r  r   )r   r   r   B1   s   r  r  c                   s   e Zd Z fddZdS )z%SubclassTest.test_fromhex.<locals>.B2c                s(    j tk	r j j| f|| d| _d S )Nr  )r  rL   r   r  )r  r	   kwargs)r   r   r   r     s    
z.SubclassTest.test_fromhex.<locals>.B2.__init__N)r   r   r   r   r   )r   r   r   B2  s   r  )r   r   r   r   r   r  r  )r   r   r  r  r   )r   r   r     s    

zSubclassTest.test_fromhexN)r   r   r   r  r   r  r%   r   r   r   r   r   r    s
   r  c               @   s   e Zd ZdS )ByteArraySubclassN)r   r   r   r   r   r   r   r    s   r  c               @   s   e Zd ZdS )r+  N)r   r   r   r   r   r   r   r+    s   r+  c               @   s   e Zd ZdS )r*  N)r   r   r   r   r   r   r   r*    s   r*  c               @   s   e Zd ZeZeZdd ZdS )ByteArraySubclassTestc             C   sN   G dd dt }|dd}|ddd}| |d |ddd}| |d d S )Nc               @   s   e Zd ZdddZdS )z:ByteArraySubclassTest.test_init_override.<locals>.subclassr(   c             _   s   t j| f|| d S )N)rd   r   )r  newargr	   r  r   r   r   r   &  s    zCByteArraySubclassTest.test_init_override.<locals>.subclass.__init__N)r(   )r   r   r   r   r   r   r   r   subclass%  s   r  rb   s   abcd)source)r  r  )rd   r   )r   r  r   r   r   r   test_init_override$  s    
z(ByteArraySubclassTest.test_init_overrideN)r   r   r   rd   r  r  r   r  r   r   r   r   r     s   r  c               @   s   e Zd ZeZeZdS )BytesSubclassTestN)r   r   r   rL   r  r+  r   r   r   r   r   r  /  s   r  __main__))r  rT   r   r]  r)   r!   r   r   rR  r  Ztest.supportr   Ztest.string_testsZtest.list_testsr   r   r  r  r   r   r   ZTestCaser  rL  r  r  Zstring_testsZBaseTestr  r  r  r  rd   r  rL   r+  r*  r  r  r   mainr   r   r   r   <module>   sX   
      , ?   s ]
