Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for compatibility with Python 3.13 #1321

Open
wants to merge 6 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
os: [macOS]
python-version: ['3.10', '3.11']
python-version: ['3.13', '3.12']
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.12', '3.11', '3.8', '3.9', '3.10']
python-version: ['3.13', '3.12', '3.11', '3.10', '3.9']
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand Down
20 changes: 10 additions & 10 deletions mathics/builtin/files_io/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,16 +1060,16 @@ class RegisterImport(Builtin):
>> FilePrint["ExampleData/ExampleData.txt"]
| Example File Format
| Created by Angus
| 0.629452 0.586355
| 0.711009 0.687453
| 0.246540 0.433973
| 0.926871 0.887255
| 0.825141 0.940900
| 0.847035 0.127464
| 0.054348 0.296494
| 0.838545 0.247025
| 0.838697 0.436220
| 0.309496 0.833591
| 0.629452 0.586355
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In docstrings, use 4 spaces instead of a tab character.

| 0.711009 0.687453
| 0.246540 0.433973
| 0.926871 0.887255
| 0.825141 0.940900
| 0.847035 0.127464
| 0.054348 0.296494
| 0.838545 0.247025
| 0.838697 0.436220
| 0.309496 0.833591

>> Import["ExampleData/ExampleData.txt", {"ExampleFormat1", "Elements"}]
= {Data, Header}
Expand Down
2 changes: 2 additions & 0 deletions mathics/core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ def get_functions(self, prefix="eval", is_pymodule=False):
for name in dir(self):
if name.startswith(prefix):
function = getattr(self, name)
if not hasattr(function, "__call__"):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, NoneType has a docstring, so we need to check that function is indeed a function.

continue
pattern = function.__doc__
if pattern is None: # Fixes PyPy bug
continue
Expand Down
14 changes: 8 additions & 6 deletions mathics/doc/doc_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
r"""(?mx)^ # re.MULTILINE (multi-line match)
# and re.VERBOSE (readable regular expressions
((?:.|\n)*?)
^\s+([>#SX])>[ ](.*) # test-code indicator
^\s*([>#SX])>[ ](.*) # test-code indicator
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Python 3.13, doctrings are trimmed of "margin" spaces.

((?:\n\s*(?:[:|=.][ ]|\.).*)*) # test-code results"""
)
TESTCASE_OUT_RE = re.compile(r"^\s*([:|=])(.*)$")
Expand Down Expand Up @@ -216,10 +216,7 @@ def parse_docstring_to_DocumentationEntry_items(
logging.warning("``key_part`` is deprecated. Its value is discarded.")

# Remove commented lines.
doc = filter_comments(doc).strip(r"\s")

# Remove leading <dl>...</dl>
# doc = DL_RE.sub("", doc)
doc = filter_comments(doc) # .strip("\s")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not remember the reason of the r"\s" strip, but it breaks some tests now.


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

# Python 3.13 replaces tabs by a single space in docstrings.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3.13 converts tab characters in docstrings to simple spaces. This "hack" helps to keep the doctests compatible with this change.

# In doctests we replace tabs by sequences of four spaces.
def tabs_to_spaces(val):
return val.text.replace("\t", 4 * " ")

# Need to check all output line by line
for got, wanted in zip(outs, wanted_outs):
if wanted.text == "...":
return True
if not got == wanted:
if not tabs_to_spaces(got) == tabs_to_spaces(wanted):
return False

return True
Expand Down