B
    u9aÉ3  ã               @   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kZnej o¨d
ej k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.7/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 )Nz
first linez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   s@  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 Q R X t 	|¡ d S )	NF)Údeletez
first linezsecond liner   r   r   é   )ÚtempfileZ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  ¡  yt  d¡ W n4 tk
rJ } z|  dt|ƒ ¡ W d d }~X Y nX 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 1z 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_historym   s    $



z-TestHistoryManipulation.test_nonascii_historyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚunittestÚ
skipUnlessr   r
   r*   r0   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 )Nz-czimport readlinezxterm-256color)ZTERMó    )r	   r   )r   Z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_scriptr-   Ú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: 0
)r<   r=   r-   r>   )r   r?   r   r   r   Útest_auto_history_disabled˜   s    z'TestReadline.test_auto_history_disabledc          
   C   s  t  t jd ¡}|dkr&|  d|›¡ yt d¡ W n4 tk
rh } z|  dt|ƒ ¡ W d d }~X Y nX 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 |¡ d S )N)ÚCZPOSIXzthe LC_CTYPE locale is u   Ã«Ã¯z Locale cannot encode test data: 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_CTYPEr,   r
   r   r+   r-   Úlenr<   r>   r   r   )r   Úlocr/   ÚscriptÚinputr?   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   s  d}t ƒ ð}tj |d¡}t|dƒ}| d| ¡ W d Q R X tj |d¡}t|dƒ,}d dd„ t|d	 ƒD ƒ¡}| |¡ W d Q R X d
}ttjƒ}||d< ||d< t	|d|d t|dƒ}| 
¡ }	W d Q R X |  t|	ƒ|¡ |  |	d  ¡ d¡ W d Q R X d S )Né
   ÚinputrcÚwbs   set history-size %d
Úhistoryr8   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)rL   ÚenvÚrbéÿÿÿÿs
   last input)r   r(   ÚpathÚjoinÚopenÚwriteÚrangeÚdictÚenvironr<   Ú	readlinesr   rI   Ústrip)
r   Zhistory_sizeZtest_dirrO   ÚfZhistory_fileÚdatarK   rU   Úlinesr   r   r   Útest_history_sizeò   s&    


zTestReadline.test_history_sizeN)r1   r2   r3   r5   ZskipIfr
   r   r   r;   r=   r@   rA   rM   rd   r   r   r   r   r7      s   Vr7   ó   dummy inputc             C   s¤  t dƒ}tƒ }| ¡ \}}tjd| f}tj|||||d}t |¡ t	ƒ N}	|	 
|¡ dd„ }
|	 |
|¡ |	 tj|¡ |	 
t ¡ ¡}| |tjtjB ¡ t |d¡ xìxæ| ¡ D ]Ú\}}|tj@ r"yt |d¡}W n4 tk
r } z|jtkrø‚ d}W d d }~X Y nX |s|S | |¡ |tj@ r´y|t ||¡d … }W n6 tk
rz } z|jtkrf‚ d}W d d }~X Y nX |s´| |tj¡ q´W qªW W d Q R X d S )	NÚptyz-c)Ústdinr9   r:   rU   c             S   s&   y|   ¡  W n tk
r    Y nX d S )N)Ú	terminateÚProcessLookupError)Úprocr   r   r   rh   #  s    zrun_pty.<locals>.terminateFi   r8   )r   Ú	bytearrayÚopenptyÚsysÚ
executableÚ
subprocessÚPopenr(   r#   r   Úenter_contextÚcallbackÚ	selectorsZSelectSelectorÚregisterZ
EVENT_READZEVENT_WRITEÚset_blockingZselectÚreadÚOSErrorÚerrnor   Úextendr[   Zmodify)rK   rL   rU   rf   r?   ZmasterZslaveÚargsrj   Zcleanuprh   ZselÚ_ZeventsÚchunkr/   r   r   r   r<     sF    





r<   Ú__main__)re   N) r4   Ú
contextlibr   rx   r   rF   r(   rs   ro   rm   r"   r5   Ztest.supportr   r   r   r   r   Ztest.support.script_helperr	   r
   r   r   r   r   r6   ZTestCaser   r7   r<   r1   Úmainr   r   r   r   Ú<module>   s0   

Y 
2
