o
    HXîh÷8  ã                   @   s  d Z ddlm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	Z	ddl
Z
ddlZddlmZ ddlmZ ddlmZmZmZ ddlmZ ed	ƒZeed
ƒrUdejv Znej o\dej v Zdd„ Ze eedƒd¡G dd„ dejƒƒZG dd„ dejƒZddd„Ze dkr‹e !¡  dS dS )z:
Very minimal unittests for parts of the readline module.
é    )Ú	ExitStack)ÚEION)Úverbose)Úimport_module)ÚunlinkÚtemp_dirÚTESTFN)Úassert_python_okÚreadlineÚ_READLINE_LIBRARY_VERSIONzEditLine wrapperÚlibeditc                   C   sb   t r/ttdƒrtdtjd›ƒ tdtjd›ƒ ttdƒr&tdtj›ƒ tdt› ƒ d S d S )NÚ_READLINE_VERSIONzreadline version: z#xzreadline runtime version: r   zreadline library version: zuse libedit emulation? )r   Úhasattrr
   Úprintr   Z_READLINE_RUNTIME_VERSIONr   Úis_editline© r   r   ú</opt/python-3.10.19/usr/lib/python3.10/test/test_readline.pyÚsetUpModule   s   

ør   Úclear_historyzXThe history update test cannot be run because the clear_history method is not available.c                   @   s:   e Zd ZdZdd„ Ze eedƒd¡dd„ ƒZ	dd	„ Z
d
S )ÚTestHistoryManipulationzÞ
    These tests were added to check that the libedit emulation on OSX and the
    "real" readline have the same interface for history manipulation. That's
    why the tests cover only a small subset of the interface.
    c                 C   sæ   t  ¡  t  d¡ t  d¡ |  t  d¡d ¡ |  t  d¡d¡ |  t  d¡d¡ t  dd¡ |  t  d¡d ¡ |  t  d¡d¡ |  t  d¡d¡ |  t  ¡ d¡ t  d¡ |  t  d¡d ¡ |  t  d¡d¡ |  t  ¡ d¡ d S )Nú
first lineúsecond liner   é   é   zreplaced line)r
   r   Úadd_historyÚassertEqualÚget_history_itemÚreplace_history_itemÚget_current_history_lengthZremove_history_item)Úselfr   r   r   ÚtestHistoryUpdates1   s   


z*TestHistoryManipulation.testHistoryUpdatesÚappend_history_filezappend_history not availablec                 C   sL  t jdd}| ¡  |j}|  t|¡ t ¡  t d¡ t d¡ t 	|¡ t ¡  |  
t ¡ d¡ t |¡ |  
t ¡ d¡ |  
t d¡d¡ |  
t d¡d¡ t d|¡ t ¡  t |¡ |  
t ¡ d¡ |  
t d¡d¡ |  
t d¡d¡ |  
t d¡d¡ t |¡ zt d|¡ W n	 ty™   Y nw t |¡ t 	|¡ d S )	NF)Údeleter   r   r   r   r   é   )ÚtempfileÚNamedTemporaryFileÚcloseÚnameÚ
addCleanupr   r
   r   r   Úwrite_history_filer   r   Úread_history_filer   r!   ÚosÚFileNotFoundError)r   ZhfileZ	hfilenamer   r   r   Útest_write_read_appendH   s:   





ÿ
z.TestHistoryManipulation.test_write_read_appendc              
   C   sÄ   t  ¡  zt  d¡ W n ty& } z|  dt|ƒ ¡ W Y d }~nd }~ww t  d¡ t  dd¡ t  t¡ |  	t
jt¡ t  ¡  t  t¡ trNt  d¡ |  t  d¡d¡ |  t  d¡d¡ d S )Nu	   entrÃ©e 1ú Locale cannot encode test data: u	   entrÃ©e 2r   u
   entrÃ©e 22Zdummyr   )r
   r   r   ÚUnicodeEncodeErrorÚskipTestÚformatr   r)   r   r(   r+   Úremover*   r   r   r   )r   Úerrr   r   r   Útest_nonascii_historyv   s"   €ÿ



