Skip to content

Commit 8978450

Browse files
committed
Refactor
1 parent f1a5cfb commit 8978450

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

bpython/patch_linecache.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import linecache
2-
from typing import Any, List, Tuple
2+
from typing import Any, List, Tuple, Optional
33

44

55
class BPythonLinecache(dict):
66
"""Replaces the cache dict in the standard-library linecache module,
77
to also remember (in an unerasable way) bpython console input."""
88

9-
def __init__(self, *args, **kwargs) -> None:
9+
def __init__(
10+
self,
11+
bpython_history: Optional[
12+
List[Tuple[int, None, List[str], str]]
13+
] = None,
14+
*args,
15+
**kwargs,
16+
) -> None:
1017
super().__init__(*args, **kwargs)
11-
self.bpython_history: List[Tuple[int, None, List[str], str]] = []
18+
self.bpython_history = bpython_history or []
1219

1320
def is_bpython_filename(self, fname: Any) -> bool:
14-
if isinstance(fname, str):
15-
return fname.startswith("<bpython-input-")
16-
else:
17-
# In case the key isn't a string
18-
return False
21+
return isinstance(fname, str) and fname.startswith("<bpython-input-")
1922

2023
def get_bpython_history(self, key: str) -> Tuple[int, None, List[str], str]:
2124
"""Given a filename provided by remember_bpython_input,
@@ -58,14 +61,13 @@ def _bpython_clear_linecache() -> None:
5861
if isinstance(linecache.cache, BPythonLinecache):
5962
bpython_history = linecache.cache.bpython_history
6063
else:
61-
bpython_history = []
62-
linecache.cache = BPythonLinecache()
63-
linecache.cache.bpython_history = bpython_history
64+
bpython_history = None
65+
linecache.cache = BPythonLinecache(bpython_history)
6466

6567

66-
# Monkey-patch the linecache module so that we're able
68+
# Monkey-patch the linecache module so that we are able
6769
# to hold our command history there and have it persist
68-
linecache.cache = BPythonLinecache(linecache.cache) # type: ignore
70+
linecache.cache = BPythonLinecache(None, linecache.cache) # type: ignore
6971
linecache.clearcache = _bpython_clear_linecache
7072

7173

0 commit comments

Comments
 (0)