B
    Y0dQI                 @   s  d Z ddlZddlZddlmZ ddlmZmZ ddlZddl	Z	ddl
mZ ddlZddlZddlmZ ddlm  mZ ddlZddlmZ ddlm  mZ G dd dZeeegZydd	lm Z! e"e! W n e#k
r   Y nX e	j$%e	j$&e'Z(ej)*d
G dd dZ+ej,dd Z-G dd dZ.dd Z/ej)0dddgej)0dddgdd Z1dd Z2ej)0deddfedd fgd!d" Z3ej)0d#dd$d%gej)0dddgd&d' Z4d(d) Z5d*d+ Z6dS ),z0
Tests for the pandas.io.common functionalities
    N)partial)BytesIOStringIO)Path)is_platform_windowsc               @   s    e Zd ZdZdd Zdd ZdS )CustomFSPathz%For testing fspath on unknown objectsc             C   s
   || _ d S )N)path)selfr    r
   M/var/www/html/venv/lib/python3.7/site-packages/pandas/tests/io/test_common.py__init__   s    zCustomFSPath.__init__c             C   s   | j S )N)r   )r	   r
   r
   r   
__fspath__!   s    zCustomFSPath.__fspath__N)__name__
__module____qualname____doc__r   r   r
   r
   r
   r   r      s   r   )localz*ignore:can't resolve package:ImportWarningc               @   s  e Zd ZdZdd Zdd Zdd Zedd	d
 Z	dd Z
dd Zejddddddddddg	ejdedd Zejdeeegdd Zdd Zd d! Zejd"ejd#ed$fejd#ed%fejd&ed'fejd(ed)fejd*ed+fejd#ed,fej d#ed-fej!d#e"d.fej#d#ed/fg	d0d1 Z$ejd"ejd#ed$fej%d#ed$fejd#ed%fejd&ed'fejd(ed)fejd*ed+fejd#ed,fej d#ed-fej!d#e"d.fej#d#ed/fg
d2d3 Z&ejd4ejd#d5fej%d#d5fejd#d6fejd&d7fejd(d8fejd*d9fejd#d:fej d#d;fej!d#d<fej#d#d=fg
ej'd>ej'd?d@dA Z(ejdBdCi d#fdDdEdFidFfdGi d(fdHi d#fdIi d#fdJi d#fdKi d#fdLdMe)dNid#fgdOdP Z*ej'd?ej+dQdR Z,dSS )TTestCommonIOCapabilitiesziindex,A,B,C,D
foo,2,3,4,5
bar,7,8,9,10
baz,12,13,14,15
qux,12,13,14,15
foo2,12,13,14,15
bar2,12,13,14,15
c             C   sB   d}t |}||ksttj|s*ttj||ks>td S )Nz
~/sometest)icom_expand_userAssertionErrorosr   isabs
expanduser)r	   filenameexpanded_namer
   r
   r   test_expand_user>   s
    
z)TestCommonIOCapabilities.test_expand_userc             C   s2   d}t |}||ksttj||ks.td S )Nz/somefolder/sometest)r   r   r   r   r   r   )r	   r   r   r
   r
   r   test_expand_user_normal_pathF   s    
z5TestCommonIOCapabilities.test_expand_user_normal_pathc             C   sB   t td}|dkstt td}|tjddks>td S )N.zfoo//barfoobar)r   stringify_pathr   r   r   r   join)r	   rel_pathZredundant_pathr
   r
   r   test_stringify_path_pathlibM   s    z4TestCommonIOCapabilities.test_stringify_path_pathlibzpy.pathc             C   s8   t jdd}t j|}t|}t||ks4td S )Nr   r    )r   r   r"   abspath	LocalPathr   r!   r   )r	   r   abs_pathZlpathr
   r
   r   test_stringify_path_localpathS   s    z6TestCommonIOCapabilities.test_stringify_path_localpathc             C   s"   t d}t|}|dkstd S )Nzfoo/bar.csv)r   r   r!   r   )r	   presultr
   r
   r   test_stringify_path_fspathZ   s    
z3TestCommonIOCapabilities.test_stringify_path_fspathc          
   C   sT   t d}t 8}|jd| dd}|t|ks<tW d Q R X W d Q R X d S )Nfsspeczfile://wb)mode)pytestimportorskiptmensure_cleanopenr   r!   r   )r	   r,   r   Z
fsspec_objr
   r
   r   !test_stringify_file_and_path_like_   s    

