
    daf	3                        d 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 ddlmZ  e
d      Z eed	      rd
ej*                  v Znej                   xr dej                   v Zd Z ej0                   eed      d       G d dej2                               Z G d dej2                        Zedk(  r ej:                          yy)z:
Very minimal unittests for parts of the readline module.
    N)verbose)import_module)unlinktemp_dirTESTFN)run_pty)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     -/root/Python-3.12.4/Lib/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.testHistoryUpdates/   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_appendF   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"   r0   r   r/   r2   remover1   r   r    r!   )r%   errs     r   test_nonascii_historyz-TestHistoryManipulation.test_nonascii_historyt   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
   r6   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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 )Nz-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r   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: 0rO   rR   s     r   test_auto_history_disabledz'TestReadline.test_auto_history_disabled   s.    1188?@ 	*F3r   c                 l    t        j                  d      }t        |d      }| j                  d|       y )Na  
            import readline
            def complete(text, state):
                if state == 0 and text == "$":
                    return "$complete"
                return None
            if "libedit" in getattr(readline, "__doc__", ""):
                readline.parse_and_bind(r'bind "\t" rl_complete')
            else:
                readline.parse_and_bind(r'"\t": complete')
            readline.set_completer_delims(" \t\n")
            readline.set_completer(complete)
            print(input())
        s   $	
)inputs	   $complete)textwrapdedentr   rQ   )r%   scriptrS   s      r   test_set_complete_delimsz%TestReadline.test_set_complete_delims   s1     "  x0lF+r   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   ëïr8   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<   lenr   rQ   sysplatformr   r   )r%   locr>   r[   rX   rS   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)rX   envrbs
   last input)r   r2   pathjoinopenwriterangedictenvironr   	readlinesr    rh   strip)
r%   history_sizetest_dirrp   fhistory_filedatar[   ry   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)r@   rA   rB   rD   skipIfr
   r   r   rM   rP   rT   rV   r\   rm   r   r   r   r   rG   rG      s    X__X//&8L_<>&>&44,&[6D X__X//%7JLX__[DF!?FL!?r   rG   __main__)rC   re   r2   ri   r+   rY   rD   test.supportr   test.support.import_helperr   test.support.os_helperr   r   r   test.support.pty_helperr   test.support.script_helperr	   r
   r   r   r   r   rE   TestCaser   rG   r@   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@y?8$$ y?x zHMMO r   