o
    Eh)]                     @   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Zddlm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! ej"#dZ$G d	d
 d
Z%e&e%egZ'zddl(m)Z* e'+e* W n	 e,y   Y nw e
j-.e
j-/e0Z1G dd dZ2ej3dd Z4G dd dZ5dd Z6ej"7dddgdd Z8dd Z9ej"7deddfeddfgd d! Z:ej"7d"g d#ej"7dddgd$d% Z;d&d' Z<d(d) Z=d*d+ Z>d,d- Z?ej"7d.ej@ejAejBejCejDejEejFejGejHg	d/d0 ZIdS )1z0
Tests for the pandas.io.common functionalities
    N)partial)BytesIOStringIOUnsupportedOperation)Path)is_platform_windows)pa_version_under19p0z=ignore:Passing a BlockManager to DataFrame:DeprecationWarningc                   @   s"   e Zd ZdZdddZdd ZdS )	CustomFSPathz%For testing fspath on unknown objectsreturnNc                 C   s
   || _ d S Npath)selfr    r   ;lib/python3.10/site-packages/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__)r
   N)__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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j2ej d$d8ejj3d9d:d;d<ej!dd=fej"dd>fej#dd?fej$dd@fg
dAdB Z4ejdCdDi dfdEdFdGidGfdHi d"fdIi dfdJi dfdKi dfdLi dfdMdNe5dOidfgdPdQ Z6dRdS Z7dTS )U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J tj|sJ tj||ksJ d S )Nz
~/sometest)icom_expand_userosr   isabs
expanduserr   filenameZexpanded_namer   r   r   test_expand_userE   s
   
z)TestCommonIOCapabilities.test_expand_userc                 C   s2   d}t |}||ksJ tj||ksJ d S )Nz/somefolder/sometest)r   r   r   r   r   r   r   r   r   test_expand_user_normal_pathM   s   
z5TestCommonIOCapabilities.test_expand_user_normal_pathc                 C   sB   t td}|dksJ t td}|tjddksJ d S )N.zfoo//barfoobar)r   stringify_pathr   r   r   join)r   Zrel_pathZredundant_pathr   r   r   test_stringify_path_pathlibT   s   z4TestCommonIOCapabilities.test_stringify_path_pathlibzpy.pathc                 C   s8   t jdd}t j|}t|}t||ksJ d S )Nr$   r%   )r   r   r'   abspath	LocalPathr   r&   )r   r   Zabs_pathZlpathr   r   r   test_stringify_path_localpathZ   s   z6TestCommonIOCapabilities.test_stringify_path_localpathc                 C   s"   t d}t|}|dksJ d S )Nfoo/bar.csv)r	   r   r&   )r   presultr   r   r   test_stringify_path_fspatha   s   
z3TestCommonIOCapabilities.test_stringify_path_fspathc              	   C   s   t d}t 4}|jd| dd}|t|ksJ W d    n1 s(w   Y  W d    d S W d    d S 1 s@w   Y  d S )Nfsspeczfile://wbmode)pytestimportorskiptmensure_cleanopenr   r&   )r   r0   r   Z
fsspec_objr   r   r   !test_stringify_file_and_path_likef   s   