z-TestHistoryManipulation.test_nonascii_historyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    ÚunittestÚ
skipUnlessr   r
   r-   r4   r   r   r   r   r   '   s    ÿ
,r   c                   @   sn   e Zd Ze ejdk oe d¡dd„ ƒZdZ	dd„ Z
dd	„ Zd
d„ Ze ejdk d¡e ed¡dd„ ƒƒZdS )ÚTestReadlinei  z%not supported in this library versionc                 C   s$   t dddd\}}}|  |d¡ d S )Nú-czimport readlinezxterm-256color)ZTERMó    )r	   r   )r   ÚrcÚstdoutÚstderrr   r   r   Ú	test_initŒ   s   ÿzTestReadline.test_initzvimport readline
readline.set_auto_history({})
input()
print("History length:", readline.get_current_history_length())
c                 C   ó    t | j d¡ƒ}|  d|¡ d S )NTs   History length: 1©Úrun_ptyÚauto_history_scriptr1   ÚassertIn©r   Úoutputr   r   r   Útest_auto_history_enabled   ó   z&TestReadline.test_auto_history_enabledc                 C   rB   )NFs   History length: 0rC   rG   r   r   r   Útest_auto_history_disabled£   rJ   z'TestReadline.test_auto_history_disabledc              
   C   s*  t  t jd ¡}|dv r|  d|›¡ zt d¡ W n ty5 } z|  dt|ƒ ¡ W Y d }~nd }~ww d}d}|dtdƒ 7 }|d	7 }|d
7 }|d7 }t	||ƒ}|  
d|¡ |  
d|¡ tjdksftsl|  
d|¡ tsttdƒr|  
d|¡ |  
d|¡ d}|  
d| d |¡ |  
d| |¡ d S )N)ÚCZPOSIXzthe LC_CTYPE locale is u   Ã«Ã¯r.   a*  import readline

is_editline = readline.__doc__ and "libedit" in readline.__doc__
inserted = "[\xEFnserted]"
macro = "|t\xEB[after]"
set_pre_input_hook = getattr(readline, "set_pre_input_hook", None)
if is_editline or not set_pre_input_hook:
    # The insert_line() call via pre_input_hook() does nothing with Editline,
    # so include the extra text that would have been inserted here
    macro = inserted + macro

if is_editline:
    readline.parse_and_bind(r'bind ^B ed-prev-char')
    readline.parse_and_bind(r'bind "\t" rl_complete')
    readline.parse_and_bind(r'bind -s ^A "{}"'.format(macro))
else:
    readline.parse_and_bind(r'Control-b: backward-char')
    readline.parse_and_bind(r'"\t": complete')
    readline.parse_and_bind(r'set disable-completion off')
    readline.parse_and_bind(r'set show-all-if-ambiguous off')
    readline.parse_and_bind(r'set show-all-if-unmodified off')
    readline.parse_and_bind(r'Control-a: "{}"'.format(macro))

def pre_input_hook():
    readline.insert_text(inserted)
    readline.redisplay()
if set_pre_input_hook:
    set_pre_input_hook(pre_input_hook)

def completer(text, state):
    if text == "t\xEB":
        if state == 0:
            print("text", ascii(text))
            print("line", ascii(readline.get_line_buffer()))
            print("indexes", readline.get_begidx(), readline.get_endidx())
            return "t\xEBnt"
        if state == 1:
            return "t\xEBxt"
    if text == "t\xEBx" and state == 0:
        return "t\xEBxt"
    return None
readline.set_completer(completer)

def display(substitution, matches, longest_match_length):
    print("substitution", ascii(substitution))
    print("matches", ascii(matches))
readline.set_completion_display_matches_hook(display)

print("result", ascii(input()))
print("history", ascii(readline.get_history_item(1)))
ó   ó   z[after]s   		s   x	ó   s   text 't\xeb'
s#   line '[\xefnserted]|t\xeb[after]'
Údarwins   indexes 11 13
Zset_pre_input_hooks   substitution 't\xeb'
s    matches ['t\xebnt', 't\xebxt']
s   '[\xefnserted]|t\xebxt[after]'s   result s   
s   history )ÚlocaleÚ	setlocaleÚLC_CTYPEr0   r
   r   r/   r1   ÚlenrD   rF   ÚsysÚplatformr   r   )r   Úlocr3   ÚscriptÚinputrH   Zexpectedr   r   r   Útest_nonascii©   s4   €ÿ4

