
    0hh@                        S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
  S SKJrJrJr  S SKJr  S SKJr  S SKJr  \
" S5      r\" \S	5      (       a  S
\R.                  ;   rO\R2                  S:H  rS r\R6                  " \" \S5      S5       " S S\R8                  5      5       r " S S\R8                  5      r\S:X  a  \R@                  " 5         gg)    N)verbose)import_module)unlinktemp_dirTESTFN)run_pty)assert_python_ok)requires_working_threadingreadline_READLINE_LIBRARY_VERSIONzEditLine wrappereditlinec                  B   [         (       a  [        [        S5      (       a:  [        S[        R                  S 35        [        S[        R
                  S 35        [        [        S5      (       a  [        S[        R                  < 35        [        S[         35        g g )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     ;/opt/python-3.13.8/usr/lib/python3.13/test/test_readline.pysetUpModuler      s    w 8011&x'A'A"&EFG.x/Q/QRT.UVW889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                   f    \ rS rSrS r\R                  " \" \S5      S5      S 5       r	S r
S rSrg	)
TestHistoryManipulation'   c                    [         R                  5         [         R                  S5        [         R                  S5        U R                  [         R	                  S5      S 5        U R                  [         R	                  S5      S5        U R                  [         R	                  S5      S5        [         R                  SS5        U R                  [         R	                  S5      S 5        U R                  [         R	                  S5      S5        U R                  [         R	                  S5      S5        U R                  [         R                  5       S5        [         R                  S5        U R                  [         R	                  S5      S 5        U R                  [         R	                  S5      S5        U R                  [         R                  5       S5        g )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   testHistoryUpdates*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                 4   [         R                  " SS9nUR                  5         UR                  nU R	                  [
        U5        [        R                  5         [        R                  S5        [        R                  S5        [        R                  U5        [        R                  5         U R                  [        R                  5       S5        [        R                  U5        U R                  [        R                  5       S5        U R                  [        R                  S5      S5        U R                  [        R                  S5      S5        [        R                  SU5        [        R                  5         [        R                  U5        U R                  [        R                  5       S5        U R                  [        R                  S5      S5        U R                  [        R                  S5      S5        U R                  [        R                  S5      S5        [        R
                  " U5         [        R                  SU5        [        R
                  " U5        [        R                  U5        U R#                  [$        5         [        R                  S	U5        S S S 5        U R#                  [$        5         [        R                  S
U5        S S S 5        g ! [          a     Nf = f! , (       d  f       NS= f! , (       d  f       g = f)NF)deleter   r   r   r    r      ii   )tempfileNamedTemporaryFileclosename
addCleanupr   r   r   r!   write_history_filer"   r%   read_history_filer#   r+   osFileNotFoundErrorassertRaises
ValueError)r(   hfile	hfilenames      r   test_write_read_append.TestHistoryManipulation.test_write_read_appendH   s8    ++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. z*((i8 + z*((i@ +*! ! 		 +* +*s*   >K( K8L	(
K54K58
L	
Lc                    [         R                  5          [         R                  S5        [         R                  S5        [         R                  SS5        [         R                  [        5        U R                  [        R                  [        5        [         R                  5         [         R                  [        5        [        (       a  [         R                  S5        U R                  [         R                  S5      S5        U R                  [         R                  S5      S5        g ! [         a(  nU R	                  S[        U5      -   5         S nAGN-S nAff = f)Nu	   entrée 1 Locale cannot encode test data: u	   entrée 2r   u
   entrée 22dummyr    )r   r   r!   UnicodeEncodeErrorskipTestformatr$   r4   r   r3   r6   remover5   r   r"   r#   )r(   errs     r   test_nonascii_history-TestHistoryManipulation.test_nonascii_history~   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5EEc                    [         R                  5       nU R                  [         R                  U5        [         R	                  5         [         R                  S5        [         R                  S5        [         R                  S5        [         R                  S5        U R                  [         R                  5       S5        [         R                  [        5        U R                  [        R                  [        5        [         R	                  5         U R                  [         R                  5       S5        U R                  [         R                  5       S5        [         R                  [        5        U R                  [         R                  S5      S5        U R                  [         R                  S5      S5        U R                  [         R                  S5      S 5        U R                  [         R                  5       S5        g )	Nr   r   
third liner    r   r   r.   )r    r.   )r   get_history_lengthr3   set_history_lengthr   r!   r"   r4   r   r6   rD   r%   r5   r#   assertInr(   previous_lengths     r   test_write_read_limited_history7TestHistoryManipulation.test_write_read_limited_history   sW   "55733_E \*]+\*##A&446:##F+		6* <<>B446:""6*2215}E2215|D2215t< 	h99;VDr   r   N)__name__
__module____qualname____firstlineno__r)   unittest
skipUnlessr   r   r<   rF   rO   __static_attributes__r   r   r   r   r   '   sC    C. +@A792A92AhE&Er   r   c                   6   \ rS rSr\R
                  " \R                  S:  =(       a    \(       + S5      S 5       r	S r
SrS rS rS	 rS
 r\R
                  " \R                  S:  S5      \R
                  " \S5      S 5       5       r\" 5       S 5       rS rSrg)TestReadline   i  z%not supported in this library versionc                 D    [        SSSS9u  pnU R                  US5        g )Nz-czimport readlinezxterm-256color)TERMr   )r	   r"   )r(   rcstdoutstderrs       r   	test_initTestReadline.test_init   s,     .d4E3CEF%r   c                 D    U R                  [        R                  S5        g )N)r   r   )rL   r   backendr'   s    r   test_backendTestReadline.test_backend   s    h&&(@Ar   zvimport readline
readline.set_auto_history({})
input()
print("History length:", readline.get_current_history_length())
c                 p    [        U R                  R                  S5      5      nU R                  SU5        g )NTs   History length: 1r   auto_history_scriptrC   rL   r(   outputs     r   test_auto_history_enabled&TestReadline.test_auto_history_enabled   s.    1188>? 	*F3r   c                 p    [        U R                  R                  S5      5      nU R                  SU5        g )NFs   History length: 0rg   ri   s     r   test_auto_history_disabled'TestReadline.test_auto_history_disabled   s.    1188?@ 	*F3r   c                 h    [         R                  " S5      n[        USS9nU R                  SU5        g )Na  
            import readline
            def complete(text, state):
                if state == 0 and text == "$":
                    return "$complete"
                return None
            if readline.backend == "editline":
                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   rL   r(   scriptrj   s      r   test_set_complete_delims%TestReadline.test_set_complete_delims   s1     "  x0lF+r   c                    [         R                  " [         R                  S 5      nUS;   a  U R                  SU< 35        [        R
                  R                  (       aP  [         R                  " 5       n[        R                  " U5      R                  nUS:w  a  U R                  SU< 35         [        R                  S5        SnSnUS	[        S
5      -  -  nUS-  nUS-  nUS-  n[!        XE5      nU R#                  SU5        U R#                  SU5        [        R$                  S:X  d  [&        (       d  U R#                  SU5        [&        (       d9  [)        [        S5      (       a$  U R#                  SU5        U R#                  SU5        SnU R#                  SU-   S-   U5        U R#                  SU-   U5        g ! [         a(  nU R                  S[        U5      -   5         S nAGN%S nAff = f)N)CPOSIXzthe LC_CTYPE locale is zutf-8zthe LC_CTYPE encoding is u   ëïr?   a  import readline

is_editline = readline.backend == "editline"
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_CTYPErB   sysflags	utf8_modegetencodingcodecslookupr2   r   r!   rA   rC   lenr   rL   platformr   r   )r(   locencodingrE   rw   rr   rj   expecteds           r   test_nonasciiTestReadline.test_nonascii   s   v5.  MM3C7;<99))+H}}X.33H7"  9(FG	L  ,2h 3y>))'*F3@&I<<8#;;MM.7 {wx1EFFMM6?MMA6J5j8+g5v> 	kH,f5c " 	LMM<vc{JKK	Ls   1F> >
G0G++G0i   z3this readline version does not support history-sizez-editline history size configuration is brokenc           	      @   Sn[        5        n[        R                  R                  US5      n[	        US5       nUR                  SU-  5        S S S 5        [        R                  R                  US5      n[	        US5       nSR                  S [        US-  5       5       5      nUR                  U5        S S S 5        S	n[        [        R                  5      nX8S
'   XXS'   [        USUS9  [	        US5       nUR                  5       n	S S S 5        U R                  [        W	5      U5        U R                  U	S   R                  5       S5        S S S 5        g ! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       Nx= f! , (       d  f       g = f)N
   inputrcwbs   set history-size %d
historyr   c              3   ,   #    U  H
  nS U-  v   M     g7f)s   item %d
Nr   ).0is     r   	<genexpr>1TestReadline.test_history_size.<locals>.<genexpr>X  s       B)@A !-q 0)@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)rr   envrbs
   last input)r   r6   pathjoinopenwriterangedictenvironr   	readlinesr"   r   strip)
r(   history_sizetest_dirr   fhistory_filedatarw   r   liness
             r   test_history_sizeTestReadline.test_history_sizeJ  sF   
 Z8ggll8Y7Ggt$0<?@ % 77<<)<LlD)Qxx  B).|a/?)@ B B	 *F rzz"C$	N".F/s;lD)Q *SZ6U2Y__.>? Z$$ *), *)9 ZsT   -FE4F6E-9AF;E>AF
E*	%F-
E;	7F>
F	F
Fc                 h    [         R                  " S5      n[        USS9nU R                  SU5        g )Na  
            import threading
            from test.support.threading_helper import join_thread

            def func():
                input()

            thread1 = threading.Thread(target=func)
            thread2 = threading.Thread(target=func)
            thread1.start()
            thread2.start()
            join_thread(thread1)
            join_thread(thread2)
            print("done")
        s   input1input2rq   s   doners   rv   s      r   test_gh123321_threadsafe%TestReadline.test_gh123321_threadsafeq  s4      "   ':;gv&r   c                    [         R                  5       nU R                  [         R                  U5        [         R	                  S5        [         R	                  S5        [         R	                  S5        [         R                  S5        U R                  [         R                  5       S5        [         R                  [        5        U R                  [        R                  [        5        [         R                  [        5        g )Nr   r   rI   r    )r   rJ   r3   rK   r!   r"   r4   r   r6   rD   r5   rM   s     r   rO   ,TestReadline.test_write_read_limited_history  s    "55733_E\*]+\*##A&446:##F+		6*""6*r   r   N)rQ   rR   rS   rT   rU   skipIfr   r   r   r`   rd   rh   rk   rn   rx   r   r   r
   r   rO   rW   r   r   r   rY   rY      s    __X//&8L_<>&>&B44,&b6R __X//%7JL__[DF!?FL!?F  !' "'.+r   rY   __main__)!r   r   r6   r   r/   rt   rU   test.supportr   test.support.import_helperr   test.support.os_helperr   r   r   test.support.pty_helperr   test.support.script_helperr	   test.support.threading_helperr
   r   r   r   r   rc   r   rV   TestCaser   rY   rQ   mainr   r   r   <module>r      s      	 
      4 ; ; + 7 D $
8011%)K)KKK""j0K	7 
WX7>?Ex00 E?EDj+8$$ j+d zMMO r   