z:TestCommonIOCapabilities.test_stringify_file_and_path_likezextension,expected) N)z.gzgzip)z.bz2bz2)z.zipzip)z.xzxz)z.GZr6   )z.BZ2r7   )z.ZIPr8   )z.XZr9   	path_typec             C   s*   |d| }t j|dd}||ks&td S )Nzfoo/bar.csvZinfer)compression)r   Zinfer_compressionr   )r	   	extensionexpectedr:   r   r;   r
   r
   r    test_infer_compression_from_pathf   s    z9TestCommonIOCapabilities.test_infer_compression_from_pathc          
   C   sz   t jt d`}|dt|j d }t|d2}t|jj sJt	t
j||jjksbt	W d Q R X W d Q R X d S )N)dirz~/z	/sometestw)tempfileTemporaryDirectoryr   homenamer   
get_handlehandleis_absoluter   r   r   r   )r	   r:   tmpr   handlesr
   r
   r   test_get_handle_with_pathz   s
    z2TestCommonIOCapabilities.test_get_handle_with_pathc          	   C   sB   t  }t|d}|j|ks"tW d Q R X |jr6t|  d S )Nr)r   r   rE   rF   r   closedclose)r	   Zinput_bufferrI   r
   r
   r   test_get_handle_with_buffer   s
    
z4TestCommonIOCapabilities.test_get_handle_with_bufferc          	   C   s   t jt| jdd}t j|dd}W d Q R X t t| j}t|| t jt| jdd>}t|}t||jdg  tt ||jdd   W d Q R X d S )N   )	chunksizeT)Zignore_indexr   )	pdread_csvr   data1concatr1   assert_frame_equalnextZiloc)r	   readerr*   r=   itfirstr
   r
   r   test_iterator   s    z&TestCommonIOCapabilities.test_iteratorz#reader, module, error_class, fn_extr   csvtxtZxlrdZxlsxZpyarrowfeathertablesZh5ZdtaZsas7bdatjsonpicklec             C   s   t | tjtdd| }d| d}d| d}d}d}	d	| d
| d}
d| d}d| d}d| }t j|d| d| d| d|	 d|
 d| d| d| dd || W d Q R X d S )Ndatazdoes_not_exist.zFile (b')?.+does_not_exist\.z'? does not existz:\[Errno 2\] No such file or directory: '.+does_not_exist\.'zExpected object or valuez7path_or_buf needs to be a string file path or file-likez#\[Errno 2\] File .+does_not_exist\.z$ does not exist: '.+does_not_exist\.u<   \[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.z?\[Errno 2\] File o directory non esistente: '.+does_not_exist\.z+Failed to open local file.+does_not_exist\.(|))match)r/   r0   r   r   r"   HEREraises)r	   rW   moduleerror_classfn_extr   msg1msg2msg3msg4msg5msg6msg7msg8r
   r
   r   test_read_non_existent   s    

<z/TestCommonIOCapabilities.test_read_non_existentc             C   s   t | tjdd| }|tddd  d| d}d| d	}d
}	d}
d| d| d	}d| d	}d| d	}d| }t j|d| d| d|	 d|
 d| d| d| d| dd || W d Q R X d S )N~zdoes_not_exist.r   c             S   s   t jd| S )Nr   )r   r   r"   )xr
   r
   r   <lambda>       zJTestCommonIOCapabilities.test_read_expands_user_home_dir.<locals>.<lambda>zFile (b')?.+does_not_exist\.z'? does not existz:\[Errno 2\] No such file or directory: '.+does_not_exist\.rb   z0Unexpected character found when decoding 'false'z7path_or_buf needs to be a string file path or file-likez#\[Errno 2\] File .+does_not_exist\.z$ does not exist: '.+does_not_exist\.u<   \[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.z?\[Errno 2\] File o directory non esistente: '.+does_not_exist\.z+Failed to open local file.+does_not_exist\.rc   rd   re   )rf   )r/   r0   r   r   r"   setattrr   rh   )r	   rW   ri   rj   rk   Zmonkeypatchr   rl   rm   rn   ro   rp   rq   rr   rs   r
   r
   r   test_read_expands_user_home_dir   s    

<z8TestCommonIOCapabilities.test_read_expands_user_home_dirzreader, module, path)iora   r[   ziris.csv)r{   ra   Zfixed_widthzfixed_width_format.txt)r{   ra   excelz
test1.xlsx)r{   ra   r]   zfeather-0_3_1.feather)r{   ra   Z
legacy_hdfzdatetimetz_object.h5)r{   ra   statazstata10_115.dta)r{   sasra   ztest1.sas7bdat)r{   r_   ra   ztsframe_v012.json)r{   ra   r`   zcategorical.0.25.0.picklez8ignore:CategoricalBlock is deprecated:DeprecationWarningz;ignore:`np.object` is a deprecated alias:DeprecationWarningc             C   sR   t | || }t|}||}||}|drBt|| nt|| d S )Nz.pickle)r/   r0   r   endswithr1   Zassert_categorical_equalrU   )r	   rW   ri   r   datapathmypathr*   r=   r
   r
   r   test_read_fspath_all   s    &

