U
    >vfW                     @   s   d Z ddlZddlZddlmZ ddlmZ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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 zdd	l!m"Z# e $e# W n e%k
r   Y nX e
j&'e
j&(e)Z*G d
d dZ+ej,dd Z-G dd dZ.dd Z/ej01dddgej01dddgdd Z2dd Z3ej01deddfeddfgd d! Z4ej01d"dd#d$gej01dddgd%d& Z5d'd( Z6d)d* Z7d+d, Z8d-d. Z9ej01d/ej:ej;ej<ej=ej>ej?ej@ejAejBg	d0d1 ZCdS )2z0
Tests for the pandas.io.common functionalities
    N)partial)BytesIOStringIOUnsupportedOperation)Path)is_platform_windowsc                   @   s&   e Zd ZdZddddZdd ZdS )CustomFSPathz%For testing fspath on unknown objectsN)returnc                 C   s
   || _ d S Npath)selfr    r   ?/tmp/pip-unpacked-wheel-vdrwu74i/pandas/tests/io/test_common.py__init__    s    zCustomFSPath.__init__c                 C   s   | j S r
   r   r   r   r   r   
__fspath__#   s    zCustomFSPath.__fspath__)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   )localc                   @   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edd Zejdeeegdd Zdd Zdd Zed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	d*d+ Z%ejd,ej&j'dedfej&j(ded-fej&j)d!ed"fej&j*ded#fej&j+ded.fej&j,ded&fej&j-ded(fej&j.ded)fgd/d0 Z/ejdejdedfej0d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
d1d2 Z1ejd3ejdd4fej0dd4f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g
d=d> Z2ejd?d@i dfdAdBdCidCfdDi dfdEi dfdFi dfdGi dfdHi dfdIdJe3dKidfgdLdM Z4dNdO Z5dPS )Q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   filenameZ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   test_expand_user_normal_pathG   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_pathlibN   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_localpathT   s    z6TestCommonIOCapabilities.test_stringify_path_localpathc                 C   s"   t d}t|}|dkstd S )Nfoo/bar.csv)r   r   r&   r   )r   presultr   r   r   test_stringify_path_fspath[   s    
z3TestCommonIOCapabilities.test_stringify_path_fspathc              
   C   sT   t d}t 8}|jd| dd}|t|ks<tW 5 Q R X W 5 Q R X d S )Nfsspeczfile://wbmode)pytestimportorskiptmensure_cleanopenr   r&   r   )r   r2   r   Z
fsspec_objr   r   r   !test_stringify_file_and_path_like`   s    

z:TestCommonIOCapabilities.test_stringify_file_and_path_like	path_typec                 C   s2   |\}}|d| }t j|dd}||ks.td S )Nr.   Zinfer)compression)r   Zinfer_compressionr   )r   Zcompression_formatr<   	extensionexpectedr   r=   r   r   r    test_infer_compression_from_pathg   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 5 Q R X W 5 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_pathn   s
    z2TestCommonIOCapabilities.test_get_handle_with_pathc              
   C   sP   t  6}t|d}|j|ks$tW 5 Q R X |jr8tW 5 Q R X |jsLtd S )Nr)r   r   rG   rH   r   closed)r   Zinput_bufferrK   r   r   r   test_get_handle_with_bufferw   s
    z4TestCommonIOCapabilities.test_get_handle_with_bufferc              	   C   s   d}t jt|dddr}d}d}|j|}t||ks>tt||k rjt|j dks`t||7 }qt||7 }q"||dkstW 5 Q R X d S )	N&   a,b,c
