From 07c4edd91f350d41466787ccdc36d60dff87dd72 2021-07-08 20:07:05 From: Blazej Michalik Date: 2021-07-08 20:07:05 Subject: [PATCH] Empty histrange means history of current session When empty string is given to `IPython.core.history.extract_hist_ranges`, it will now return a range corresponding to the whole history of the current session. This means that `HistoryManager.get_range_by_str` will now also return everything if given an empty string - which has implications for other magics like `%save`, `%pastebin`, etc. Commits that follow this one will introduce that fact to each magic docstring and implementation, where required. --- diff --git a/IPython/core/history.py b/IPython/core/history.py index 08db18f..afa05fa 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -445,8 +445,11 @@ class HistoryAccessor(HistoryAccessorBase): Parameters ---------- rangestr : str - A string specifying ranges, e.g. "5 ~2/1-4". See - :func:`magic_history` for full details. + A string specifying ranges, e.g. "5 ~2/1-4". If empty string is used, + this will return everything from current session's history. + + See the documentation of :func:`%history` for the full details. + raw, output : bool As :meth:`get_range` @@ -851,11 +854,18 @@ $""", re.VERBOSE) def extract_hist_ranges(ranges_str): """Turn a string of history ranges into 3-tuples of (session, start, stop). + Empty string results in a `[(0, 1, None)]`, i.e. "everything from current + session". + Examples -------- >>> list(extract_hist_ranges("~8/5-~7/4 2")) [(-8, 5, None), (-7, 1, 5), (0, 2, 3)] """ + if ranges_str == '': + yield (0, 1, None) # Everything from current session + return + for range_str in ranges_str.split(): rmatch = range_re.match(range_str) if not rmatch: diff --git a/IPython/core/tests/test_history.py b/IPython/core/tests/test_history.py index 57266e5..6f1d820 100644 --- a/IPython/core/tests/test_history.py +++ b/IPython/core/tests/test_history.py @@ -160,6 +160,14 @@ def test_extract_hist_ranges(): actual = list(extract_hist_ranges(instr)) nt.assert_equal(actual, expected) + +def test_extract_hist_ranges_empty_str(): + instr = '' + expected = [(0, 1, None)] # 0 == current session, None == to end + actual = list(extract_hist_ranges(instr)) + nt.assert_equal(actual, expected) + + def test_magic_rerun(): """Simple test for %rerun (no args -> rerun last line)""" ip = get_ipython()