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

Skip to content

Commit 1b2e401

Browse files
committed
Also allows to omit the end line in history requests.
1 parent 83d6111 commit 1b2e401

3 files changed

Lines changed: 24 additions & 13 deletions

File tree

IPython/core/history.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,13 +1151,14 @@ def __del__(self) -> None:
11511151
# To match, e.g. ~5/8-~2/3, or ~4 (without trailing slash for full session)
11521152
# Session numbers: ~N or N/
11531153
# Line numbers: N (just digits, no ~ and no /)
1154+
# Range syntax: 4-6 (with end) or 4- (without end, means "onward")
11541155
range_re = re.compile(
11551156
r"""
11561157
((?P<startsess>(?:~\d+/?|\d+/)))?
11571158
(?P<start>\d+)?
11581159
((?P<sep>[\-:])
11591160
((?P<endsess>(?:~\d+/?|\d+/)))?
1160-
(?P<end>\d+))?
1161+
(?P<end>\d*))?
11611162
$""",
11621163
re.VERBOSE,
11631164
)
@@ -1175,6 +1176,10 @@ def extract_hist_ranges(ranges_str: str) -> Iterable[tuple[int, int, Optional[in
11751176
[(-8, 5, None), (-7, 1, 5), (0, 2, 3)]
11761177
>>> list(extract_hist_ranges("~4"))
11771178
[(-4, 1, None)] # Full session 4 (trailing / is optional)
1179+
>>> list(extract_hist_ranges("4-"))
1180+
[(0, 4, None)] # From line 4 onward (to end)
1181+
>>> list(extract_hist_ranges("~4/4-"))
1182+
[(-4, 4, None)] # From line 4 onward in session 4
11781183
"""
11791184
if ranges_str == "":
11801185
yield (0, 1, None) # Everything from current session
@@ -1185,23 +1190,21 @@ def extract_hist_ranges(ranges_str: str) -> Iterable[tuple[int, int, Optional[in
11851190
if not rmatch:
11861191
continue
11871192
start = rmatch.group("start")
1193+
sep = rmatch.group("sep")
11881194
if start:
11891195
start = int(start)
11901196
end = rmatch.group("end")
1191-
# If no end specified, get (a, a + 1)
1192-
end = int(end) if end else start + 1
1193-
else: # start not specified
1194-
if not rmatch.group("startsess"): # no startsess
1197+
if sep == "-":
1198+
end = (int(end) + 1) if end else None
1199+
else:
1200+
end = int(end) if end else start + 1
1201+
else:
1202+
if not rmatch.group("startsess"):
11951203
continue
11961204
start = 1
1197-
end = None # provide the entire session hist
1198-
1199-
if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3]
1200-
assert end is not None
1201-
end += 1
1205+
end = None
12021206
startsess = rmatch.group("startsess") or "0"
12031207
endsess = rmatch.group("endsess") or startsess
1204-
# Strip trailing / from session numbers (e.g., "~4/" -> "~4", "4/" -> "4")
12051208
startsess = startsess.rstrip("/")
12061209
endsess = endsess.rstrip("/")
12071210
startsess = int(startsess.replace("~", "-"))

IPython/core/magics/history.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,14 @@ def history(self, parameter_s = ''):
121121
Line 4, current session
122122
``4-6``
123123
Lines 4-6, current session
124-
``~2/`` or `~2`
125-
All lines of session 2 before current
124+
``4-``
125+
Lines 4 onward (to end), current session
126126
``243/1-5``
127127
Lines 1-5, session 243
128+
``~2/`` or `~2`
129+
All lines of session 2 before current
130+
``~4/4-``
131+
Lines 4 onward (to end), session 4 before current
128132
``~2/7``
129133
Line 7, session 2 before current
130134
``~8/1-~6/5``

tests/test_history.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ def test_extract_hist_ranges_empty_str():
225225
("~1/", [(-1, 1, None)], "single digit session with slash"),
226226
("~2", [(-2, 1, None)], "backward compatibility without slash"),
227227
("~2/", [(-2, 1, None)], "backward compatibility with slash"),
228+
("4-", [(0, 4, None)], "from line 4 onward in current session"),
229+
("~4/4-", [(-4, 4, None)], "from line 4 onward in session 4"),
230+
("2/4-", [(2, 4, None)], "from line 4 onward in session 2"),
231+
("~5/10-", [(-5, 10, None)], "from line 10 onward in session 5"),
228232
],
229233
)
230234
def test_extract_hist_ranges_without_trailing_slash(instr, expected, description):

0 commit comments

Comments
 (0)