Skip to content

Commit 275a3b8

Browse files
authored
More robust docstring parsing (#1323)
In the current (master) documentation/doctest implementation, it is assumed that all the docstrings have a "margin" of spaces, from the class indentation. Then, the regular expression looking for doctests fails to find them if there is not at least a single space at the beginning of the line. Python 3.13 removes the "left margin" of docstrings, so the regular expression will fail to detect some tests. This PR changes the regular expression to allow doctests which do not start with a space. Also, in future versions of Python, tab characters in docstrings will be converted into spaces, which will make to fail some tests. In this PR, tabs in doctests are replaced by sequences of 4 spaces, making the tests more robust under this kind of change.
1 parent b5b8857 commit 275a3b8

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

mathics/builtin/files_io/importexport.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,16 +1060,16 @@ class RegisterImport(Builtin):
10601060
>> FilePrint["ExampleData/ExampleData.txt"]
10611061
| Example File Format
10621062
| Created by Angus
1063-
| 0.629452 0.586355
1064-
| 0.711009 0.687453
1065-
| 0.246540 0.433973
1066-
| 0.926871 0.887255
1067-
| 0.825141 0.940900
1068-
| 0.847035 0.127464
1069-
| 0.054348 0.296494
1070-
| 0.838545 0.247025
1071-
| 0.838697 0.436220
1072-
| 0.309496 0.833591
1063+
| 0.629452 0.586355
1064+
| 0.711009 0.687453
1065+
| 0.246540 0.433973
1066+
| 0.926871 0.887255
1067+
| 0.825141 0.940900
1068+
| 0.847035 0.127464
1069+
| 0.054348 0.296494
1070+
| 0.838545 0.247025
1071+
| 0.838697 0.436220
1072+
| 0.309496 0.833591
10731073
10741074
>> Import["ExampleData/ExampleData.txt", {"ExampleFormat1", "Elements"}]
10751075
= {Data, Header}

mathics/doc/doc_entries.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
r"""(?mx)^ # re.MULTILINE (multi-line match)
9494
# and re.VERBOSE (readable regular expressions
9595
((?:.|\n)*?)
96-
^\s+([>#SX])>[ ](.*) # test-code indicator
96+
^\s*([>#SX])>[ ](.*) # test-code indicator
9797
((?:\n\s*(?:[:|=.][ ]|\.).*)*) # test-code results"""
9898
)
9999
TESTCASE_OUT_RE = re.compile(r"^\s*([:|=])(.*)$")
@@ -216,10 +216,7 @@ def parse_docstring_to_DocumentationEntry_items(
216216
logging.warning("``key_part`` is deprecated. Its value is discarded.")
217217

218218
# Remove commented lines.
219-
doc = filter_comments(doc).strip(r"\s")
220-
221-
# Remove leading <dl>...</dl>
222-
# doc = DL_RE.sub("", doc)
219+
doc = filter_comments(doc)
223220

224221
# pre-substitute Python code because it might contain tests
225222
doc, post_substitutions = pre_sub(
@@ -394,11 +391,16 @@ def compare_out(self, outs: tuple = tuple()) -> bool:
394391
# Mismatched number of output lines, and we don't have "..."
395392
return False
396393

394+
# Python 3.13 replaces tabs by a single space in docstrings.
395+
# In doctests we replace tabs by sequences of four spaces.
396+
def tabs_to_spaces(val):
397+
return val.text.replace("\t", 4 * " ")
398+
397399
# Need to check all output line by line
398400
for got, wanted in zip(outs, wanted_outs):
399401
if wanted.text == "...":
400402
return True
401-
if not got == wanted:
403+
if not tabs_to_spaces(got) == tabs_to_spaces(wanted):
402404
return False
403405

404406
return True

0 commit comments

Comments
 (0)