z-TestCommonIOCapabilities.test_read_fspath_allz"writer_name, writer_kwargs, moduleto_csvZto_excelengineZxlwtZ
to_featherZto_htmlto_jsonZto_latexZ	to_pickleZto_stataZ
time_stampz2019-01-01 00:00c             C   s   t d}t d}tdddgi}|}|}t| t|}	t||}
|
|f| t|d}|	 }W d Q R X |
|	f| t|d}|	 }W d Q R X ||kst
W d Q R X W d Q R X d S )NstringfspathArO      rb)r1   r2   rQ   	DataFramer/   r0   r   getattrr3   readr   )r	   Zwriter_nameZwriter_kwargsri   p1p2dfr   r   r   writerfr=   r*   r
   r
   r   test_write_fspath_all  s    



z.TestCommonIOCapabilities.test_write_fspath_allc       	   
   C   s   t d tdddgi}td}td}|V}|F}t|}|j|dd |j|dd tj|dd}tj|dd}W d Q R X W d Q R X t	|| d S )	Nr^   r   rO   r   r   r   r    )key)
r/   r0   rQ   r   r1   r2   r   Zto_hdfread_hdfrU   )	r	   r   r   r   r   r   r   r*   r=   r
   r
   r   test_write_fspath_hdf5;  s    


