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