"z:TestCommonIOCapabilities.test_stringify_file_and_path_like	path_typec                 C   s2   |\}}|d| }t j|dd}||ksJ d S )Nr,   Zinfer)compression)r   Zinfer_compression)r   Zcompression_formatr:   	extensionexpectedr   r;   r   r   r    test_infer_compression_from_pathm   s   z9TestCommonIOCapabilities.test_infer_compression_from_pathc              	   C   s   t jt dH}|dt|j d }t|d}t|jj s%J t	j
||jjks1J W d    n1 s;w   Y  W d    d S W d    d S 1 sSw   Y  d S )N)dirz~/z	/sometestw)tempfileZTemporaryDirectoryr   homenamer   
get_handlehandleZis_absoluter   r   r   )r   r:   tmpr    handlesr   r   r   test_get_handle_with_patht   s   "z2TestCommonIOCapabilities.test_get_handle_with_pathc              	   C   sx   t  *}t|d}|j|ksJ W d    n1 sw   Y  |jr&J W d    n1 s0w   Y  |js:J d S )Nr)r   r   rD   rE   closed)r   Zinput_bufferrG   r   r   r   test_get_handle_with_buffer}   s   z4TestCommonIOCapabilities.test_get_handle_with_bufferc                 C   s   d}t jt|ddd@}d}d}	 |j|}t||ks J t||k r6t|j dks1J ||7 }n||7 }q||d	ksDJ W d    d S 1 sOw   Y  d S )
N&   a,b,c
1,2,3
©,®,®
Look,a snake,🐍rbFis_text       Tr   utf-8)r   rD   r   rE   readlenencode)r   datarG   r.   	chunksizechunkr   r   r   )test_bytesiowrapper_returns_correct_bytes   s   "zBTestCommonIOCapabilities.test_bytesiowrapper_returns_correct_bytesc                 C   s   t d}d}tg dg dg dd}t|}tj|ddd	#}||j	 }t
r2|d
}t|| |jr=J W d    d S 1 sHw   Y  d S )Nzpyarrow.csvrL   )1   ©ZLook)2   ®za snake)3r]   u   🐍abcrM   FrN   object)r4   r5   pd	DataFramer   r   rD   read_csvrE   Z	to_pandasr   Zastyper6   assert_frame_equalrJ   )r   Zpa_csvrV   r=   srG   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 d    n1 sw   Y  t t| j}t|| t jt| jdd%}t|}t||jdg  tt ||jdd   W d    d S 1 saw   Y  d S )N   )rW   T)Zignore_indexr   )	rd   rf   r   data1concatr6   rg   nextZiloc)r   readerr.   r=   itfirstr   r   r   test_iterator   s   "z&TestCommonIOCapabilities.test_iteratorz#reader, module, error_class, fn_extr   csvZ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    d S 1 siw   Y  d S )NrV   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)r4   r5   r   r   r'   HEREraises)r   ro   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_extZhtmlZparquetc                 C   s|   t | tg dg dg dd}tjtddd| }t j|dd	 ||| W d    d S 1 s7w   Y  d S )
N)rk         )r   r      )r   r   rQ   r_   rV   Zmissing_folderrx   z@Cannot save file into a non-existent directory: .*missing_folderr   )	r4   r5   rd   re   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 d    d S 1 sqw   Y  d S )N~rx   r   c                 S   s   t jd| S )Nr$   )r   r   r'   )xr   r   r   <lambda>  s    zJTestCommonIOCapabilities.test_read_expands_user_home_dir.<locals>.<lambda>ry   rz   r{   r|   z0Unexpected character found when decoding 'false'r}   r~   r   r   r   r   r   r   r   r   )r4   r5   r   r   r'   setattrr   r   )r   ro   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)iorV   rs   ziris.csv)r   rV   Zfixed_widthzfixed_width_format.txt)r   rV   Zexcelz
test1.xlsx)r   rV   rt   zfeather-0_3_1.feather)r   rV   Z
legacy_hdfzdatetimetz_object.h5zTODO(infer_string)F)reasonstrict)Zmarks)r   rV   Zstatazstata10_115.dta)r   ZsasrV   ztest1.sas7bdat)r   rv   rV   ztsframe_v012.json)r   rV   rw   zcategorical.0.25.0.picklec                 C   sT   t | || }t|}||}||}|dr"t|| d S t|| d S )Nz.pickle)r4   r5   r	   endswithr6   Zassert_categorical_equalrg   )r   ro   r   r   datapathmypathr.   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v r	t d td}td}tdddgi}|}|{}t | t|}	t||}
|
|fi | |
|	fi | t|dH}t|d3}|d	kritj	|fi |}tj	|fi |}t
|| n| }| }||kswJ W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )
N)r   Zjinja2stringfspathArk   r   rM   r   )r4   r5   r6   r7   rd   re   r	   getattrr8   
read_excelrg   rS   )r   Zwriter_nameZwriter_kwargsr   p1p2ri   r   r   r   writerZf_strZf_pathr.   r=   r   r   r   test_write_fspath_allV  s2   




 Pz.TestCommonIOCapabilities.test_write_fspath_allc           	   	   C   s   t d tdddgi}td}td}|:}|(}t|}|j|dd |j|dd tj|dd}tj|dd}W d    n1 sHw   Y  W d    n1 sWw   Y  t	|| d S )	Nru   r   rk   r   r   r   r%   )key)
r4   r5   rd   re   r6   r7   r	   Zto_hdfread_hdfrg   )	r   ri   r   r   r   r   r   r.   r=   r   r   r   test_write_fspath_hdf5}  s   


 z/TestCommonIOCapabilities.test_write_fspath_hdf5N)8r   r   r   rl   r!   r"   r(   tdZ
