Debian Codegraph @

Lib/_pyrepl/readline.py

raw
1 # Copyright 2000-2010 Michael Hudson-Doyle <[email protected]>
2 # Alex Gaynor
3 # Antonio Cuni
4 # Armin Rigo
5 # Holger Krekel
6 #
7 # All Rights Reserved
8 #
9 #
10 # Permission to use, copy, modify, and distribute this software and
11 # its documentation for any purpose is hereby granted without fee,
12 # provided that the above copyright notice appear in all copies and
13 # that both that copyright notice and this permission notice appear in
14 # supporting documentation.
15 #
16 # THE AUTHOR MICHAEL HUDSON DISCLAIMS ALL WARRANTIES WITH REGARD TO
17 # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 # AND FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
19 # INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 # RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 # CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
24 """A compatibility wrapper reimplementing the 'readline' standard module
25 on top of pyrepl. Not all functionalities are supported. Contains
26 extensions for multiline input.
27 """
28
29 from __future__ import annotations
30
31 import warnings
32 from dataclasses import dataclass, field
33
34 import os
35 from site import gethistoryfile
36 import sys
37 from rlcompleter import Completer as RLCompleter
38
39 from . import commands, historical_reader
40 from .completing_reader import CompletingReader
41 from .console import Console as ConsoleType
42 from ._module_completer import ModuleCompleter, make_default_module_completer
43
44 Console: type[ConsoleType]
45 _error: tuple[type[Exception], ...] | type[Exception]
46
47 if os.name == "nt":
48 from .windows_console import WindowsConsole as Console, _error
49 else:
50 from .unix_console import UnixConsole as Console, _error
51
52 ENCODING = sys.getdefaultencoding() or "latin1"
53
54
55 # types
56 Command = commands.Command
57 from collections.abc import Callable, Collection
58 from .types import Callback, Completer, KeySpec, CommandName
59
60 TYPE_CHECKING = False
61
62 if TYPE_CHECKING:
63 from typing import Any, Mapping
64
65
66 MoreLinesCallable = Callable[[str], bool]
67
68
69 __all__ = [
70 "add_history",
71 "clear_history",
72 "get_begidx",
73 "get_completer",
74 "get_completer_delims",
75 "get_current_history_length",
76 "get_endidx",
77 "get_history_item",
78 "get_history_length",
79 "get_line_buffer",
80 "insert_text",
81 "parse_and_bind",
82 "read_history_file",
83 # "read_init_file",
84 # "redisplay",
85 "remove_history_item",
86 "replace_history_item",
87 "set_auto_history",
88 "set_completer",
89 "set_completer_delims",
90 "set_history_length",
91 # "set_pre_input_hook",
92 "set_startup_hook",
93 "write_history_file",
94 "append_history_file",
95 # ---- multiline extensions ----
96 "multiline_input",
97 ]
98
99 # ____________________________________________________________
100
101 @dataclass
102 class ReadlineConfig:
103 readline_completer: Completer | None = None
104 completer_delims: frozenset[str] = frozenset(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?")
105 module_completer: ModuleCompleter = field(default_factory=make_default_module_completer)
106
107 @dataclass(kw_only=True)
108 class ReadlineAlikeReader(historical_reader.HistoricalReader, CompletingReader):
109 # Class fields
110 assume_immutable_completions = False
111 use_brackets = False
112 sort_in_column = True
113
114 # Instance fields
115 config: ReadlineConfig
116 more_lines: MoreLinesCallable | None = None
117 last_used_indentation: str | None = None
118
119 def __post_init__(self) -> None:
120 super().__post_init__()
121 self.commands["maybe_accept"] = maybe_accept
122 self.commands["maybe-accept"] = maybe_accept
123 self.commands["backspace_dedent"] = backspace_dedent
124 self.commands["backspace-dedent"] = backspace_dedent
125
126 def error(self, msg: str = "none") -> None:
127 pass # don't show error messages by default
128
129 def get_stem(self) -> str:
130 b = self.buffer
131 p = self.pos - 1
132 completer_delims = self.config.completer_delims
133 while p >= 0 and b[p] not in completer_delims:
134 p -= 1
135 return "".join(b[p + 1 : self.pos])
136
137 def get_completions(self, stem: str) -> list[str]:
138 module_completions = self.get_module_completions()
139 if module_completions is not None:
140 return module_completions
141 if len(stem) == 0 and self.more_lines is not None:
142 b = self.buffer
143 p = self.pos
144 while p > 0 and b[p - 1] != "\n":
145 p -= 1
146 num_spaces = 4 - ((self.pos - p) % 4)
147 return [" " * num_spaces]
148 result = []
149 function = self.config.readline_completer
150 if function is not None:
151 try:
152 stem = str(stem) # rlcompleter.py seems to not like unicode
153 except UnicodeEncodeError:
154 pass # but feed unicode anyway if we have no choice
155 state = 0
156 while True:
157 try:
158 next = function(stem, state)
159 except Exception:
160 break
161 if not isinstance(next, str):
162 break
163 result.append(next)
164 state += 1
165 # emulate the behavior of the standard readline that sorts
166 # the completions before displaying them.
167 result.sort()
168 return result
169
170 def get_module_completions(self) -> list[str] | None:
171 line = self.get_line()
172 return self.config.module_completer.get_completions(line)
173
174 def get_trimmed_history(self, maxlength: int) -> list[str]:
175 if maxlength >= 0:
176 cut = len(self.history) - maxlength
177 if cut < 0:
178 cut = 0
179 else:
180 cut = 0
181 return self.history[cut:]
182
183 def update_last_used_indentation(self) -> None:
184 indentation = _get_first_indentation(self.buffer)
185 if indentation is not None:
186 self.last_used_indentation = indentation
187
188 # --- simplified support for reading multiline Python statements ---
189
190 def collect_keymap(self) -> tuple[tuple[KeySpec, CommandName], ...]:
191 return super().collect_keymap() + (
192 (r"\n", "maybe-accept"),
193 (r"\<backspace>", "backspace-dedent"),
194 )
195
196 def after_command(self, cmd: Command) -> None:
197 super().after_command(cmd)
198 if self.more_lines is None:
199 # Force single-line input if we are in raw_input() mode.
200 # Although there is no direct way to add a \n in this mode,
201 # multiline buffers can still show up using various
202 # commands, e.g. navigating the history.
203 try:
204 index = self.buffer.index("\n")
205 except ValueError:
206 pass
207 else:
208 self.buffer = self.buffer[:index]
209 if self.pos > len(self.buffer):
210 self.pos = len(self.buffer)
211
212
213 def set_auto_history(_should_auto_add_history: bool) -> None:
214 """Enable or disable automatic history"""
215 historical_reader.should_auto_add_history = bool(_should_auto_add_history)
216
217
218 def _get_this_line_indent(buffer: list[str], pos: int) -> int:
219 indent = 0
220 while pos > 0 and buffer[pos - 1] in " \t":
221 indent += 1
222 pos -= 1
223 if pos > 0 and buffer[pos - 1] == "\n":
224 return indent
225 return 0
226
227
228 def _get_previous_line_indent(buffer: list[str], pos: int) -> tuple[int, int | None]:
229 prevlinestart = pos
230 while prevlinestart > 0 and buffer[prevlinestart - 1] != "\n":
231 prevlinestart -= 1
232 prevlinetext = prevlinestart
233 while prevlinetext < pos and buffer[prevlinetext] in " \t":
234 prevlinetext += 1
235 if prevlinetext == pos:
236 indent = None
237 else:
238 indent = prevlinetext - prevlinestart
239 return prevlinestart, indent
240
241
242 def _get_first_indentation(buffer: list[str]) -> str | None:
243 indented_line_start = None
244 for i in range(len(buffer)):
245 if (i < len(buffer) - 1
246 and buffer[i] == "\n"
247 and buffer[i + 1] in " \t"
248 ):
249 indented_line_start = i + 1
250 elif indented_line_start is not None and buffer[i] not in " \t\n":
251 return ''.join(buffer[indented_line_start : i])
252 return None
253
254
255 def _should_auto_indent(buffer: list[str], pos: int) -> bool:
256 # check if last character before "pos" is a colon, ignoring
257 # whitespaces and comments.
258 last_char = None
259 while pos > 0:
260 pos -= 1
261 if last_char is None:
262 if buffer[pos] not in " \t\n#": # ignore whitespaces and comments
263 last_char = buffer[pos]
264 else:
265 # even if we found a non-whitespace character before
266 # original pos, we keep going back until newline is reached
267 # to make sure we ignore comments
268 if buffer[pos] == "\n":
269 break
270 if buffer[pos] == "#":
271 last_char = None
272 return last_char == ":"
273
274
275 class maybe_accept(commands.Command):
276 def do(self) -> None:
277 r: ReadlineAlikeReader
278 r = self.reader # type: ignore[assignment]
279 r.dirty = True # this is needed to hide the completion menu, if visible
280
281 # if there are already several lines and the cursor
282 # is not on the last one, always insert a new \n.
283 text = r.get_unicode()
284
285 if "\n" in r.buffer[r.pos :] or (
286 r.more_lines is not None and r.more_lines(text)
287 ):
288 def _newline_before_pos():
289 before_idx = r.pos - 1
290 while before_idx > 0 and text[before_idx].isspace():
291 before_idx -= 1
292 return text[before_idx : r.pos].count("\n") > 0
293
294 # if there's already a new line before the cursor then
295 # even if the cursor is followed by whitespace, we assume
296 # the user is trying to terminate the block
297 if _newline_before_pos() and text[r.pos:].isspace():
298 self.finish = True
299 return
300
301 # auto-indent the next line like the previous line
302 prevlinestart, indent = _get_previous_line_indent(r.buffer, r.pos)
303 r.insert("\n")
304 if not self.reader.paste_mode:
305 if indent:
306 for i in range(prevlinestart, prevlinestart + indent):
307 r.insert(r.buffer[i])
308 r.update_last_used_indentation()
309 if _should_auto_indent(r.buffer, r.pos):
310 if r.last_used_indentation is not None:
311 indentation = r.last_used_indentation
312 else:
313 # default
314 indentation = " " * 4
315 r.insert(indentation)
316 elif not self.reader.paste_mode:
317 self.finish = True
318 else:
319 r.insert("\n")
320
321
322 class backspace_dedent(commands.Command):
323 def do(self) -> None:
324 r = self.reader
325 b = r.buffer
326 if r.pos > 0:
327 repeat = 1
328 if b[r.pos - 1] != "\n":
329 indent = _get_this_line_indent(b, r.pos)
330 if indent > 0:
331 ls = r.pos - indent
332 while ls > 0:
333 ls, pi = _get_previous_line_indent(b, ls - 1)
334 if pi is not None and pi < indent:
335 repeat = indent - pi
336 break
337 r.pos -= repeat
338 del b[r.pos : r.pos + repeat]
339 r.dirty = True
340 else:
341 self.reader.error("can't backspace at start")
342
343
344 # ____________________________________________________________
345
346
347 @dataclass(slots=True)
348 class _ReadlineWrapper:
349 f_in: int = -1
350 f_out: int = -1
351 reader: ReadlineAlikeReader | None = field(default=None, repr=False)
352 saved_history_length: int = -1
353 startup_hook: Callback | None = None
354 config: ReadlineConfig = field(default_factory=ReadlineConfig, repr=False)
355
356 def __post_init__(self) -> None:
357 if self.f_in == -1:
358 self.f_in = os.dup(0)
359 if self.f_out == -1:
360 self.f_out = os.dup(1)
361
362 def get_reader(self) -> ReadlineAlikeReader:
363 if self.reader is None:
364 console = Console(self.f_in, self.f_out, encoding=ENCODING)
365 self.reader = ReadlineAlikeReader(console=console, config=self.config)
366 return self.reader
367
368 def input(self, prompt: object = "") -> str:
369 try:
370 reader = self.get_reader()
371 except _error:
372 assert raw_input is not None
373 return raw_input(prompt)
374 prompt_str = str(prompt)
375 reader.ps1 = prompt_str
376 sys.audit("builtins.input", prompt_str)
377 result = reader.readline(startup_hook=self.startup_hook)
378 sys.audit("builtins.input/result", result)
379 return result
380
381 def multiline_input(self, more_lines: MoreLinesCallable, ps1: str, ps2: str) -> str:
382 """Read an input on possibly multiple lines, asking for more
383 lines as long as 'more_lines(unicodetext)' returns an object whose
384 boolean value is true.
385 """
386 reader = self.get_reader()
387 saved = reader.more_lines
388 try:
389 reader.more_lines = more_lines
390 reader.ps1 = ps1
391 reader.ps2 = ps1
392 reader.ps3 = ps2
393 reader.ps4 = ""
394 with warnings.catch_warnings(action="ignore"):
395 return reader.readline()
396 finally:
397 reader.more_lines = saved
398 reader.paste_mode = False
399
400 def parse_and_bind(self, string: str) -> None:
401 pass # XXX we don't support parsing GNU-readline-style init files
402
403 def set_completer(self, function: Completer | None = None) -> None:
404 self.config.readline_completer = function
405
406 def get_completer(self) -> Completer | None:
407 return self.config.readline_completer
408
409 def set_completer_delims(self, delimiters: Collection[str]) -> None:
410 self.config.completer_delims = frozenset(delimiters)
411
412 def get_completer_delims(self) -> str:
413 return "".join(sorted(self.config.completer_delims))
414
415 def _histline(self, line: str) -> str:
416 line = line.rstrip("\n")
417 return line
418
419 def get_history_length(self) -> int:
420 return self.saved_history_length
421
422 def set_history_length(self, length: int) -> None:
423 self.saved_history_length = length
424
425 def get_current_history_length(self) -> int:
426 return len(self.get_reader().history)
427
428 def read_history_file(self, filename: str = gethistoryfile()) -> None:
429 # multiline extension (really a hack) for the end of lines that
430 # are actually continuations inside a single multiline_input()
431 # history item: we use \r\n instead of just \n. If the history
432 # file is passed to GNU readline, the extra \r are just ignored.
433 history = self.get_reader().history
434
435 with open(os.path.expanduser(filename), 'rb') as f:
436 is_editline = f.readline().startswith(b"_HiStOrY_V2_")
437 if is_editline:
438 encoding = "unicode-escape"
439 else:
440 f.seek(0)
441 encoding = "utf-8"
442
443 lines = [line.decode(encoding, errors='replace') for line in f.read().split(b'\n')]
444 buffer = []
445 for line in lines:
446 if line.endswith("\r"):
447 buffer.append(line+'\n')
448 else:
449 line = self._histline(line)
450 if buffer:
451 line = self._histline("".join(buffer).replace("\r", "") + line)
452 del buffer[:]
453 if line:
454 history.append(line)
455 self.set_history_length(self.get_current_history_length())
456
457 def write_history_file(self, filename: str = gethistoryfile()) -> None:
458 maxlength = self.saved_history_length
459 history = self.get_reader().get_trimmed_history(maxlength)
460 f = open(os.path.expanduser(filename), "w",
461 encoding="utf-8", newline="\n")
462 with f:
463 for entry in history:
464 entry = entry.replace("\n", "\r\n") # multiline history support
465 f.write(entry + "\n")
466
467 def append_history_file(self, filename: str = gethistoryfile()) -> None:
468 reader = self.get_reader()
469 saved_length = self.get_history_length()
470 length = self.get_current_history_length() - saved_length
471 history = reader.get_trimmed_history(length)
472 f = open(os.path.expanduser(filename), "a",
473 encoding="utf-8", newline="\n")
474 with f:
475 for entry in history:
476 entry = entry.replace("\n", "\r\n") # multiline history support
477 f.write(entry + "\n")
478 self.set_history_length(saved_length + length)
479
480 def clear_history(self) -> None:
481 del self.get_reader().history[:]
482
483 def get_history_item(self, index: int) -> str | None:
484 history = self.get_reader().history
485 if 1 <= index <= len(history):
486 return history[index - 1]
487 else:
488 return None # like readline.c
489
490 def remove_history_item(self, index: int) -> None:
491 history = self.get_reader().history
492 if 0 <= index < len(history):
493 del history[index]
494 else:
495 raise ValueError("No history item at position %d" % index)
496 # like readline.c
497
498 def replace_history_item(self, index: int, line: str) -> None:
499 history = self.get_reader().history
500 if 0 <= index < len(history):
501 history[index] = self._histline(line)
502 else:
503 raise ValueError("No history item at position %d" % index)
504 # like readline.c
505
506 def add_history(self, line: str) -> None:
507 self.get_reader().history.append(self._histline(line))
508
509 def set_startup_hook(self, function: Callback | None = None) -> None:
510 self.startup_hook = function
511
512 def get_line_buffer(self) -> str:
513 return self.get_reader().get_unicode()
514
515 def _get_idxs(self) -> tuple[int, int]:
516 start = cursor = self.get_reader().pos
517 buf = self.get_line_buffer()
518 for i in range(cursor - 1, -1, -1):
519 if buf[i] in self.get_completer_delims():
520 break
521 start = i
522 return start, cursor
523
524 def get_begidx(self) -> int:
525 return self._get_idxs()[0]
526
527 def get_endidx(self) -> int:
528 return self._get_idxs()[1]
529
530 def insert_text(self, text: str) -> None:
531 self.get_reader().insert(text)
532
533
534 _wrapper = _ReadlineWrapper()
535
536 # ____________________________________________________________
537 # Public API
538
539 parse_and_bind = _wrapper.parse_and_bind
540 set_completer = _wrapper.set_completer
541 get_completer = _wrapper.get_completer
542 set_completer_delims = _wrapper.set_completer_delims
543 get_completer_delims = _wrapper.get_completer_delims
544 get_history_length = _wrapper.get_history_length
545 set_history_length = _wrapper.set_history_length
546 get_current_history_length = _wrapper.get_current_history_length
547 read_history_file = _wrapper.read_history_file
548 write_history_file = _wrapper.write_history_file
549 append_history_file = _wrapper.append_history_file
550 clear_history = _wrapper.clear_history
551 get_history_item = _wrapper.get_history_item
552 remove_history_item = _wrapper.remove_history_item
553 replace_history_item = _wrapper.replace_history_item
554 add_history = _wrapper.add_history
555 set_startup_hook = _wrapper.set_startup_hook
556 get_line_buffer = _wrapper.get_line_buffer
557 get_begidx = _wrapper.get_begidx
558 get_endidx = _wrapper.get_endidx
559 insert_text = _wrapper.insert_text
560
561 # Extension
562 multiline_input = _wrapper.multiline_input
563
564 # Internal hook
565 _get_reader = _wrapper.get_reader
566
567 # ____________________________________________________________
568 # Stubs
569
570
571 def _make_stub(_name: str, _ret: object) -> None:
572 def stub(*args: object, **kwds: object) -> None:
573 import warnings
574
575 warnings.warn("readline.%s() not implemented" % _name, stacklevel=2)
576
577 stub.__name__ = _name
578 globals()[_name] = stub
579
580
581 for _name, _ret in [
582 ("read_init_file", None),
583 ("redisplay", None),
584 ("set_pre_input_hook", None),
585 ]:
586 assert _name not in globals(), _name
587 _make_stub(_name, _ret)
588
589 # ____________________________________________________________
590
591
592 def _setup(namespace: Mapping[str, Any]) -> None:
593 global raw_input
594 if raw_input is not None:
595 return # don't run _setup twice
596
597 try:
598 f_in = sys.stdin.fileno()
599 f_out = sys.stdout.fileno()
600 except (AttributeError, ValueError):
601 return
602 if not os.isatty(f_in) or not os.isatty(f_out):
603 return
604
605 _wrapper.f_in = f_in
606 _wrapper.f_out = f_out
607
608 # set up namespace in rlcompleter, which requires it to be a bona fide dict
609 if not isinstance(namespace, dict):
610 namespace = dict(namespace)
611 _wrapper.config.module_completer = ModuleCompleter(namespace)
612 _wrapper.config.readline_completer = RLCompleter(namespace).complete
613
614 # this is not really what readline.c does. Better than nothing I guess
615 import builtins
616 raw_input = builtins.input
617 builtins.input = _wrapper.input
618
619
620 raw_input: Callable[[object], str] | None = None