1,2,3
©,®,®
Look,a snake,🐍rbFis_text       r   utf-8)r   rG   r   rH   readlenr   encode)r   datarK   r0   	chunksizechunkr   r   r   )test_bytesiowrapper_returns_correct_bytes   s    
zBTestCommonIOCapabilities.test_bytesiowrapper_returns_correct_bytespyarrowc              	   C   s   ddl m} d}tdddgddd	gd
ddgd}t|}tj|ddd,}||j	 }t
|| |jrttW 5 Q R X d S )Nr   )csvrP   1   ©ZLook2   ®za snake3u   🐍abcrQ   FrR   )r^   r_   pd	DataFramer   r   rG   read_csvrH   Z	to_pandasr8   assert_frame_equalrN   r   )r   r_   rZ   r?   srK   dfr   r   r   test_get_handle_pyarrow_compat   s    z7TestCommonIOCapabilities.test_get_handle_pyarrow_compatc              	   C   s   t jt| jdd}t j|dd}W 5 Q R X t t| j}t|| t jt| jdd>}t|}t||jdg  tt ||jdd   W 5 Q R X d S )N   )r[   T)Zignore_indexr   )	ri   rk   r   data1concatr8   rl   nextZiloc)r   readerr0   r?   itfirstr   r   r   test_iterator   s    z&TestCommonIOCapabilities.test_iteratorz#reader, module, error_class, fn_extr   r_   txtZxlrdZxlsxfeathertables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 5 Q R X d S )NrZ   does_not_exist.File (b')?.+does_not_exist\.'? does not exist:\[Errno 2\] No such file or directory: '.+does_not_exist\.'zExpected object or value7path_or_buf needs to be a string file path or file-like#\[Errno 2\] File .+does_not_exist\.$ does not exist: '.+does_not_exist\.<   \[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.?\[Errno 2\] File o directory non esistente: '.+does_not_exist\.+Failed to open local file.+does_not_exist\.(|)match)r6   r7   r   r   r'   HEREraises)r   rt   moduleerror_classfn_extr   msg1msg2msg3msg4msg5msg6msg7msg8r   r   r   test_read_non_existent   s"    


4z/TestCommonIOCapabilities.test_read_non_existentz#method, module, error_class, fn_exthtmlZparquetc              	   C   sl   t | tdddgdddgdddgd}tjtddd	| }t j|d
d ||| W 5 Q R X d S )Nrp            rU   re   rZ   Zmissing_folderr~   z@Cannot save file into a non-existent directory: .*missing_folderr   )	r6   r7   ri   rj   r   r   r'   r   r   )r   methodr   r   r   Zdummy_framer   r   r   r   #test_write_missing_parent_directory   s    
$z<TestCommonIOCapabilities.test_write_missing_parent_directoryc                 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 5 Q R X d S )N~r~   r   c                 S   s   t jd| S )Nr$   )r   r   r'   )xr   r   r   <lambda>
  rT   zJTestCommonIOCapabilities.test_read_expands_user_home_dir.<locals>.<lambda>r   r   r   r   z0Unexpected character found when decoding 'false'r   r   r   r   r   r   r   r   r   r   )r6   r7   r   r   r'   setattrr   r   )r   rt   r   r   r   Zmonkeypatchr   r   r   r   r   r   r   r   r   r   r   r   test_read_expands_user_home_dir   s$    


4z8TestCommonIOCapabilities.test_read_expands_user_home_dirzreader, module, path)iorZ   r_   ziris.csv)r   rZ   Zfixed_widthzfixed_width_format.txt)r   rZ   excelz
test1.xlsx)r   rZ   ry   zfeather-0_3_1.feather)r   rZ   Z
legacy_hdfzdatetimetz_object.h5)r   rZ   Zstatazstata10_115.dta)r   ZsasrZ   ztest1.sas7bdat)r   r|   rZ   ztsframe_v012.json)r   rZ   r}   zcategorical.0.25.0.picklec                 C   sR   t | || }t|}||}||}|drBt|| nt|| d S )Nz.pickle)r6   r7   r   endswithr8   Zassert_categorical_equalrl   )r   rt   r   r   datapathmypathr0   r?   r   r   r   test_read_fspath_all   s     

z-TestCommonIOCapabilities.test_read_fspath_allz"writer_name, writer_kwargs, moduleto_csvto_excelengineZopenpyxl
to_featherto_htmlto_jsonto_latex	to_pickleto_stataZ
time_stampz2019-01-01 00:00c                 C   s
  |dkrt d td}td}tdddgi}|}|}t | t|}	t||}
