Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75b3d1c

Browse files
committed
Get history from sql.
Fixes #13585 By getting history from sql we can get the transformed history. This also skip storing history if `%paste` is used and `%paste` itself will insert the pasted value in history which is more conveninent.
1 parent d7cc335 commit 75b3d1c

3 files changed

Lines changed: 43 additions & 17 deletions

File tree

IPython/core/interactiveshell.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,9 +3042,8 @@ def error_before_exec(value):
30423042
cell = raw_cell
30433043

30443044
# Store raw and processed history
3045-
if store_history:
3046-
self.history_manager.store_inputs(self.execution_count,
3047-
cell, raw_cell)
3045+
if store_history and raw_cell.strip(" %") != "paste":
3046+
self.history_manager.store_inputs(self.execution_count, cell, raw_cell)
30483047
if not silent:
30493048
self.logger.log(cell, raw_cell)
30503049

IPython/terminal/interactiveshell.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import asyncio
44
import os
55
import sys
6-
import warnings
76
from warnings import warn
87

98
from IPython.core.async_helpers import get_asyncio_loop
109
from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
11-
from IPython.utils import io
1210
from IPython.utils.py3compat import input
1311
from IPython.utils.terminal import toggle_set_term_title, set_term_title, restore_term_title
1412
from IPython.utils.process import abbrev_cwd
@@ -32,7 +30,7 @@
3230
from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
3331
from prompt_toolkit.filters import (HasFocus, Condition, IsDone)
3432
from prompt_toolkit.formatted_text import PygmentsTokens
35-
from prompt_toolkit.history import InMemoryHistory
33+
from prompt_toolkit.history import History
3634
from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor
3735
from prompt_toolkit.output import ColorDepth
3836
from prompt_toolkit.patch_stdout import patch_stdout
@@ -132,6 +130,43 @@ def yapf_reformat_handler(text_before_cursor):
132130
return text_before_cursor
133131

134132

133+
class PtkHistoryAdapter(History):
134+
"""
135+
Prompt toolkit has it's own way of handling history, Where it assumes it can
136+
Push/pull from history.
137+
138+
"""
139+
140+
def __init__(self, shell):
141+
super().__init__()
142+
self.shell = shell
143+
self._refresh()
144+
145+
def append_string(self, string):
146+
# we rely on sql for that.
147+
self._loaded = False
148+
self._refresh()
149+
150+
def _refresh(self):
151+
if not self._loaded:
152+
self._loaded_strings = list(self.load_history_strings())
153+
154+
def load_history_strings(self):
155+
last_cell = ""
156+
res = []
157+
for __, ___, cell in self.shell.history_manager.get_tail(
158+
self.shell.history_load_length, include_latest=True
159+
):
160+
# Ignore blank lines and consecutive duplicates
161+
cell = cell.rstrip()
162+
if cell and (cell != last_cell):
163+
res.append(cell)
164+
last_cell = cell
165+
yield from res[::-1]
166+
167+
def store_string(self, string: str) -> None:
168+
pass
169+
135170
class TerminalInteractiveShell(InteractiveShell):
136171
mime_renderers = Dict().tag(config=True)
137172

@@ -397,16 +432,9 @@ def prompt():
397432
# Set up keyboard shortcuts
398433
key_bindings = create_ipython_shortcuts(self)
399434

435+
400436
# Pre-populate history from IPython's history database
401-
history = InMemoryHistory()
402-
last_cell = u""
403-
for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
404-
include_latest=True):
405-
# Ignore blank lines and consecutive duplicates
406-
cell = cell.rstrip()
407-
if cell and (cell != last_cell):
408-
history.append_string(cell)
409-
last_cell = cell
437+
history = PtkHistoryAdapter(self)
410438

411439
self._style = self._make_style_from_name_or_cls(self.highlighting_style)
412440
self.style = DynamicStyle(lambda: self._style)
@@ -586,7 +614,6 @@ def prompt_for_code(self):
586614
def enable_win_unicode_console(self):
587615
# Since IPython 7.10 doesn't support python < 3.6 and PEP 528, Python uses the unicode APIs for the Windows
588616
# console by default, so WUC shouldn't be needed.
589-
from warnings import warn
590617
warn("`enable_win_unicode_console` is deprecated since IPython 7.10, does not do anything and will be removed in the future",
591618
DeprecationWarning,
592619
stacklevel=2)

IPython/terminal/magics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def store_or_execute(self, block, name):
5353
self.shell.user_ns['pasted_block'] = b
5454
self.shell.using_paste_magics = True
5555
try:
56-
self.shell.run_cell(b)
56+
self.shell.run_cell(b, store_history=True)
5757
finally:
5858
self.shell.using_paste_magics = False
5959

0 commit comments

Comments
 (0)