| 226 | self.assertIn(b"$complete", output) |
| 227 | |
| 228 | def test_nonascii(self): |
| 229 | loc = locale.setlocale(locale.LC_CTYPE, None) |
| 230 | if loc in ('C', 'POSIX'): |
| 231 | # bpo-29240: On FreeBSD, if the LC_CTYPE locale is C or POSIX, |
| 232 | # writing and reading non-ASCII bytes into/from a TTY works, but |
| 233 | # readline or ncurses ignores non-ASCII bytes on read. |
| 234 | self.skipTest(f"the LC_CTYPE locale is {loc!r}") |
| 235 | if sys.flags.utf8_mode: |
| 236 | encoding = locale.getencoding() |
| 237 | encoding = codecs.lookup(encoding).name # normalize the name |
| 238 | if encoding != "utf-8": |
| 239 | # gh-133711: The Python UTF-8 Mode ignores the LC_CTYPE locale |
| 240 | # and always use the UTF-8 encoding. |
| 241 | self.skipTest(f"the LC_CTYPE encoding is {encoding!r}") |
| 242 | |
| 243 | try: |
| 244 | readline.add_history("\xEB\xEF") |
| 245 | except UnicodeEncodeError as err: |
| 246 | self.skipTest("Locale cannot encode test data: " + format(err)) |
| 247 | |
| 248 | script = r"""import readline |
| 249 | |
| 250 | is_editline = readline.backend == "editline" |
| 251 | inserted = "[\xEFnserted]" |
| 252 | macro = "|t\xEB[after]" |
| 253 | set_pre_input_hook = getattr(readline, "set_pre_input_hook", None) |
| 254 | if is_editline or not set_pre_input_hook: |
| 255 | # The insert_line() call via pre_input_hook() does nothing with Editline, |
| 256 | # so include the extra text that would have been inserted here |
| 257 | macro = inserted + macro |
| 258 | |
| 259 | if is_editline: |
| 260 | readline.parse_and_bind(r'bind ^B ed-prev-char') |
| 261 | readline.parse_and_bind(r'bind "\t" rl_complete') |
| 262 | readline.parse_and_bind(r'bind -s ^A "{}"'.format(macro)) |
| 263 | else: |
| 264 | readline.parse_and_bind(r'Control-b: backward-char') |
| 265 | readline.parse_and_bind(r'"\t": complete') |
| 266 | readline.parse_and_bind(r'set disable-completion off') |
| 267 | readline.parse_and_bind(r'set show-all-if-ambiguous off') |
| 268 | readline.parse_and_bind(r'set show-all-if-unmodified off') |
| 269 | readline.parse_and_bind(r'Control-a: "{}"'.format(macro)) |
| 270 | |
| 271 | def pre_input_hook(): |
| 272 | readline.insert_text(inserted) |
| 273 | readline.redisplay() |
| 274 | if set_pre_input_hook: |
| 275 | set_pre_input_hook(pre_input_hook) |
| 276 | |
| 277 | def completer(text, state): |
| 278 | if text == "t\xEB": |
| 279 | if state == 0: |
| 280 | print("text", ascii(text)) |
| 281 | print("line", ascii(readline.get_line_buffer())) |
| 282 | print("indexes", readline.get_begidx(), readline.get_endidx()) |
| 283 | return "t\xEBnt" |
| 284 | if state == 1: |
| 285 | return "t\xEBxt" |