|
|f| |
|	f| t|dj}t|dT}|d	krtj	|f|}tj	|f|}t
|| n| }| }||kstW 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X d S )
N)r   Zjinja2stringfspathArp   r   rQ   r   )r6   r7   r8   r9   ri   rj   r   getattrr:   
read_excelrl   rW   r   )r   Zwriter_nameZwriter_kwargsr   p1p2rn   r   r   r   writerZf_strZf_pathr0   r?   r   r   r   test_write_fspath_allM  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 5 Q R X W 5 Q R X t	|| d S )	Nrz   r   rp   r   r   r   r%   )key)
r6   r7   ri   rj   r8   r9   r   Zto_hdfread_hdfrl   )	r   rn   r   r   r   r   r   r0   r?   r   r   r   test_write_fspath_hdf5t  s    


"z/TestCommonIOCapabilities.test_write_fspath_hdf5N)6r   r   r   rq   r!   r"   r)   tdZ
skip_if_nor-   r1   r;   r6   markparametrize
path_typesr@   strr   r   rL   rO   r]   ro   rw   ri   rk   FileNotFoundErrorread_fwfr   read_featherOSErrorr   
read_stataread_sas	read_jsonread_pickler   rj   r   r   r   r   Z
to_parquetr   r   r   r   Z
read_tabler   r   Zto_datetimer   r   r   r   r   r   r   5   s   	














r   c                 C   s   | ddddS )Nr   rZ   r_   ztest_mmap.csvr   )r   r   r   r   	mmap_file  s    r   c                   @   sX   e Z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	|d W 5 Q R X t
|}W 5 Q R X d}tjt|d t	|d W 5 Q R X d S )	NzI am not a filec                   S   s   dS )Nr   r   r   r   r   r     rT   z;TestMMapWrapper.test_constructor_bad_file.<locals>.<lambda>zThe parameter is incorrectz
[Errno 22]r   TzI/O operation on closed file)r   filenor   r   mmaperrorr6   r   r   Z_maybe_memory_mapr:   
ValueError)r   r   Znon_filemsgerrtargetr   r   r   test_constructor_bad_file  s    


z)TestMMapWrapper.test_constructor_bad_filec                 C   s   t |}| }tj|ddddh}|j}t|jjtjs@t|D ] }t	|}|
 |
 ksDtqDtjtdd t	| W 5 Q R X W 5 Q R X W 5 Q R X d S )NrM   T)rS   
memory_mapz^$r   )r:   	readlinesr   rG   rH   
isinstancebufferr   r   rs   stripr6   r   StopIteration)r   r   r   lineswrapperswrapper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 5 Q R X W 5 Q R X d S )NzUnknown enginer   Zpyt)r   )	r8   r9   makeDataFramer   r6   r   r   ri   rk   r   r   rn   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 5 Q R X dS )z\
        'encoding' shouldn't be passed to 'open' in binary mode.

        GH 35058
        zw+br4   r   Z	index_colN)r8   r9   r   r   rl   ri   rk   r   r   r   r   test_binary_mode  s    
z TestMMapWrapper.test_binary_modeencodingzutf-16zutf-32compression_bz2xzc              
   C   st   t  }t  Z}t t |j|||d W 5 Q R X d}tjt|d t	j
|||d W 5 Q R X W 5 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   z&UTF-\d+ stream does not start with BOMr   N)r8   r   r9   Zassert_produces_warningUnicodeWarningr   r6   r   UnicodeErrorri   rk   )r   r   r   rn   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   r6   r   r   r   r   r   r   r   r     s   r   c                   C   s   t dstt dstt dr*tt dr8tt drFtt drTtt drbtt drptt d	s~td S )
Nzgcs://pandas/somethingelse.comzgs://pandas/somethingelse.comzhttp://pandas/somethingelse.comzrandom:pandas/somethingelse.comz/local/pathzrelative/local/pathzthis is not fsspec://urlz({'url': 'gs://pandas/somethingelse.com'}z#RFC-3986+compliant.spec://something)r   Zis_fsspec_urlr   r   r   r   r   test_is_fsspec_url  s    r   r   rV   formatr_   r|   c              
   C   s   t  }t  t}tj|d| d}t|d| | W 5 Q R X tj|d| d(}|dkrltj|dd}n