skip_if_nor+   r/   r9   r4   markparametrize
path_typesr>   strr	   r   rH   rK   rY   rj   rr   rd   rf   FileNotFoundErrorread_fwfr   read_featherOSErrorr   
read_stataread_sas	read_jsonread_pickler   re   r   r   r   r   Z
to_parquetr   r   r   r   Z
read_tabler   ZparamZxfailr   Zto_datetimer   r   r   r   r   r   r   ;   s    	












!
r   c                 C   s   | ddddS )Nr   rV   rs   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 d    n1 s.w   Y  t
|dd	}W d    n1 sDw   Y  d
}tjt|d t	|d W d    d S 1 sdw   Y  d S )NzI am not a filec                   S   s   dS )Nr   r   r   r   r   r     s    z;TestMMapWrapper.test_constructor_bad_file.<locals>.<lambda>zThe parameter is incorrectz
[Errno 22]r   TrR   encodingzI/O operation on closed file)r   filenor   r   mmaperrorr4   r   r   Z_maybe_memory_mapr8   
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 |ddi}| }tj|ddddC}|j}t|jjtjs"J |D ]}t|}|	 |	 ks4J q$t
jtdd t| W d    n1 sKw   Y  W d    n1 sZw   Y  W d    d S W d    d S 1 srw   Y  d S )NrR   r   rI   T)rO   
memory_mapz^$r   )r8   	readlinesr   rD   rE   
isinstancebufferr   rn   stripr4   r   StopIteration)r   r   r   linesZwrapperswrapperlineZ	next_liner   r   r   	test_next  s&   
"zTestMMapWrapper.test_nextc              	   C   s   t  S}tjdtdd ttdtdd t	dD d}|
| tjtd	d
 tj|dd W d    n1 sBw   Y  W d    d S W d    d S 1 sZw   Y  d S )N皙?x      r   ABCDc                 S      g | ]}d | qS zi-r   .0ir   r   r   
<listcomp>      z7TestMMapWrapper.test_unknown_engine.<locals>.<listcomp>r   columnsindexzUnknown enginer   Zpyt)r   )r6   r7   rd   re   nparangereshapeIndexlistranger   r4   r   r   rf   r   r   ri   r   r   r   test_unknown_engine  s   

"z#TestMMapWrapper.test_unknown_enginec              	   C   s   t  :}tjdtdd ttdtdd t	dD d}|j
|d	d
 t |tj|dd W d   dS 1 sAw   Y  dS )z\
        'encoding' shouldn't be passed to 'open' in binary mode.

        GH 35058
        r   r   r   r   c                 S   r   r   r   r   r   r   r   r     r   z4TestMMapWrapper.test_binary_mode.<locals>.<listcomp>r   r   zw+br2   r   Z	index_colN)r6   r7   rd   re   r   r   r   r   r   r   r   rg   rf   r   r   r   r   test_binary_mode  s   
"z TestMMapWrapper.test_binary_moder   zutf-16zutf-32compression_bz2xzc              	   C   s   t jdtdd t tdt dd tdD d}t	 O}t
t |j|||d	 W d
   n1 s<w   Y  d}tjt|d t j|||d	 W d
   n1 s]w   Y  W d
   d
S W d
   d
S 1 suw   Y  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   r   r   c                 S   r   r   r   r   r   r   r   r     r   z@TestMMapWrapper.test_warning_missing_utf_bom.<locals>.<listcomp>r   r   )r;   r   NzHUTF-\d+ stream does not start with BOM|'utf-\d+' codec can't decode byter   )rd   re   r   r   r   r   r   r   r6   r7   Zassert_produces_warningUnicodeWarningr   r4   r   UnicodeErrorrf   )r   r   r   ri   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   r4   r   r   r   r   r   r   r   r     s    r   c                   C   s   t dsJ t dsJ t drJ t drJ t dr#J t dr*J t dr1J t dr8J t d	s?J 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   test_is_fsspec_url  s   r   formatrs   rv   c              	   C   s  t jdtdd t tdt dd tdD d}t	 P}t
|d	d
d}t|d|  | W d    n1 s@w   Y  t
|d
d}| dkrXt j|dd}nt |}W d    n1 sgw   Y  W d    n1 svw   Y  t|| d S )Nr   r   r   r   c                 S   r   r   r   r   r   r   r   r   
  r   z(test_codecs_encoding.<locals>.<listcomp>r   r   r@   rR   )r3   r   Zto_r   rs   r   r   )rd   re   r   r   r   r   r   r   r6   r7   r8   r   rf   r   rg   )r   r=   r   rE   ri   r   r   r   test_codecs_encoding  s$   

