diff --git a/IPython/core/history.py b/IPython/core/history.py index 505d9aa8c90..b1216bc55c7 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -1066,6 +1066,9 @@ def extract_hist_ranges(ranges_str: str) -> Iterable[Tuple[int, int, Optional[in return for range_str in ranges_str.split(): + if re.match(r'~\d+', range_str): + yield (int(range_str.replace("~", "-")), 1, None) + continue rmatch = range_re.match(range_str) if not rmatch: continue diff --git a/IPython/core/tests/test_history.py b/IPython/core/tests/test_history.py index fa64fe04df1..14bd9ec04da 100644 --- a/IPython/core/tests/test_history.py +++ b/IPython/core/tests/test_history.py @@ -15,6 +15,7 @@ from tempfile import TemporaryDirectory # our own packages from traitlets.config.loader import Config +import pytest from IPython.core.history import HistoryAccessor, HistoryManager, extract_hist_ranges @@ -152,16 +153,21 @@ def test_history(): ip.history_manager = hist_manager_ori -def test_extract_hist_ranges(): - instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5 ~10/" - expected = [(0, 1, 2), # 0 == current session - (2, 3, 4), - (-4, 5, 7), - (-4, 7, 10), - (-9, 2, None), # None == to end - (-8, 1, None), - (-7, 1, 6), - (-10, 1, None)] + + +@pytest.mark.parametrize( + "instr, expected", + [ + ("1", [(0, 1, 2)]), + ("2/3", [(2, 3, 4)]), + ("~4/5-6", [(-4, 5, 7)]), + ("~4/7-~4/9", [(-4, 7, 10)]), + ("~9/2-~7/5", [(-9, 2, None)]), + ("~10/", [(-10, 1, None)]), + ("~4", [(-4, 1, None)]), + ], +) +def test_extract_hist_ranges(instr, expected): actual = list(extract_hist_ranges(instr)) assert actual == expected