t|}W 5 Q R X W 5 Q R X t 	|| d S )NrB   )r5   r   to_rM   r_   r   r   )
r8   r   r9   codecsr:   r   ri   rk   r   rl   )r   r   r?   r   rH   rn   r   r   r   test_codecs_encoding  s    
r   c                  C   s   t  } t  ~}t|d*}td|}| | W 5 Q R X W 5 Q R X t|d.}td|}tj	|dd}W 5 Q R X W 5 Q R X W 5 Q R X t 
| | d S )Nr3   rV   rQ   r   r   )r8   r   r9   r:   r   	getwriterr   	getreaderri   rk   rl   )r?   r   rH   encodedrn   r   r   r   test_codecs_get_writer_reader  s    
,r   zio_class,mode,msgtz*a bytes-like object is required, not 'str'rg   z%string argument expected, got 'bytes'c              
   C   sL   t  }|  4}tjt|d |j|d| d W 5 Q R X W 5 Q R X d S )Nr   rB   r4   )r8   r   r6   r   	TypeErrorr   )Zio_classr5   r   r?   r   r   r   r   test_explicit_encoding	  s    r   encoding_errorsstrictreplacec              
   C   s  d}d}|dkr<d| d |d  d | }t tjdd}n.d	|d  d
 | d | d }t tjdd}t }t|}|| | dkrtj	t
|d ||| d W 5 Q R X n>||| d}|j| d}	tj|	|	gi|	d gd}
t||
 W 5 Q R X d S )Nz'utf-8' codec can't decode byte   r_      ,   
r   r   r   s   {"s   ": {"s   ":"s   "}}index)Zorientr   r   )r   errors)r  )r   ri   rk   r   r8   r9   r   write_bytesr6   r   UnicodeDecodeErrordecoderj   rl   )r   r   r   Zbad_encodingcontentrt   r   filern   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 5 Q R X W 5 Q R X d S )Nzunknown error handler namer   rB   badr  )r8   r9   r6   r   LookupErrorr   rG   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 5 Q R X d S )Nz\[Errno 2\]r   Zdoesnt_exist)r6   r   r   ri   rk   errnoENOENTr   )r   r   r   r   test_errno_attributeD  s    
r  c               
   C   s@   t jtdd( t } tj| ddd W 5 Q R X W 5 Q R X d S )Nr   r   rQ   T)r   )r6   r   r   r   r   rG   )r   r   r   r   test_fail_mmapK  s    r  c                  C   sd   G dd d} t jtdd> t ,}t|d}|j|   W 5 Q R X W 5 Q R X W 5 Q R X d S )Nc                   @   s   e Zd Zdd ZdS )z&test_close_on_error.<locals>.TestErrorc                 S   s   t dd S )Ntest)r   r   r   r   r   closeT  s    z,test_close_on_error.<locals>.TestError.closeN)r   r   r   r  r   r   r   r   	TestErrorS  s   r  r  r   rQ   )r6   r   r   r   r   rG   Zcreated_handlesappend)r  r   rK   r   r   r   test_close_on_errorQ  s
    r  rt   c              	   C   s"   t  }t| | W 5 Q R X d S r
   )r   r}   dump)rt   r   r   r   r   test_pickle_reader]  s    r  )Dr   r   r  	functoolsr   r   r   r   r   r   r   pathlibr   r}   rC   r6   Zpandas.compatr   Zpandas.util._test_decoratorsutilZ_test_decoratorsr   Zpandasri   Zpandas._testingZ_testingr8   Zpandas.io.commoncommonr   r   r   r   Zpy.pathr   r+   r  ImportErrorr   r*   dirname__file__r   r   Zfixturer   r   r   r   r   r   r   r   r  r  r  r  r  rk   r   r   r   r   r   r   r   r   r  r   r   r   r   <module>   s|   
  V
O

!