Skip to content

Conversation

@cxzhong
Copy link
Contributor

@cxzhong cxzhong commented Oct 20, 2025

Fix all doctests exceptions raise changes in python 3.14.
1.Just for lazy_list, The from itertools import count is not picklable. It seems a new change in python 3.14.
2.In python 3.14, the error of PicklingError changes a lot. I have to use try.. except to detect it
Now fix #41028 with cython 3.1.5

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

… use ellipsis pattern

Python 3.14 changed the format of TypeError messages for unhashable types from:
  TypeError: unhashable type: 'list'
to:
  TypeError: cannot use 'list' as a dict key (unhashable type: 'list')

Updated all doctests to use ellipsis pattern (e.g., 'TypeError: ...unhashable...')
to match both old and new error message formats.

Modified 25 files across multiple modules including:
- Documentation (tutorial-programming-python.rst)
- Graph modules (graph.py, generic_graph.py, c_graph.pyx, bipartite_graph.py)
- Combinatorics (diagram_algebras.py, finite_state_machine.py)
- Sets (disjoint_set.pyx, set.py)
- Geometry (polyhedron modules)
- Structure (sage_object.pyx, unique_representation.py)
- Matrix (matrix0.pyx)
- Misc utilities (weak_dict.pyx, cachefunc.pyx)
- P-adic rings (qadic_flint_*.pyx, padic_ZZ_pX_CR_element.pyx)
- Polynomials (multi_polynomial.pyx, polynomial_element.pyx)
- Modules (indexed_element.pyx, tutorial_free_modules.py)
- Topology (simplicial_complex.py)
@cxzhong cxzhong marked this pull request as ready for review October 20, 2025 09:15
@cxzhong cxzhong requested a review from dimpase October 20, 2025 09:16
@github-actions
Copy link

github-actions bot commented Oct 20, 2025

Documentation preview for this PR (built with commit 32899be; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

Python 3.14 changed several error message formats:
- ZeroDivisionError: 'float division' -> '...division'
- ValueError list.index: '5 is not in list' -> '...not in list'
- TypeError pickle generator: Added exception chaining, use try-except

Files fixed:
- src/sage/plot/colors.py: ZeroDivisionError message
- src/sage/structure/list_clone.pyx: ValueError message
- src/sage/combinat/words/word.py: TypeError with pickle (4 occurrences)
Python 3.14 changed the format of PicklingError messages. Use try-except
blocks instead of matching exact error output for more robust testing.

Files fixed:
- src/sage/structure/sage_object.pyx: _test_pickling()
- src/sage/sets/set_from_iterator.py: DummyExampleForPicklingTest
- src/sage/misc/sage_unittest.py: TestSuite Blah example class
Instead of using try-except to catch unpicklable iterators like
itertools.count (which can't be pickled in Python 3.14), changed
the doctests to use list iterators which ARE picklable.

This properly tests that the __reduce__ method works correctly
while being compatible with Python 3.14's removal of pickle
support for itertools objects.
@cxzhong cxzhong force-pushed the fix-unhashable-doctests-develop branch from bd24622 to 32899be Compare October 20, 2025 10:44
@cxzhong cxzhong changed the title Fix doctests for Python 3.14: Update unhashable TypeError messages to… Fix doctests for Python 3.14 Oct 20, 2025
@cxzhong
Copy link
Contributor Author

cxzhong commented Oct 20, 2025

sage: from itertools import count
sage: from sage.misc.lazy_list import lazy_list
sage: m = lazy_list(count())
sage: x = loads(dumps(m))
/home/zhongcx/sage/local/var/lib/sage/venv-python3.14/lib/python3.14/site-packages/IPython/core/interactiveshell.py:3112: SyntaxWarning: 'return' in a 'finally' block
  return result
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File sage/misc/persist.pyx:338, in sage.misc.persist.dumps()

AttributeError: type object 'sage.misc.lazy_list.lazy_list_from_iterator' has no attribute 'dumps'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 x = loads(dumps(m))

File sage/misc/persist.pyx:340, in sage.misc.persist.dumps()

File sage/misc/persist.pyx:306, in sage.misc.persist._base_dumps()

File sage/misc/persist.pyx:830, in sage.misc.persist.SagePickler.dumps()

TypeError: cannot pickle 'itertools.count' object
when serializing tuple item 0
when serializing sage.misc.lazy_list.lazy_list_from_iterator reconstructor arguments
when serializing sage.misc.lazy_list.lazy_list_from_iterator object

Seems in python 3.14 we can not pickle itertools.count
I tested in python 3.13.7

>>> import pickle
>>> from itertools import count
>>> c = count(10, 2)
>>> pickle.dumps(c)
<python-input-3>:1: DeprecationWarning: Pickle, copy, and deepcopy support will be removed from itertools in Python 3.14.
b'\x80\x04\x95\x1f\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x94\x8c\x05count\x94\x93\x94K\nK\x02\x86\x94R\x94.'

in python 3.14

Python 3.14.0 (main, Oct 14 2025, 21:27:55) [Clang 20.1.4 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>  import pickle
  File "<python-input-0>", line 1
    import pickle
IndentationError: unexpected indent
>>> import pickle
>>> from itertools import count
>>> c = count(10, 2)
>>> pickle.dumps(c)
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    pickle.dumps(c)
    ~~~~~~~~~~~~^^^
TypeError: cannot pickle 'itertools.count' object

So we have to change the doctest in lazy_list.
Relate to python/cpython#101588

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some doctests fails in python3.14.0

1 participant