Skip to content

Commit

Permalink
Merge pull request #13657 from Carreau/fix-mix-history
Browse files Browse the repository at this point in the history
This fixed the mixing of multiple history seen in #13631
  • Loading branch information
Carreau committed Apr 29, 2022
2 parents 7943389 + dc5bcc1 commit 4d97ab1
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions IPython/core/history.py
Expand Up @@ -296,8 +296,8 @@ def _run_sql(self, sql, params, raw=True, output=False, latest=False):
toget = "history.%s, output_history.output" % toget
if latest:
toget += ", MAX(session * 128 * 1024 + line)"
cur = self.db.execute("SELECT session, line, %s FROM %s " %\
(toget, sqlfrom) + sql, params)
this_querry = "SELECT session, line, %s FROM %s " % (toget, sqlfrom) + sql
cur = self.db.execute(this_querry, params)
if latest:
cur = (row[:-1] for row in cur)
if output: # Regroup into 3-tuples, and parse JSON
Expand Down Expand Up @@ -344,6 +344,11 @@ def get_last_session_id(self):
def get_tail(self, n=10, raw=True, output=False, include_latest=False):
"""Get the last n lines from the history database.
Most recent entry last.
Completion will be reordered so that that the last ones are when
possible from current session.
Parameters
----------
n : int
Expand All @@ -362,11 +367,31 @@ def get_tail(self, n=10, raw=True, output=False, include_latest=False):
self.writeout_cache()
if not include_latest:
n += 1
cur = self._run_sql("ORDER BY session DESC, line DESC LIMIT ?",
(n,), raw=raw, output=output)
# cursor/line/entry
this_cur = list(
self._run_sql(
"WHERE session == ? ORDER BY line DESC LIMIT ? ",
(self.session_number, n),
raw=raw,
output=output,
)
)
other_cur = list(
self._run_sql(
"WHERE session != ? ORDER BY session DESC, line DESC LIMIT ?",
(self.session_number, n),
raw=raw,
output=output,
)
)

everything = this_cur + other_cur

everything = everything[:n]

if not include_latest:
return reversed(list(cur)[1:])
return reversed(list(cur))
return list(everything)[:0:-1]
return list(everything)[::-1]

@catch_corrupt_db
def search(self, pattern="*", raw=True, search_raw=True,
Expand Down

0 comments on commit 4d97ab1

Please sign in to comment.