
    <e8                        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
          r
dej        v Znej         odej         v Zd Z ej         eed          d           G d dej                              Z G d dej                  ZddZe dk    r ej!                     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                  F   t           rt          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   _READLINE_RUNTIME_VERSIONr   is_editline     )/usr/lib/python3.11/test/test_readline.pysetUpModuler      s     7 8011 	XFx'AFFFGGGVx/QVVVWWW8899 	WUx/QUUVVV555666667 7r   clear_historyzXThe history update test cannot be run because the clear_history method is not available.c                   f    e Zd ZdZd Z ej         eed          d          d             Z	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                    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_lengthremove_history_item)selfs    r   testHistoryUpdatesz*TestHistoryManipulation.testHistoryUpdates1   s      \***]+++22155t<<<22155|DDD22155}EEE%%a99922155t<<<22155GGG22155}EEE<<>>BBB$$Q'''22155t<<<22155}EEE<<>>BBBBBr   append_history_filezappend_history not availablec                 $   t          j        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          j        |           	 t                              d|           t          j        |           n# t           $ r Y nw xY w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&   hfile	hfilenames      r   test_write_read_appendz.TestHistoryManipulation.test_write_read_appendH   s    +5999J		*** 	   \***]+++##I...   <<>>BBB""9---<<>>BBB22155|DDD22155}EEE 	$$Q	222   ""9---<<>>BBB22155|DDD22155}EEE22155}EEE 		)	!((I666 Ii     ! 	 	 	D	 	##I.....s   6K& &
K32K3c                 D   t                                            	 t                               d           n<# t          $ r/}|                     dt          |          z              Y d }~nd }~ww xY w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#   r1   r	   r0   r3   remover2   r   r!   r"   )r&   errs     r   test_nonascii_historyz-TestHistoryManipulation.test_nonascii_historyv   sg      	L  ----! 	L 	L 	LMM<vc{{JKKKKKKKK	L[)))%%a666##F+++	6***   ""6*** 	*   )))22155{CCC22155|DDDDDs   6 
A/ %A**A/N)__name__
__module____qualname____doc__r'   unittest
skipUnlessr   r   r7   r@   r   r   r   r   r   '   s         C C C. X+@AA79 9*/ */9 9*/XE E E E Er   r   c                       e Zd Z ej        ej        dk     oe d          d             ZdZ	d Z
d Zd Z ej        ej        dk     d	           ej        ed
          d                         ZdS )TestReadlinei  z%not supported in this library versionc                 ^    t          ddd          \  }}}|                     |d           d S )N-czimport readlinezxterm-256color)TERMr   )r
   r!   )r&   rcstdoutstderrs       r   	test_initzTestReadline.test_init   sF     .d4E3CE E EFF%%%%%r   zvimport readline
readline.set_auto_history({})
input()
print("History length:", readline.get_current_history_length())
c                     t          | j                            d                    }|                     d|           d S )NTs   History length: 1run_ptyauto_history_scriptr=   assertInr&   outputs     r   test_auto_history_enabledz&TestReadline.test_auto_history_enabled   s=    188>>?? 	*F33333r   c                     t          | j                            d                    }|                     d|           d S )NFs   History length: 0rQ   rU   s     r   test_auto_history_disabledz'TestReadline.test_auto_history_disabled   s=    188??@@ 	*F33333r   c                 L   t          j        t           j        d           }|dv r|                     d|           	 t                              d           n<# t          $ r/}|                     dt          |          z              Y d }~nd }~ww xY wd}d}|dt          d          z  z  }|d	z  }|d
z  }|dz  }t          ||          }| 
                    d|           | 
                    d|           t          j        dk    st          s| 
                    d|           t          sAt          t          d          r,| 
                    d|           | 
                    d|           d}| 
                    d|z   dz   |           | 
                    d|z   |           d S )N)CPOSIXzthe LC_CTYPE locale is u   ëïr9   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
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=   lenrR   rT   sysplatformr   r   )r&   locr?   scriptinputrV   expecteds          r   test_nonasciizTestReadline.test_nonascii   s   v55.   MM;C;;<<<	L  ,,,,! 	L 	L 	LMM<vc{{JKKKKKKKK	L2h 3y>>))''*F333@&III<8##;#MM.777  	Kwx1EFF 	KMM6???MMA6JJJ5j8+g5v>>> 	kH,f55555s   A 
B"%BBi   z3this readline version does not support history-sizez-editline history size configuration is brokenc           	         d}t                      5 }t          j                            |d          }t	          |d          5 }|                    d|z             d d d            n# 1 swxY w Y   t          j                            |d          }t	          |d          5 }d                    d t          |dz            D                       }|                    |           d d d            n# 1 swxY w Y   d	}t          t          j                  }||d
<   ||d<   t          |d|           t	          |d          5 }|
                                }	d d d            n# 1 swxY w Y   |                     t          |	          |           |                     |	d                                         d           d d d            d S # 1 swxY w Y   d S )N
   inputrcwbs   set history-size %d
