a
    äze.5  ã                   @   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mZmZmZmZ ddlmZ edƒZeedƒršd	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 )z:
Very minimal unittests for parts of the readline module.
é    )Ú	ExitStack)ÚEION)Úimport_moduleÚunlinkÚtemp_dirÚTESTFNÚverbose)Úassert_python_okÚreadlineÚ_READLINE_LIBRARY_VERSIONzEditLine wrapperÚlibeditc                   C   s^   t rZttdƒr2tdtjd›ƒ tdtjd›ƒ ttdƒrLtdtj›ƒ tdt› ƒ 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   ú(/usr/lib/python3.9/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   ÚtestHistoryUpdates/   s    


z*TestHistoryManipulation.testHistoryUpdatesÚappend_history_filezappend_history not availablec                 C   sV  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 |¡ |  t¡ t d|¡ W d   ƒ n1 s>0    Y  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!   ÚosZassertRaisesÚFileNotFoundError)r   ZhfileZ	hfilenamer   r   r   Útest_write_read_appendF   s2    





,z.TestHistoryManipulation.test_write_read_appendc              
   C   sÄ   t  ¡  zt  d¡ W n6 tyL } z|  dt|ƒ ¡ W Y d }~n
d }~0 0 t  d¡ t  dd¡ t  t¡ |  	t
jt¡ t  ¡  t  t¡ trœt  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 22Údummyr   )r
   r   r   ÚUnicodeEncodeErrorÚskipTestÚformatr   r)   r   r(   r+   Úremover*   r   r   r   )r   Úerrr   r   r   Útest_nonascii_historym   s    (



z-TestHistoryManipulation.test_nonascii_historyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    ÚunittestÚ
skipUnlessr   r
   r-   r5   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   s    t | j d¡ƒ}|  d|¡ d S )NTs   History length: 1©Úrun_ptyÚauto_history_scriptr2   ÚassertIn©r   Úoutputr   r   r   Útest_auto_history_enabled”   s    z&TestReadline.test_auto_history_enabledc                 C   s    t | j d¡ƒ}|  d|¡ d S )NFs   History length: 0rC   rG   r   r   r   Útest_auto_history_disabledš   s    z'TestReadline.test_auto_history_disabledc              
   C   s  t  t jd ¡}|dv r&|  d|›¡ zt d¡ W n6 tyj } z|  dt|ƒ ¡ W Y d }~n
d }~0 0 d}d}|dtdƒ 7 }|d	7 }|d
7 }|d7 }t	||ƒ}|  
d|¡ |  
d|¡ |  
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]'
s   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_CTYPEr1   r
   r   r0   r2   ÚlenrD   rF   r   r   )r   Úlocr4   ÚscriptÚinputrH   Zexpectedr   r   r   Útest_nonascii    s.    (4
zTestReadline.test_nonasciii   z3this readline version does not support history-sizez-editline history size configuration is brokenc           
   	   C   sZ  d}t ƒ 8}tj |d¡}t|dƒ}| d| ¡ W d   ƒ n1 sJ0    Y  tj |d¡}t|dƒ6}d dd„ t|d	 ƒD ƒ¡}| |¡ W d   ƒ n1 s¨0    Y  d
}ttjƒ}||d< ||d< t	|d|d t|dƒ}| 
¡ }	W d   ƒ n1 s0    Y  |  t|	ƒ|¡ |  |	d  ¡ d¡ W d   ƒ n1 sL0    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)rU   ÚenvÚrbéÿÿÿÿs
   last input)r   r+   ÚpathÚjoinÚopenÚwriteÚrangeÚdictÚenvironrD   Ú	readlinesr   rR   Ústrip)
r   Zhistory_sizeZtest_dirrX   ÚfZhistory_fileÚdatarT   r^   Úlinesr   r   r   Útest_history_sizeø   s(    
,

ÿ(

(zTestReadline.test_history_sizeN)r6   r7   r8   r:   ZskipIfr
   r   r   rB   rE   rI   rJ   rV   rm   r   r   r   r   r<      s   ÿ
Xÿÿr<   ó   dummy inputc                 C   sÈ  t dƒ}tƒ }| ¡ \}}tjd| f}tj|||||d}t |¡ t	ƒ f}	|	 
|¡ dd„ }
|	 |
|¡ |	 tj|¡ |	 
t ¡ ¡}| |tjtjB ¡ t |d¡ | ¡ D ]ð\}}|tj@ r2zt |d¡}W n6 ty
 } z|jtkrò‚ d}W Y d }~n
d }~0 0 |s(|  W  d   ƒ S | |¡ |tj@ r°z|t ||¡d … }W n8 tyŒ } z|jtkrt‚ d}W Y d }~n
d }~0 0 |s°| |tj¡ q°q¨W d   ƒ n1 sº0    Y  d S )	NÚptyr=   )Ústdinr@   rA   r^   c                 S   s$   z|   ¡  W n ty   Y n0 d S )N)Ú	terminateÚProcessLookupError)Úprocr   r   r   rq   )  s    zrun_pty.<locals>.terminateFi   r>   )r   Ú	bytearrayÚopenptyÚsysÚ
executableÚ
subprocessÚPopenr+   r&   r   Úenter_contextÚcallbackÚ	selectorsÚSelectSelectorÚregisterÚ
EVENT_READÚEVENT_WRITEÚset_blockingÚselectÚreadÚOSErrorÚerrnor   Úextendrd   Úmodify)rT   rU   r^   ro   rH   ZmasterZslaveÚargsrs   Úcleanuprq   ZselÚ_ÚeventsÚchunkr4   r   r   r   rD      sD    





rD   Ú__main__)rn   N) r9   Ú
contextlibr   r…   r   rO   r+   r|   rx   rv   r$   r:   Ztest.supportr   r   r   r   r   Ztest.support.script_helperr	   r
   r   r   r   r   r;   ZTestCaser   r<   rD   r6   Úmainr   r   r   r   Ú<module>   s2   
ÿY  
2