r  c               
   C   s>  t jdtdd t tdt dd tdD d} t	 l}t
|d	$}td
|}| | W d    n1 sAw   Y  W d    n1 sPw   Y  t
|d&}td
|}t j|dd}W d    n1 stw   Y  W d    n1 sw   Y  W d    n1 sw   Y  t| | d S )Nr   r   r   r   c                 S   r   r   r   r   r   r   r   r     r   z1test_codecs_get_writer_reader.<locals>.<listcomp>r   r   r1   rR   rM   r   r   )rd   re   r   r   r   r   r   r   r6   r7   r8   codecs	getwriterr   	getreaderrf   rg   )r=   r   rE   Zencodedri   r   r   r   test_codecs_get_writer_reader  s*   
r  zio_class,mode,msgtz*a bytes-like object is required, not 'str'ra   z%string argument expected, got 'bytes'c              	   C   s   t jdtdd t tdt dd tdD d}|  2}tj	t
|d	 |j|d
| d W d    n1 s?w   Y  W d    d S W d    d S 1 sWw   Y  d S )Nr   r   r   r   c                 S   r   r   r   r   r   r   r   r   6  r   z*test_explicit_encoding.<locals>.<listcomp>r   r   r   r@   r2   )rd   re   r   r   r   r   r   r   r4   r   	TypeErrorr   )Zio_classr3   r   r=   r   r   r   r   test_explicit_encoding(  s   "r  encoding_errors)Nr   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retj	t
|d ||| d W d    n1 s_w   Y  n'||| d}|j| d}	tj|	|	gi|	d gd}
t||
 W d    d S W d    d S 1 sw   Y  d S )Nz'utf-8' codec can't decode byte   rs      ,   
r   r   r   s   {"s   ": {"s   ":"s   "}}r   )Zorientr
  r   )r	  errors)r   )r   rd   rf   r   r6   r7   r   Zwrite_bytesr4   r   UnicodeDecodeErrordecodere   rg   )r	  r   r   Zbad_encodingZcontentro   r   fileri   Zdecodedr=   r   r   r   test_encoding_errors=  sF   	

" r  c               	   C   s|   t  0} tjtdd tj| ddd W d    n1 sw   Y  W d    d S W d    d S 1 s7w   Y  d S )Nzunknown error handler namer   r@   Zbadr  )r6   r7   r4   r   LookupErrorr   rD   r   r   r   r   test_bad_encdoing_errors`  s   
"r  c                  C   sN   t jtdd} td | jtjksJ W d    d S 1 s w   Y  d S )Nz\[Errno 2\]r   Zdoesnt_exist)r4   r   r   rd   rf   errnoENOENT)r   r   r   r   test_errno_attributeg  s   
"r  c               	   C   sz   t jtdd, t } tj| ddd W d    n1 sw   Y  W d    d S W d    d S 1 s6w   Y  d S )Nr   r   rM   T)r   )r4   r   r   r   r   rD   )r   r   r   r   test_fail_mmapn  s   "r  c               
   C   s   G dd d} t jtddA t %}t|d}|j|   W d    n1 s+w   Y  W d    n1 s:w   Y  W d    d S W d    d S 1 sRw   Y  d S )Nc                   @   s   e Zd Zdd ZdS )z&test_close_on_error.<locals>.TestErrorc                 S   s   t d)Ntest)r   r   r   r   r   closew  s   z,test_close_on_error.<locals>.TestError.closeN)r   r   r   r  r   r   r   r   	TestErrorv  s    r  r  r   rM   )r4   r   r   r   r   rD   Zcreated_handlesappend)r  r   rG   r   r   r   test_close_on_errort  s   "r  ro   c                 C   s8   t  }t| | W d    d S 1 sw   Y  d S r   )r   rw   dump)ro   r   r   r   r   test_pickle_reader  s   "r   )Jr   r  r  	functoolsr   r   r   r   r   r   r   Zpathlibr   rw   rA   Znumpyr   r4   Zpandas.compatr   Zpandas.compat.pyarrowr   Zpandas.util._test_decoratorsutilZ_test_decoratorsr   Zpandasrd   Zpandas._testingZ_testingr6   Zpandas.io.commoncommonr   r   filterwarningsZ
pytestmarkr	   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  rf   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s    
  Y
^

!