historyr   c              3       K   | ]	}d |z  V  
dS )s   item %d
Nr   ).0is     r   	<genexpr>z1TestReadline.test_history_size.<locals>.<genexpr>  sC        B  B$% !-q 0  B  B  B  B  B  Br   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)
INPUTRCHISTORY_FILEs   last input)rj   envrbs
   last input)r   r3   pathjoinopenwriterangedictenvironrR   	readlinesr!   re   strip)
r&   history_sizetest_dirro   fhistory_filedatari   rx   liness
             r   test_history_sizezTestReadline.test_history_size  s   
 ZZ 	?8gll8Y77Ggt$$ A0<?@@@A A A A A A A A A A A A A A A 7<<)<<LlD)) Qxx  B  B).|a/?)@)@ B  B  B B B	              F rz""C$C	N".CF/s;;;;lD)) &Q& & & & & & & & & & & & & & &SZZ666U2Y__..>>>?	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?s   1GA'G'A+	+G.A+	/3G"AC3'G3C7	7G:C7	;A
GE&G&E*	*G-E*	.AGGGN)rA   rB   rC   rE   skipIfr   r   r   rO   rS   rW   rY   rl   r   r   r   r   rH   rH      s        X_X/&8L_<> >& &> >&4 4 44 4 4[6 [6 [6D X_X/%7JL LX_[DF F!? !?F FL L!? !? !?r   rH      dummy inputc                    t          d          }t                      }|                                \  }}t          j        d| f}t          j        |||||          }t          j        |           t                      5 }	|	
                    |           d }
|	                    |
|           |	                    t          j        |           |	
                    t          j                              }|                    |t          j        t          j        z             t          j        |d           	 |                                D ]\  }}|t          j        z  rh	 t          j        |d          }n*# t(          $ r}|j        t,          k    r d}Y d }~nd }~ww xY w|s|c cd d d            S |                    |           |t          j        z  rk	 |t          j        ||          d          }n*# t(          $ r}|j        t,          k    r d}Y d }~nd }~ww xY w|s |                    |t          j                   # 1 swxY w Y   d S )	NptyrJ   )stdinrM   rN   rx   c                 R    	 |                                   d S # t          $ r Y d S w xY w)N)	terminateProcessLookupError)procs    r   r   zrun_pty.<locals>.terminate<  s?         %   s    
&&FTi   r   )r   	bytearrayopenptyrf   
executable
subprocessPopenr3   r.   r   enter_contextcallback	selectorsSelectSelectorregister
EVENT_READEVENT_WRITEset_blockingselectreadOSErrorerrnor   extendr~   modify)ri   rj   rx   r   rV   masterslaveargsr   cleanupr   sel_eventschunkr?   s                   r   rR   rR   3  s   


C[[FkkmmOVUND&)DDeEsSSSDHUOOO	 (Ad###	 	 	 	D)))6*** ##I$<$>$>??VY1I4IIJJJ
&&&	A"zz|| A AFI00 
)$ " 8 8" $ $ $9++! #	$
 ! &%;(A (A (A (A (A (A (A (A< MM%(((I11 	A$ %bhvu&=&=&>&> ?" $ $ $9++! #	$
 ! A

69+?@@@-	A%(A (A (A (A (A (A (A (A (A (Asm   ?C I E65I6
F FIFI2$IG54I5
H?HIH(III__main__)r   N)"rD   
contextlibr   r   r   rb   r3   r   r   rf   r,   rE   test.supportr   test.support.import_helperr   test.support.os_helperr   r   r	   test.support.script_helperr
   r   r   r   r   r   rF   TestCaser   rH   rR   rA   mainr   r   r   <module>r      s    !                  				         



               4 4 4 4 4 4 ; ; ; ; ; ; ; ; ; ; 7 7 7 7 7 7 =$$
78011 G%)KKKK#E	X5E(EK	7 	7 	7 WWX77>? ?]E ]E ]E ]E ]Ex0 ]E ]E? ?]E@f? f? f? f? f?8$ f? f? f?R/A /A /A /Ad zHMOOOOO r   