
    z%i8                        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j0                  v Znej                   xr dej                   v Zd Z ej6                   eed      d       G d dej8                               Z G d dej8                        ZddZe dk(  r ejB                          yy)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                  "   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                y y )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     F/home/uftp/.pyenv/versions/3.12.0/lib/python3.12/test/test_readline.pysetUpModuler      s|     801&x'A'A"&EFG.x/Q/QRT.UVW889.x/Q/Q.TUV'}56 r   clear_historyzXThe history update test cannot be run because the clear_history method is not available.c                   \    e Zd ZdZd Z ej                   eed      d      d        Z	d Z
y)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         j                          t         j                  d       t         j                  d       | j                  t         j	                  d      d        | j                  t         j	                  d      d       | j                  t         j	                  d      d       t         j                  dd       | j                  t         j	                  d      d        | j                  t         j	                  d      d       | j                  t         j	                  d      d       | j                  t         j                         d       t         j                  d       | j                  t         j	                  d      d        | j                  t         j	                  d      d       | j                  t         j                         d       y )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   sR    \*]+2215t<2215|D2215}E%%a92215t<2215G2215}E<<>B$$Q'2215t<2215}E<<>Br   append_history_filezappend_history not availablec                    t        j                  d      }|j                          |j                  }| j	                  t
        |       t        j                          t        j                  d       t        j                  d       t        j                  |       t        j                          | j                  t        j                         d       t        j                  |       | j                  t        j                         d       | j                  t        j                  d      d       | j                  t        j                  d      d       t        j                  d|       t        j                          t        j                  |       | j                  t        j                         d       | j                  t        j                  d      d       | j                  t        j                  d      d       | j                  t        j                  d      d       t        j
                  |       	 t        j                  d|       t        j
                  |       t        j                  |       y # t         $ r Y !w xY w)	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JJ		* 	 \*]+##I. <<>B""9-<<>B2215|D2215}E 	$$Q	2 ""9-<<>B2215|D2215}E2215}E 			)	!((I6 IIi  	##I. ! 		s   ?J   	JJc                    t         j                          	 t         j                  d       t         j                  d       t         j                  dd       t         j                  t               | j                  t        j                  t               t         j                          t         j                  t               t        rt         j                  d       | j                  t         j                  d      d       | j                  t         j                  d      d       y # t        $ r(}| j	                  dt        |      z          Y d }~'d }~ww xY w)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   s     	L  - 	[)%%a6##F+		6* ""6*   )2215{C2215|D " 	LMM<vc{JKK	Ls   D& &	E/EEN)__name__
__module____qualname____doc__r'   unittest
skipUnlessr   r   r7   r@   r   r   r   r   r   '   sB    C. X+@A79*/9*/XEr   r   c                       e Zd Z ej                  ej                  dk  xr 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y)TestReadlinei  z%not supported in this library versionc                 L    t        ddd      \  }}}| j                  |d       y )N-czimport readlinezxterm-256color)TERMr   )r
   r!   )r&   rcstdoutstderrs       r   	test_initzTestReadline.test_init   s.     .d4E3CEFF%r   zvimport readline
readline.set_auto_history({})
input()
print("History length:", readline.get_current_history_length())
c                 p    t        | j                  j                  d            }| j                  d|       y )NTs   History length: 1run_ptyauto_history_scriptr=   assertInr&   outputs     r   test_auto_history_enabledz&TestReadline.test_auto_history_enabled   s.    1188>? 	*F3r   c                 p    t        | j                  j                  d            }| j                  d|       y )NFs   History length: 0rQ   rU   s     r   test_auto_history_disabledz'TestReadline.test_auto_history_disabled   s.    1188?@ 	*F3r   c                    t        j                  t         j                  d       }|dv r| j                  d|       	 t        j                  d       d}d}|dt        d      z  z  }|d	z  }|d
z  }|dz  }t        ||      }| j                  d|       | j                  d|       t        j                  dk(  st        s| j                  d|       t        s4t        t        d      r$| j                  d|       | j                  d|       d}| j                  d|z   dz   |       | j                  d|z   |       y # t        $ r(}| j                  dt        |      z          Y d }~d }~ww xY w)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.  MM3C7;<	L  ,2h 3y>))'*F3@&I<<8#;MM.7 wx1EFMM6?MMA6J5j8+g5v> 	kH,f5c " 	LMM<vc{JKK	Ls   D< <	E-E((E-i   z3this readline version does not support history-sizez-editline history size configuration is brokenc           	      "   d}t               5 }t        j                  j                  |d      }t	        |d      5 }|j                  d|z         d d d        t        j                  j                  |d      }t	        |d      5 }dj                  d t        |dz        D              }|j                  |       d d d        d	}t        t        j                        }||d
<   ||d<   t        |d|       t	        |d      5 }|j                         }	d d d        | j                  t        	      |       | j                  |	d   j                         d       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   ixY w# 1 sw Y   y xY w)N
   inputrcwbs   set history-size %d
historyr   c              3   &   K   | ]	  }d |z    yw)s   item %d
Nr   ).0is     r   	<genexpr>z1TestReadline.test_history_size.<locals>.<genexpr>  s       B$% !-q 0  Bs   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  sp   
 Z 	?8ggll8Y7Ggt$ A0<?@A 77<<)<LlD) Qxx  B).|a/?)@ B B	F rzz"C$C	N".CF/s;lD) &Q&SZ6U2Y__.>?	? 	?A A ,& &9	? 	?sT   -FE 4F6E-9AF E9AF E*	%F-E6	2F9F	>FF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[6D X__X//%7JLX__[DF!?FL!?r   rH   c                 ~   t        d      }t               }|j                         \  }}t        j                  d| f}t        j                  |||||      }t        j                  |       t               5 }	|	j                  |       d }
|	j                  |
|       |	j                  t        j                  |       |	j                  t        j                               }|j                  |t        j                  t        j                   z         t        j"                  |d       	 |j%                         D ]  \  }}|t        j                  z  r7	 t        j&                  |d      }|s|c cd d d        S |j/                  |       |t        j                   z  sd	 |t        j0                  ||      d  }|r|j3                  |t        j                          # t(        $ r }|j*                  t,        k7  r d}Y d }~d }~ww xY w# t(        $ r }|j*                  t,        k7  r d}Y d }~vd }~ww xY w# 1 sw Y   y xY w)NptyrJ   )stdinrM   rN   rx   c                 D    	 | j                          y # t        $ r Y y w xY w)N)	terminateProcessLookupError)procs    r   r   zrun_pty.<locals>.terminate<  s#     % s    	Fi   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kkmOVUNND&)DDeEsSDHHUO	 (Ad#	 	D)6* ##I$<$<$>?VY11I4I4IIJ
&"zz| AFI000$ " 8 !%;(A (A< MM%(I111$ %bhhvu&=&> ? !

69+?+?@+A 
 # $99+! #	$ # $99+! #	$E(A (Asm   5CH3G'H36$H3H5H38#H3	H$G?:H3?HH3	H0H+&H3+H00H33H<__main__)s   dummy input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 ; ; 7 $
801%)K)KKK##E	X5E5E(EK	7 WX7>?]Ex00 ]E?]E@f?8$$ f?R/Ad zHMMO r   