a ze.5@sdZddlmZddlmZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z mZmZmZmZddlmZe dZeedrd ejvZnejod ejvZd d Ze eed dGddde jZGddde jZdddZedkre dS)z: Very minimal unittests for parts of the readline module. ) ExitStack)EION) import_moduleunlinktemp_dirTESTFNverbose)assert_python_okreadline_READLINE_LIBRARY_VERSIONzEditLine wrapperlibeditcCs^trZttdr2tdtjdtdtjdttdrLtdtjtdtdS)N_READLINE_VERSIONzreadline version: z#xzreadline runtime version: r zreadline library version: zuse libedit emulation? )rhasattrr printr Z_READLINE_RUNTIME_VERSIONr is_editlinerrF/opt/bitninja-python-dojo/embedded/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@s:eZdZdZddZeeeddddZ dd Z d S) 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. cCsttdtd|tdd|tdd|tddtdd|tdd|tdd|tdd|tdtd|tdd|tdd|tddS)N first line second linerz replaced line)r r add_history assertEqualget_history_itemreplace_history_itemget_current_history_lengthZremove_history_item)selfrrrtestHistoryUpdates/s    z*TestHistoryManipulation.testHistoryUpdatesappend_history_filezappend_history not availablecCsVtjdd}||j}|t|ttdtdt |t| t dt || t d| t dd| t ddtd|tt || t d| t dd| t dd| t ddt||ttd|Wdn1s>0Yt |dS) NF)deleterrrrr)tempfileNamedTemporaryFileclosename addCleanuprr rrwrite_history_filerrread_history_filerr!osZ assertRaisesFileNotFoundError)rZhfileZ hfilenamerrrtest_write_read_appendFs2          ,z.TestHistoryManipulation.test_write_read_appendc CstztdWn6tyL}z|dt|WYd}~n d}~00tdtddtt| t j ttt tt rtd|tdd|tdddS)Nu entrée 1 Locale cannot encode test data: u entrée 2ru entrée 22dummyr)r rrUnicodeEncodeErrorskipTestformatrr)rr(r+remover*rrr)rerrrrrtest_nonascii_historyms(     z-TestHistoryManipulation.test_nonascii_historyN) __name__ __module__ __qualname____doc__r unittest skipUnlessrr r-r5rrrrr%s  %rc@sneZdZeejdkoe dddZdZ ddZ dd Z d d Z eejd kd eedddZ dS) TestReadlineiz%not supported in this library versioncCs$tdddd\}}}||ddS)N-czimport readlinezxterm-256color)ZTERM)r r)rrcstdoutstderrrrr test_inits zTestReadline.test_initzvimport readline readline.set_auto_history({}) input() print("History length:", readline.get_current_history_length()) cCs t|jd}|d|dS)NTsHistory length: 1run_ptyauto_history_scriptr2assertInroutputrrrtest_auto_history_enabledsz&TestReadline.test_auto_history_enabledcCs t|jd}|d|dS)NFsHistory length: 0rCrGrrrtest_auto_history_disabledsz'TestReadline.test_auto_history_disabledc Csttjd}|dvr&|d|ztdWn6tyj}z|dt|WYd}~n d}~00d}d}|dtd7}|d 7}|d 7}|d 7}t ||}| d || d || d|t st tdr| 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 setlocaleLC_CTYPEr1r rr0r2lenrDrFrr)rlocr4scriptinputrHZexpectedrrr test_nonasciis.(4      zTestReadline.test_nonasciiiz3this readline version does not support history-sizez-editline history size configuration is brokenc CsZd}t8}tj|d}t|d}|d|Wdn1sJ0Ytj|d}t|d6}dddt|d D}||Wdn1s0Yd }ttj}||d <||d <t |d |dt|d}| } Wdn1s0Y| t | || | d dWdn1sL0YdS)N inputrcwbsset history-size %d historyr>css|]}d|VqdS)sitem %d Nr).0irrr sz1TestReadline.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 )rUenvrbs last input)rr+pathjoinopenwriterangedictenvironrD readlinesrrRstrip) rZ history_sizeZtest_dirrXfZ history_filedatarTr^linesrrrtest_history_sizes(  ,   (  (zTestReadline.test_history_sizeN)r6r7r8r:ZskipIfr r rrBrErIrJrVrmrrrrr<s X r< dummy input c Cstd}t}|\}}tjd|f}tj|||||d}t|t f} | |dd} | | || tj|| t } | |t jt jBt|d| D]\} } | t j@r2zt|d}Wn6ty }z|jtkrd}WYd}~n d}~00|s(|WdS||| t j@rz|t||d}Wn8ty}z|jtkrtd}WYd}~n d}~00|s| |t jqqWdn1s0YdS) Nptyr=)stdinr@rAr^cSs$z |WntyYn0dS)N) terminateProcessLookupError)procrrrrq)s  zrun_pty..terminateFir>)r bytearrayopenptysys executable subprocessPopenr+r&r enter_contextcallback selectorsSelectSelectorregister EVENT_READ EVENT_WRITE set_blockingselectreadOSErrorerrnorextendrdmodify)rTrUr^rorHZmasterZslaveargsrscleanuprqZsel_eventschunkr4rrrrD sD            rD__main__)rnN) r9 contextlibrrrrOr+r|rxrvr$r:Z test.supportrrrrrZtest.support.script_helperr r rr rrr;ZTestCaserr<rDr6mainrrrrs2      Y 2