
    7bh>                        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 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j4                   eed      d       G d dej6                               Z G d dej6                        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)requires_working_threading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     </opt/python-3.12.12/usr/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                   b    e Zd ZdZd Z ej                   eed      d      d        Z	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.testHistoryUpdates0   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                  |       | j#                  t$              5  t        j                  d	|       d d d        | j#                  t$              5  t        j                  d
|       d d d        y # t         $ r Y w xY w# 1 sw Y   MxY w# 1 sw Y   y xY w)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_appendz.TestHistoryManipulation.test_write_read_appendG   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( K7L(	K43K47L L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_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/EEc                    t         j                         }| j                  t         j                  |       t         j	                          t         j                  d       t         j                  d       t         j                  d       t         j                  d       | j                  t         j                         d       t         j                  t               | j                  t        j                  t               t         j	                          | j                  t         j                         d       | j                  t         j                         d       t         j                  t               | j                  t         j                  d      d       | j                  t         j                  d      d       | j                  t         j                  d      d        | j                  t         j                         d       y )	Nr   r   
third liner   r   r   r+   )r   r+   )r   get_history_lengthr0   set_history_lengthr   r    r!   r1   r   r3   r@   r$   r2   r"   assertInr&   previous_lengths     r   test_write_read_limited_historyz7TestHistoryManipulation.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   N)__name__
__module____qualname____doc__r'   unittest
skipUnlessr   r   r9   rB   rJ   r   r   r   r   r   &   sJ    C. X+@A792A92AhE&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 e       d        Z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?   rG   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: 0rZ   r\   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   rG   r&   scriptr]   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   ëïr;   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   rG   sysplatformr   r   )r&   locrA   rh   rc   r]   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>M  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)rc   envrbs
   last input)r   r3   pathjoinopenwriterangedictenvironr   	readlinesr!   ru   strip)
r&   history_sizetest_dirr}   fhistory_filedatarh   r   liness
             r   test_history_sizezTestReadline.test_history_size?  sJ   
 Z8ggll8Y7Ggt$0<?@ % 77<<)<LlD)Qxx  B).|a/?)@ B B	 *F rzz"C$C	N".CF/s;lD)Q *SZ6U2Y__.>? Z$$ *), *)9 ZsT   -FE 4F6E-9AF E9AF E*	%F-E6	2F9F	>FFc                 l    t        j                  d      }t        |d      }| j                  d|       y)z7gh-123321: readline should be thread-safe and not crasha  
            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rb   s   doneNrd   rg   s      r   test_gh123321_threadsafez%TestReadline.test_gh123321_threadsafef  s4      "   ':;gv&r   c                    t         j                         }| j                  t         j                  |       t         j	                  d       t         j	                  d       t         j	                  d       t         j                  d       | j                  t         j                         d       t         j                  t               | j                  t        j                  t               t         j                  t               y )Nr   r   rD   r   )r   rE   r0   rF   r    r!   r1   r   r3   r@   r2   rH   s     r   rJ   z,TestReadline.test_write_read_limited_history~  s    "55733_E\*]+\*##A&446:##F+		6*""6*r   N)rK   rL   rM   rO   skipIfr   r   r   rX   r[   r^   r`   ri   rz   r   r
   r   rJ   r   r   r   rR   rR      s    X__X//&8L_<>&>&44,&[6D X__X//%7JLX__[DF!?FL!?F  !' "'.+r   rR   __main__) rN   rr   r3   rv   r,   re   rO   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   r   rP   TestCaser   rR   rK   mainr   r   r   <module>r      s     	 
      4 ; ; + 7 D $
801%)K)KKK##E	X5E5E(EK	7 WX7>?Ex00 E?ED`+8$$ `+P zHMMO r   