a 5êdgÉ3ã@sddlmZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z m Z mZmZmZddlmZe dƒZeedƒr–dejvZnejo¤d ejvZd d „Ze  eed ƒd ¡Gdd„de jƒƒZGdd„de jƒZddd„Zedkre  ¡dS)é)Ú ExitStack)ÚEION)Ú import_moduleÚunlinkÚtemp_dirÚTESTFNÚverbose)Úassert_python_okÚreadlineÚ_READLINE_LIBRARY_VERSIONzEditLine wrapperÚlibeditcCs^trZttdƒr2tdtjd›ƒtdtjd›ƒttdƒrLtdtj›ƒtdt›ƒdS)NÚ_READLINE_VERSIONzreadline version: z#xzreadline runtime version: r zreadline library version: zuse libedit emulation? )rÚhasattrr Úprintr Z_READLINE_RUNTIME_VERSIONr Ú is_editline©rrú./usr/local/lib/python3.9/test/test_readline.pyÚ setUpModules  rÚ clear_historyzXThe history update test cannot be run because the clear_history method is not available.c@s6eZdZdd„Ze eedƒd¡dd„ƒZdd„Z d S) ÚTestHistoryManipulationcCsæt ¡t d¡t d¡| t d¡d¡| t d¡d¡| t d¡d¡t dd¡| t d¡d¡| t d¡d¡| t d¡d¡| t ¡d¡t d¡| t d¡d¡| t d¡d¡| t ¡d¡dS)Nú first lineú second linerééz replaced line)r rÚ add_historyÚ assertEqualÚget_history_itemÚreplace_history_itemÚget_current_history_lengthZremove_history_item)ÚselfrrrÚtestHistoryUpdates/s    z*TestHistoryManipulation.testHistoryUpdatesÚappend_history_filezappend_history not availablecCsVtjdd}| ¡|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 |¡| t¡t d|¡Wdƒn1s>0Yt  |¡dS) NF)Údeleterrrrré)ÚtempfileÚNamedTemporaryFileÚcloseÚnameÚ addCleanuprr rrÚwrite_history_filerrÚread_history_filerr!ÚosZ assertRaisesÚFileNotFoundError)rZhfileZ hfilenamerrrÚtest_write_read_appendFs2          ,z.TestHistoryManipulation.test_write_read_appendc CsÄt ¡zt d¡Wn6tyL}z| dt|ƒ¡WYd}~n d}~00t d¡t dd¡t t¡|  t j t¡t ¡t  t¡t rœt d¡| t d¡d¡| t d¡d¡dS)Nu entrée 1ú Locale cannot encode test data: u entrée 2ru entrée 22Údummyr)r rrÚUnicodeEncodeErrorÚskipTestÚformatrr)rr(r+Úremover*rrr)rÚerrrrrÚtest_nonascii_historyms(     z-TestHistoryManipulation.test_nonascii_historyN) Ú__name__Ú __module__Ú __qualname__r ÚunittestÚ skipUnlessrr r-r5rrrrr%s   ÿ %rc@sneZdZe ejdkoe d¡dd„ƒZdZ dd„Z dd „Z d d „Z e ejd kd ¡e ed¡dd„ƒƒZ dS)Ú TestReadlineiz%not supported in this library versioncCs$tdddd\}}}| |d¡dS)Nú-czimport readlinezxterm-256color)ZTERMó)r r)rÚrcÚstdoutÚstderrrrrÚ test_initƒsÿ zTestReadline.test_initzvimport readline readline.set_auto_history({}) input() print("History length:", readline.get_current_history_length()) cCs t|j d¡ƒ}| d|¡dS)NTsHistory length: 1 ©Úrun_ptyÚauto_history_scriptr2ÚassertIn©rÚoutputrrrÚtest_auto_history_enabled”sz&TestReadline.test_auto_history_enabledcCs t|j d¡ƒ}| d|¡dS)NFsHistory length: 0 rBrFrrrÚtest_auto_history_disabled˜sz'TestReadline.test_auto_history_disabledc Cs t tjd¡}|dvr&| d|›¡zt d¡Wn6tyj}z| dt|ƒ¡WYd}~n d}~00d}d}|dtdƒ7}|d 7}|d 7}|d 7}t ||ƒ}|  d |¡|  d |¡|  d|¡t sðt tdƒrð|  d|¡|  d|¡d}|  d|d|¡|  d|d|¡dS)N)ÚCZPOSIXzthe 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 sx ó stext 't\xeb' s#line '[\xefnserted]|t\xeb[after]' sindexes 11 13 Zset_pre_input_hookssubstitution 't\xeb' s matches ['t\xebnt', 't\xebxt'] s'[\xefnserted]|t\xebxt[after]'sresult s shistory ) ÚlocaleÚ setlocaleÚLC_CTYPEr1r rr0r2ÚlenrCrErr)rÚlocr4ÚscriptÚinputrGZexpectedrrrÚ test_nonasciiœs.(4      zTestReadline.test_nonasciiiz3this readline version does not support history-sizez-editline history size configuration is brokenc CsZd}tƒ8}tj |d¡}t|dƒ}| d|¡Wdƒn1sJ0Ytj |d¡}t|dƒ6}d dd„t|d ƒDƒ¡}| |¡Wdƒn1s¨0Yd }ttjƒ}||d <||d <t |d |dt|dƒ}|  ¡} Wdƒn1s0Y|  t | ƒ|¡|  | d  ¡d¡Wdƒn1sL0YdS)Né ÚinputrcÚwbsset history-size %d Úhistoryr=css|]}d|VqdS)sitem %d Nr)Ú.0ÚirrrÚ sÿz1TestReadline.test_history_size..rz¡ import os import readline history_file = os.environ["HISTORY_FILE"] readline.read_history_file(history_file) input() readline.write_history_file(history_file) ZINPUTRCZ HISTORY_FILEs last input )rTÚenvÚrbéÿÿÿÿs last input)rr+ÚpathÚjoinÚopenÚwriteÚrangeÚdictÚenvironrCÚ readlinesrrQÚstrip) rZ history_sizeZtest_dirrWÚfZ history_fileÚdatarSr]ÚlinesrrrÚtest_history_sizeòs(  ,   ÿ(  (zTestReadline.test_history_sizeN)r6r7r8r9ZskipIfr r rrArDrHrIrUrlrrrrr;sÿ V ÿÿr;ó dummy input c CsÈtdƒ}tƒ}| ¡\}}tjd|f}tj|||||d}t |¡t ƒf} |   |¡dd„} |   | |¡|   tj|¡|   t   ¡¡} |  |t jt jB¡t |d¡|  ¡D]ð\} } | t j@r2zt |d¡}Wn6ty }z|jtkrò‚d}WYd}~n d}~00|s(|WdƒS| |¡| t j@r°z|t ||¡d…}Wn8tyŒ}z|jtkrt‚d}WYd}~n d}~00|s°|  |t j¡q°q¨Wdƒn1sº0YdS) NÚptyr<)Ústdinr?r@r]cSs$z | ¡WntyYn0dS)N)Ú terminateÚProcessLookupError)Úprocrrrrp#s  zrun_pty..terminateFir=)rÚ bytearrayÚopenptyÚsysÚ executableÚ subprocessÚPopenr+r&rÚ enter_contextÚcallbackÚ selectorsÚSelectSelectorÚregisterÚ EVENT_READÚ EVENT_WRITEÚ set_blockingÚselectÚreadÚOSErrorÚerrnorÚextendrcÚmodify)rSrTr]rnrGZmasterZslaveÚargsrrÚcleanuprpZselÚ_ÚeventsÚchunkr4rrrrCsD            rCÚ__main__)rmN) Ú contextlibrr„rrNr+r{rwrur$r9Z test.supportrrrrrZtest.support.script_helperr r rr rÚ__doc__rr:ZTestCaserr;rCr6ÚmainrrrrÚs0      ÿY 2