"z/TestCommonIOCapabilities.test_write_fspath_hdf5N)-r   r   r   rS   r   r   r$   tdZ
skip_if_nor(   r+   r4   r/   markparametrize
path_typesr>   strr   r   rJ   rN   rZ   rQ   rR   FileNotFoundErrorZread_fwfZ
read_excelZread_featherIOErrorr   Z
read_stataZread_sas	read_json
ValueErrorZread_picklert   Z
read_tablerz   filterwarningsr   to_datetimer   Z&skip_array_manager_not_yet_implementedr   r
   r
   r
   r   r   3   s   		





 r   c             C   s   | ddddS )Nr{   ra   r[   ztest_mmap.csvr
   )r   r
   r
   r   	mmap_fileT  s    r   c               @   s`   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zej	
dddgej	
dddgdd ZdS )TestMMapWrapperc          	   C   s   t d}dd |_t r"d}t}n
d}tj}tj||d t	| W d Q R X t
|}|  d}tjt|d t	| W d Q R X d S )NzI am not a filec               S   s   dS )Nr
   r
   r
   r
   r   rw   \  rx   z;TestMMapWrapper.test_constructor_bad_file.<locals>.<lambda>zThe parameter is incorrectz
[Errno 22])rf   zI/O operation on closed file)r   filenor   OSErrormmaperrorr/   rh   r   _MMapWrapperr3   rM   r   )r	   r   Znon_filemsgerrtargetr
   r
   r   test_constructor_bad_fileZ  s    
z)TestMMapWrapper.test_constructor_bad_filec          	   C   sn   t |}t|}W d Q R X t|j}dd |D }|d x|D ]}t||sFtqFW t|drjtd S )Nc             S   s   g | ]}| d s|qS )__)
startswith).0attrr
   r
   r   
<listcomp>u  s    z1TestMMapWrapper.test_get_attr.<locals>.<listcomp>__next__r   )r3   r   r   r?   r   appendhasattrr   )r	   r   r   wrapperattrsr   r
   r
   r   test_get_attrp  s    



zTestMMapWrapper.test_get_attrc          	   C   sv   t |}t|}| }W d Q R X x(|D ] }t|}| | ks,tq,W tjt	dd t| W d Q R X d S )Nz^$)rf   )
r3   r   r   	readlinesrV   stripr   r/   rh   StopIteration)r	   r   r   r   lineslineZ	next_liner
   r
   r   	test_next}  s    


zTestMMapWrapper.test_nextc          
   C   sR   t  @}t  }|| tjtdd tj|dd W d Q R X W d Q R X d S )NzUnknown engine)rf   Zpyt)r   )	r1   r2   makeDataFramer   r/   rh   r   rQ   rR   )r	   r   r   r
   r
   r   test_unknown_engine  s
    

z#TestMMapWrapper.test_unknown_enginec          	   C   sD   t  2}t  }|j|dd t |tj|dd W dQ R X dS )z\
        'encoding' shouldn't be passed to 'open' in binary mode.

        GH 35058
        zw+b)r.   r   )	index_colN)r1   r2   r   r   rU   rQ   rR   )r	   r   r   r
   r
   r   test_binary_mode  s    
z TestMMapWrapper.test_binary_modeencodingzutf-16zutf-32compression_r7   r9   c          
   C   st   t  }t  Z}t t |j|||d W dQ R X d}tjt|d t	j
|||d W dQ R X W dQ R X dS )z
        bz2 and xz do not write the byte order mark (BOM) for utf-16/32.

        https://stackoverflow.com/questions/55171439

        GH 35681
        )r;   r   Nz&UTF-\d+ stream does not start with BOM)rf   )r1   r   r2   Zassert_produces_warningUnicodeWarningr   r/   rh   UnicodeErrorrQ   rR   )r	   r   r   r   r   r   r
   r
   r   test_warning_missing_utf_bom  s    

z,TestMMapWrapper.test_warning_missing_utf_bomN)r   r   r   r   r   r   r   r   r/   r   r   r   r
   r
   r
   r   r   Y  s   r   c               C   sX   t dstt dstt dr*tt dr8tt drFtt drTtd S )Nzgcs://pandas/somethingelse.comzgs://pandas/somethingelse.comzhttp://pandas/somethingelse.comzrandom:pandas/somethingelse.comz/local/pathzrelative/local/path)r   Zis_fsspec_urlr   r
   r
   r
   r   test_is_fsspec_url  s    r   r   zutf-8formatr[   r_   c          
   C   s   t  }t  t}tj|d| d}t|d| | W d Q R X tj|d| d(}|dkrltj|dd}n
t|}W d Q R X W d Q R X t 	|| d S )Nr@   )r.   r   Zto_rK   r[   r   )r   )
r1   r   r2   codecsr3   r   rQ   rR   r   rU   )r   r   r=   r   rF   r   r
   r
   r   test_codecs_encoding  s    
r   c              C   s   t  } t  ~}t|d*}td|}| | W d Q R X W d Q R X t|d.}td|}tj	|dd}W d Q R X W d Q R X W d Q R X t 
| | d S )Nr-   zutf-8r   r   )r   )r1   r   r2   r3   r   	getwriterr   	getreaderrQ   rR   rU   )r=   r   rF   encodedr   r
   r
   r   test_codecs_get_writer_reader  s    
,r   zio_class,mode,msgtz*a bytes-like object is required, not 'str'bz%string argument expected, got 'bytes'c          
   C   sL   t  }|  4}tjt|d |j|d| d W d Q R X W d Q R X d S )N)rf   r@   )r.   )r1   r   r/   rh   	TypeErrorr   )Zio_classr.   r   r=   bufferr
   r
   r   test_explicit_encoding  s    r   encoding_errorsstrictreplacec          
   C   s   d}d}|dkrd S d|d  d | d | d	 }tt jd
d}t }t|}|| | dkrtj	t
|d ||| d W d Q R X n>||| d}|j| d}	t j|	|	gi|	d gd}
t||
 W d Q R X d S )Nz'utf-8' codec can't decode byte   r[      
s   {"r   s   ": {"s   ":"s   "}}index)Zorientr   )rf   )r   )errors)r   )rQ   rR   r   r   r1   r2   r   write_bytesr/   rh   UnicodeDecodeErrordecoder   rU   )r   r   r   Zbad_encodingcontentrW   r   filer   decodedr=   r
   r
   r   test_encoding_errors  s$      	 

r   c           
   C   sB   t  0} tjtdd tj| ddd W d Q R X W d Q R X d S )Nz#Invalid value for `encoding_errors`)rf   r@   bad)r   )r1   r2   r/   rh   r   r   rE   )r   r
   r
   r   test_bad_encdoing_errors  s    
r   c           	   C   s8   t jtdd } td | jtjks*tW d Q R X d S )Nz\[Errno 2\])rf   Zdoesnt_exist)r/   rh   r   rQ   rR   errnoENOENTr   )r   r
   r
   r   test_errno_attribute  s    
r   )7r   r   r   	functoolsr   r{   r   r   r   r   pathlibr   rA   r/   Zpandas.compatr   Zpandas.util._test_decoratorsutilZ_test_decoratorsr   ZpandasrQ   Zpandas._testingZ_testingr1   Zpandas.io.commoncommonr   r   r   r   Zpy.pathr   r&   r   ImportErrorr   r%   dirname__file__rg   r   r   r   Zfixturer   r   r   r   r   r   r   r   r   r   r
   r
   r
   r   <module>   sN   
  #W
#