zTestReadline.test_nonasciii   z3this readline version does not support history-sizez-editline history size configuration is brokenc           
   	   C   sV  d}t ƒ œ}tj |d¡}t|dƒ}| d| ¡ W d   ƒ n1 s$w   Y  tj |d¡}t|dƒ}d dd„ t|d	 ƒD ƒ¡}| |¡ W d   ƒ n1 sSw   Y  d
}ttjƒ}||d< ||d< t	|d|d t|dƒ}| 
¡ }	W d   ƒ n1 s‚w   Y  |  t|	ƒ|¡ |  |	d  ¡ d¡ W d   ƒ d S 1 s¤w   Y  d S )Né
   ÚinputrcÚwbs   set history-size %d
Úhistoryr=   c                 s   s    | ]}d | V  qdS )s   item %d
Nr   )Ú.0Úir   r   r   Ú	<genexpr>  s   € ÿz1TestReadline.test_history_size.<locals>.<genexpr>r   z¡
import os
import readline

history_file = os.environ["HISTORY_FILE"]
readline.read_history_file(history_file)
input()
readline.write_history_file(history_file)
ZINPUTRCZHISTORY_FILEs   last input)rY   ÚenvÚrbéÿÿÿÿs
   last input)r   r+   ÚpathÚjoinÚopenÚwriteÚrangeÚdictÚenvironrD   Ú	readlinesr   rT   Ústrip)
r   Zhistory_sizeZtest_dirr\   ÚfZhistory_fileÚdatarX   rb   Úlinesr   r   r   Útest_history_size  s0   ÿ

ÿü


ÿ"ázTestReadline.test_history_sizeN)r5   r6   r7   r9   ZskipIfr
   r   r   rA   rE   rI   rK   rZ   rq   r   r   r   r   r;   Š   s     ÿ
bÿÿr;   ó   dummy inputc                 C   s®  t dƒ}tƒ }| ¡ \}}tjd| f}tj|||||d}t |¡ t	ƒ ¨}	|	 
|¡ dd„ }
|	 |
|¡ |	 tj|¡ |	 
t ¡ ¡}| |tjtjB ¡ t |d¡ 	 | ¡ D ]s\}}|tj@ r–zt |d¡}W n tyƒ } z|jtkrw‚ d	}W Y d }~nd }~ww |s‘|  W  d   ƒ S | |¡ |tj@ rËz|t ||¡d … }W n tyÁ } z|jtkrµ‚ d	}W Y d }~nd }~ww |sË| |tj¡ qXqT1 sÐw   Y  d S )
NÚptyr<   )Ústdinr?   r@   rb   c                 S   s$   z|   ¡  W d S  ty   Y d S w )N)Ú	terminateÚProcessLookupError)Úprocr   r   r   ru   <  s
   þzrun_pty.<locals>.terminateFTi   r=   )r   Ú	bytearrayÚopenptyrU   Ú
executableÚ
subprocessÚPopenr+   r&   r   Úenter_contextÚcallbackÚ	selectorsÚSelectSelectorÚregisterÚ
EVENT_READÚEVENT_WRITEÚset_blockingÚselectÚreadÚOSErrorÚerrnor   Úextendrh   Úmodify)rX   rY   rb   rs   rH   ZmasterZslaveÚargsrw   Úcleanupru   ZselÚ_ÚeventsÚchunkr3   r   r   r   rD   3  sV   



€üã


€ü€êîrD   Ú__main__)rr   N)"r8   Ú
contextlibr   rˆ   r   rQ   r+   r   r{   rU   r$   r9   Ztest.supportr   Ztest.support.import_helperr   Ztest.support.os_helperr   r   r   Ztest.support.script_helperr	   r
   r   r   r   r   r:   ZTestCaser   r;   rD   r5   Úmainr   r   r   r   Ú<module>   s:    
ÿ` 
*2ÿ