Skip to content

Commit b86de33

Browse files
committed
Make implementations of current_line and cursor_offset consistent
1 parent e906df7 commit b86de33

File tree

4 files changed

+73
-49
lines changed

4 files changed

+73
-49
lines changed

bpython/cli.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,6 @@ def _get_cursor_offset(self) -> int:
383383
def _set_cursor_offset(self, offset: int) -> None:
384384
self.cpos = len(self.s) - offset
385385

386-
cursor_offset = property(
387-
_get_cursor_offset,
388-
_set_cursor_offset,
389-
None,
390-
"The cursor offset from the beginning of the line",
391-
)
392-
393386
def addstr(self, s: str) -> None:
394387
"""Add a string to the current input line and figure out
395388
where it should go, depending on the cursor position."""
@@ -539,13 +532,6 @@ def _get_current_line(self) -> str:
539532
def _set_current_line(self, line: str) -> None:
540533
self.s = line
541534

542-
current_line = property(
543-
_get_current_line,
544-
_set_current_line,
545-
None,
546-
"The characters of the current line",
547-
)
548-
549535
def cut_to_buffer(self) -> None:
550536
"""Clear from cursor to end of line, placing into cut buffer"""
551537
self.cut_buffer = self.s[-self.cpos :]

bpython/curtsiesfrontend/repl.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,18 +1866,13 @@ def __repr__(self):
18661866
lines scrolled down: {self.scroll_offset}
18671867
>"""
18681868

1869-
@property
1870-
def current_line(self):
1869+
def _get_current_line(self) -> str:
18711870
"""The current line"""
18721871
return self._current_line
18731872

1874-
@current_line.setter
1875-
def current_line(self, value):
1876-
self._set_current_line(value)
1877-
18781873
def _set_current_line(
18791874
self,
1880-
line,
1875+
line: str,
18811876
update_completion=True,
18821877
reset_rl_history=True,
18831878
clear_special_mode=True,
@@ -1895,18 +1890,13 @@ def _set_current_line(
18951890
self.special_mode = None
18961891
self.unhighlight_paren()
18971892

1898-
@property
1899-
def cursor_offset(self):
1893+
def _get_cursor_offset(self) -> int:
19001894
"""The current cursor offset from the front of the "line"."""
19011895
return self._cursor_offset
19021896

1903-
@cursor_offset.setter
1904-
def cursor_offset(self, value):
1905-
self._set_cursor_offset(value)
1906-
19071897
def _set_cursor_offset(
19081898
self,
1909-
offset,
1899+
offset: int,
19101900
update_completion=True,
19111901
reset_rl_history=False,
19121902
clear_special_mode=True,

bpython/repl.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class _FuncExpr:
392392
keyword: Optional[str] = None
393393

394394

395-
class Repl:
395+
class Repl(metaclass=abc.ABCMeta):
396396
"""Implements the necessary guff for a Python-repl-alike interface
397397
398398
The execution of the code entered and all that stuff was taken from the
@@ -425,27 +425,51 @@ class Repl:
425425
XXX Subclasses should implement echo, current_line, cw
426426
"""
427427

428-
if TYPE_CHECKING:
428+
@abc.abstractmethod
429+
def reevaluate(self):
430+
pass
429431

430-
@property
431-
@abstractmethod
432-
def current_line(self) -> str:
433-
pass
432+
@abc.abstractmethod
433+
def reprint_line(
434+
self, lineno: int, tokens: List[Tuple[_TokenType, str]]
435+
) -> None:
436+
pass
434437

435-
@property
436-
@abstractmethod
437-
def cursor_offset(self) -> int:
438-
pass
438+
@abc.abstractmethod
439+
def _get_current_line(self) -> str:
440+
pass
439441

440-
@abstractmethod
441-
def reevaluate(self):
442-
pass
442+
@abc.abstractmethod
443+
def _set_current_line(self, val: str) -> None:
444+
pass
443445

444-
@abstractmethod
445-
def reprint_line(
446-
self, lineno: int, tokens: List[Tuple[_TokenType, str]]
447-
) -> None:
448-
pass
446+
@property
447+
def current_line(self) -> str:
448+
"""The current line"""
449+
return self._get_current_line()
450+
451+
@current_line.setter
452+
def current_line(self, value: str) -> None:
453+
self._set_current_line(value)
454+
455+
@abc.abstractmethod
456+
def _get_cursor_offset(self) -> int:
457+
pass
458+
459+
@abc.abstractmethod
460+
def _set_cursor_offset(self, val: int) -> None:
461+
pass
462+
463+
@property
464+
def cursor_offset(self) -> int:
465+
"""The current cursor offset from the front of the "line"."""
466+
return self._get_cursor_offset()
467+
468+
@cursor_offset.setter
469+
def cursor_offset(self, value: int) -> None:
470+
self._set_cursor_offset(value)
471+
472+
if TYPE_CHECKING:
449473

450474
# not actually defined, subclasses must define
451475
cpos: int

bpython/test/test_repl.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import tempfile
66
import unittest
77

8+
from typing import List, Tuple
89
from itertools import islice
910
from pathlib import Path
1011
from unittest import mock
@@ -38,9 +39,32 @@ def reset(self):
3839

3940
class FakeRepl(repl.Repl):
4041
def __init__(self, conf=None):
41-
repl.Repl.__init__(self, repl.Interpreter(), setup_config(conf))
42-
self.current_line = ""
43-
self.cursor_offset = 0
42+
super().__init__(repl.Interpreter(), setup_config(conf))
43+
self._current_line = ""
44+
self._cursor_offset = 0
45+
46+
def _get_current_line(self) -> str:
47+
return self._current_line
48+
49+
def _set_current_line(self, val: str) -> None:
50+
self._current_line = val
51+
52+
def _get_cursor_offset(self) -> int:
53+
return self._cursor_offset
54+
55+
def _set_cursor_offset(self, val: int) -> None:
56+
self._cursor_offset = val
57+
58+
def getstdout(self) -> str:
59+
raise NotImplementedError
60+
61+
def reprint_line(
62+
self, lineno: int, tokens: List[Tuple[repl._TokenType, str]]
63+
) -> None:
64+
raise NotImplementedError
65+
66+
def reevaluate(self):
67+
raise NotImplementedError
4468

4569

4670
class FakeCliRepl(cli.CLIRepl, FakeRepl):

0 commit comments

Comments
 (0)