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

Update Cython to 3.0.2 #36110

Merged
merged 18 commits into from
Sep 27, 2023
Merged

Update Cython to 3.0.2 #36110

merged 18 commits into from
Sep 27, 2023

Conversation

infmagic2047
Copy link
Contributor

@infmagic2047 infmagic2047 commented Aug 21, 2023

This PR builds on #36109 and contains the rest of changes needed to update Cython to 3.0.0. The actual changes needed for building Sage are a632238 for the code, 67ad2e4 for the build system, and 7195b8f for the doctests. The rest of commits are either from #36109 or for patching dependencies.

Closes #29863.

Ideally we should wait until numpy and scipy are upgraded to 1.25 and 1.11 respectively before merging this, so we can drop the patches for them.

📝 Checklist

  • The title is concise, informative, and self-explanatory.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.

⌛ Dependencies

@tornaria
Copy link
Contributor

Can you please rebase on top of current #36109 (i.e. 4b85613)?

@tornaria
Copy link
Contributor

Thanks. On a first attempt everything is looking great.

I had this:

sage -t --random-seed=16985807869938045917183290929540248820 src/sage/graphs/connectivity.pyx
**********************************************************************
File "src/sage/graphs/connectivity.pyx", line 2817, in sage.graphs.connectivity._Component.__init__
Failed example:
    cython(os.linesep.join(cython_code))                                  # needs sage.misc.cython
Expected:
    Polygon: 2 3 4
Got:
    warning: /usr/lib/python3.11/site-packages/cysignals/signals.pxd:57:56: The keyword 'nogil' should appear at the end of the function signature line. Placing it before 'except' or 'noexcept' will be disallowed in a future version of Cython.
    Polygon: 2 3 4
**********************************************************************
File "src/sage/graphs/connectivity.pyx", line 2864, in sage.graphs.connectivity._Component.__str__
Failed example:
    cython(os.linesep.join(cython_code))                                  # needs sage.misc.cython
Expected:
    Polygon: 2 3 4
Got:
    warning: /usr/lib/python3.11/site-packages/cysignals/signals.pxd:57:56: The keyword 'nogil' should appear at the end of the function signature line. Placing it before 'except' or 'noexcept' will be disallowed in a future version of Cython.
    Polygon: 2 3 4
**********************************************************************
2 items had failures:
   1 of   3 in sage.graphs.connectivity._Component.__init__
   1 of   3 in sage.graphs.connectivity._Component.__str__
    [523 tests, 2 failures, 9.23 s]
----------------------------------------------------------------------
sage -t --random-seed=16985807869938045917183290929540248820 src/sage/graphs/connectivity.pyx  # 2 doctests failed
----------------------------------------------------------------------

Are you getting this failure?

This is just a warning coming from cysignals, which seems it could be fixed by

--- a/src/cysignals/signals.pxd.in
+++ b/src/cysignals/signals.pxd.in
@@ -54,7 +54,7 @@ cdef extern from "macros.h" nogil:
 # can be used to make Cython check whether there is a pending exception
 # (PyErr_Occurred() is non-NULL). To Cython, it will look like
 # cython_check_exception() actually raised the exception.
-cdef inline void cython_check_exception() nogil except *:
+cdef inline void cython_check_exception() except * nogil:
     pass
 
 

@@ -84,7 +84,7 @@ def xsrange(start, end=None, step=1, universe=None, *, coerce=True, bint include
EXAMPLES::

sage: xsrange(10)
<generator object at 0x...>
<_cython_3_0_0.generator object at 0x...>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
<_cython_3_0_0.generator object at 0x...>
<_cython_3_....generator object at 0x...>

or maybe just

Suggested change
<_cython_3_0_0.generator object at 0x...>
<...generator object at 0x...>

since the original one is already failing with cython 3.0.2 (which I'm testing with right now).

@tornaria
Copy link
Contributor

Except the cysignals warning, everything passes (I didn't run --long yet):

  • with cython 3.0.0
  • with cython 3.0.2 (except the issue with generators that I mentioned above)

This is looking very promising. More testing necessary. I hope #36109 is merged soon so it becomes much easier to play with this PR.

@tornaria
Copy link
Contributor

BTW, I added the patch to cysignals that I suggested above; then the related doctest failure is gone.

@tornaria
Copy link
Contributor

tornaria commented Aug 30, 2023

I ran all doctests (normal and --long) on

  • x86_64 glibc
  • x86_64 musl
  • i686 glibc

Everything passes.

Not a serious benchmark yet, but it feels like doctests are taking a bit longer (maybe ~10%, but I'm not sure as the computer might have been loaded)... I will test more carefully later.

@infmagic2047
Copy link
Contributor Author

About the cysignals failure, I got the same on my system, but I did not include the patch here since I consider it more appropriate to fix cysignals directly and publish a new version there.

@@ -10,13 +10,17 @@ def compiler_directives(profile: bool):
auto_pickle=False,
# Do not create __test__ dictionary automatically from docstrings
autotestdict=False,
binding=False,
c_api_binop_methods=True,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why binding=False?

binding=True is a present that we were expecting from cython3 for a long time. #26254

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the goal of this PR is to be able to build sagemath with cython 3. It seems enabling options to make cython 3 keep legacy behaviour is the minimal working step we can make. After sagemath is built with cython 3, it can be explored how to switch to the new behaviour. Same for legacy_implicit_noexcept, which can be removed after all the implicit noexcept are made explicit, etc.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fair enough.

@tornaria
Copy link
Contributor

tornaria commented Sep 2, 2023

Please rebase to 10.2.beta1. Now that #36109 is merged, I think this is ready for review, in fact it works very well for me, I am almost ready to give positive review. Caveats:

  • I'm only building sagelib, and I haven't tested the spkg updates, etc.
  • cython should be updated to 3.0.2 instead of 3.0.0
  • maybe cysignals should be patched... I agree it's better to fix it upstream but until there is a new release of cysignals with this fixed, this is a simple patch (and it will be easy to catch and remove when this is fixed upstream).

For some reason @infmagic2047 PR is not getting tested by CI: "This workflow requires approval from a maintainer." maybe some maintainer can approve the workflow (and add them to whatever group is necessary so they can run workflows without approval). I don't have workflow approval powers myself.

@tornaria
Copy link
Contributor

tornaria commented Sep 2, 2023

For cysignals fix I mentioned, here's the patch we are using: https://github.com/void-linux/void-packages/blob/master/srcpkgs/python3-cysignals/patches/cython3-fix-warning.patch

@infmagic2047
Copy link
Contributor Author

I rebased on develop and added the cysignals patch.

@tornaria
Copy link
Contributor

tornaria commented Sep 5, 2023

LGTM. In void-linux/void-packages#45887 I build and doctest sagemath with cython 3 on the following architectures:

  • x86_64 with glibc
  • x86_64 with musl libc
  • i686 with glibc

@orlitzky
Copy link
Contributor

So far, I have not done anything on that front. I do not have as much time as I used to. But I would appreciate some roll out of release from the various dependencies rather than patch to them. cysignals, cypari, does memory_allocator, fpylll need anything done?

Tobias says no but I've only tried it with the patched versions so far.

@kiwifb
Copy link
Member

kiwifb commented Sep 20, 2023

So far, I have not done anything on that front. I do not have as much time as I used to. But I would appreciate some roll out of release from the various dependencies rather than patch to them. cysignals, cypari, does memory_allocator, fpylll need anything done?

Tobias says no but I've only tried it with the patched versions so far.

Well, I see patches for all of them (except memory_allocator) plus pplpy that I had forgotten in the PR. Frankly all those should have new releases. Otherwise, all the packages have to patched individually. I guess I can do some release of all of them in sage-on-gentoo to test.

@kiwifb
Copy link
Member

kiwifb commented Sep 20, 2023

So, I tried to insert all that stuff in my sage-on-gentoo development tree and stable cython (3.0.0) and pythran (0.13.1). I got the following failures when cythonizing sage

Error compiling Cython file:
------------------------------------------------------------
...
"""
^
------------------------------------------------------------

sage/algebras/finite_dimensional_algebras/finite_dimensional_algebra_element.pyx:1:0: Compiler crash in DebugTransform


Compiler crash traceback from this point on:
  File "Cython/Compiler/Visitor.py", line 182, in Cython.Compiler.Visitor.TreeVisitor._visit
  File "/usr/lib/python3.11/site-packages/Cython/Compiler/ParseTreeTransforms.py", line 4033, in visit_ModuleNode
    self.visit_FuncDefNode(nested_funcdef)
  File "/usr/lib/python3.11/site-packages/Cython/Compiler/ParseTreeTransforms.py", line 4093, in visit_FuncDefNode
    self.tb.start(arg.name)
  File "/usr/lib/python3.11/site-packages/Cython/Debugger/DebugWriter.py", line 42, in start
    self.tb.start(name, attrs or {})
  File "src/lxml/saxparser.pxi", line 841, in lxml.etree.TreeBuilder.start
  File "src/lxml/saxparser.pxi", line 769, in lxml.etree.TreeBuilder._handleSaxStart
  File "src/lxml/apihelpers.pxi", line 179, in lxml.etree._makeSubElement
  File "src/lxml/apihelpers.pxi", line 1754, in lxml.etree._tagValidOrRaise
ValueError: Invalid tag name '.0'
Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/Cython/Build/Dependencies.py", line 1330, in cythonize_one_helper
    return cythonize_one(*m)
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/Cython/Build/Dependencies.py", line 1306, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: sage/algebras/finite_dimensional_algebras/finite_dimensional_algebra_element.pyx

and similar messages for quite a lot of files. I'd say I missed a patch or a dependency needs to be raised.

@tornaria
Copy link
Contributor

lxml

Use cython/cython#5690 or set SAGE_DEBUG=no.

@kiwifb
Copy link
Member

kiwifb commented Sep 20, 2023

lxml

Use cython/cython#5690 or set SAGE_DEBUG=no.

OK, SAGE_DEBUG=no let me go past that to compile. Let's see what happens next.

@kiwifb
Copy link
Member

kiwifb commented Sep 21, 2023

Well, sage-on-gentoo builds and doctest fine with the patches here for the dependencies and sage itself. I had to export SAGE_DEBUG=no as suggested by @tornaria but it otherwise did fine. I ended going for cython 3.0.2 and pythran 0.14.0. I will look to see if some of the optional dependencies I have in my tree also build.

@kiwifb
Copy link
Member

kiwifb commented Sep 21, 2023

There seems to be an issue with sagemath-meataxe

Error compiling Cython file:
------------------------------------------------------------
...
from sage.matrix.matrix_space import MatrixSpace
from sage.misc.randstate import current_randstate
from sage.misc.randstate cimport randstate
from sage.structure.element cimport Element, Matrix
from sage.structure.richcmp import rich_to_bool
from .args cimport MatrixArgs_init
^
------------------------------------------------------------

sage/matrix/matrix_gfpn_dense.pyx:58:0: 'sage/matrix/args.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
            Vector space of degree 2 and dimension 2 over Finite Field in y of size 5^3
            Basis matrix:
            [1 0]
            [0 1]
        """
        ma = MatrixArgs_init(parent, entries)
             ^
------------------------------------------------------------

sage/matrix/matrix_gfpn_dense.pyx:428:13: undeclared name not builtin: MatrixArgs_init
Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/Cython/Build/Dependencies.py", line 1330, in cythonize_one_helper
    return cythonize_one(*m)
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/Cython/Build/Dependencies.py", line 1306, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: sage/matrix/matrix_gfpn_dense.pyx

Feels like something waiting to happen.

Edit: Replacing the offending line with from sage.matrix.args cimport MatrixArgs_init is enough to fix things.

@mkoeppe
Copy link
Contributor

mkoeppe commented Sep 21, 2023

I have some fixes for such relative cimports on #35095 in dfceb98

@kiwifb
Copy link
Member

kiwifb commented Sep 21, 2023

I have some fixes for such relative cimports on #35095 in dfceb98

4253 files changed, who do you expect to review that? It cannot be reviewed by a human apart from "does it build on my systems."

@mkoeppe
Copy link
Contributor

mkoeppe commented Sep 21, 2023

The cited commit only changes 314 files...

@kiwifb
Copy link
Member

kiwifb commented Sep 21, 2023

The cited commit only changes 314 files...

Indeed, and my guilty file is in there. Well done. But that's still "only 314".

@mkoeppe
Copy link
Contributor

mkoeppe commented Sep 21, 2023

4253 files changed, who do you expect to review that? It cannot be reviewed by a human apart from "does it build on my systems."

#35095 is not ready for review (and indeed not reviewable); I am upstreaming its changes in smaller, reviewable chunks such as #36272 and #36271

@kiwifb
Copy link
Member

kiwifb commented Sep 21, 2023

The good news is that out of the optional packages I ship, this is the only that had an issue.

@mkoeppe
Copy link
Contributor

mkoeppe commented Sep 21, 2023

Then let's push this one change here on the branch.
The 313 other changes are towards #36228

@github-actions
Copy link

Documentation preview for this PR (built with commit cb78fd9; changes) is ready! 🎉

@mkoeppe
Copy link
Contributor

mkoeppe commented Sep 24, 2023

Does anyone have more concerns here, or can we merge it?

Various package upgrade PRs are waiting for it

@tornaria
Copy link
Contributor

LGTM

@dimpase
Copy link
Member

dimpase commented Sep 25, 2023

do we want to update on the latest develop branch?

@vbraun vbraun merged commit 05d7d1d into sagemath:develop Sep 27, 2023
19 of 37 checks passed
vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 1, 2023
    
<!-- ^^^^^
Please provide a concise, informative and self-explanatory title.
Don't put issue numbers in there, do this in the PR body below.
For example, instead of "Fixes sagemath#1234" use "Introduce new method to
calculate 1+1"
-->
<!-- Describe your changes here in detail -->

NumPy 1.26 provides Python 3.12 support and Cython 3 compatibility.

1.26.0 was released 2023-09-16.

As NumPy has changed its build system from setuptools-pinned-to-an-
ancient-version to meson_python, we get rid of our ancient version of
`setuptools`. The SPKGs `setuptools` and `setuptools_wheel` now ship the
same (current) version.

- [x] Check portability run:
https://github.com/mkoeppe/sage/actions/runs/5959239800
- [ ] Check SAGE_FAT_BINARY

<!-- Why is this change required? What problem does it solve? -->
<!-- If this PR resolves an open issue, please link to it here. For
example "Fixes sagemath#12345". -->
Resolves sagemath#34816
<!-- If your change requires a documentation PR, please link it
appropriately. -->

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->
<!-- If your change requires a documentation PR, please link it
appropriately -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
<!-- Feel free to remove irrelevant items. -->

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.

### ⌛ Dependencies
- Depends on sagemath#36112 (for the `meson_python` upgrades, merged here)
- Depends on sagemath#35404 (merged here as
part of above)
- Depends on sagemath#35810 (merged here as part of above)
- Depends on sagemath#36110 (merged here)
- Depends on sagemath#36263 (merged here)
- Depends on sagemath#36238 (merged here)
- Depends on sagemath#36255 (merged here)
<!-- List all open PRs that this PR logically depends on
- sagemath#12345: short description why this is a dependency
- sagemath#34567: ...
-->

<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: sagemath#36123
Reported by: Matthias Köppe
Reviewer(s):
vbraun pushed a commit that referenced this pull request Oct 8, 2023
    
<!-- ^^^^^
Please provide a concise, informative and self-explanatory title.
Don't put issue numbers in there, do this in the PR body below.
For example, instead of "Fixes #1234" use "Introduce new method to
calculate 1+1"
-->
<!-- Describe your changes here in detail -->

NumPy 1.26 provides Python 3.12 support and Cython 3 compatibility.

1.26.0 was released 2023-09-16.

As NumPy has changed its build system from setuptools-pinned-to-an-
ancient-version to meson_python, we get rid of our ancient version of
`setuptools`. The SPKGs `setuptools` and `setuptools_wheel` now ship the
same (current) version.

- [x] Check portability run:
https://github.com/mkoeppe/sage/actions/runs/5959239800
- [ ] Check SAGE_FAT_BINARY

<!-- Why is this change required? What problem does it solve? -->
<!-- If this PR resolves an open issue, please link to it here. For
example "Fixes #12345". -->
Resolves #34816
<!-- If your change requires a documentation PR, please link it
appropriately. -->

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->
<!-- If your change requires a documentation PR, please link it
appropriately -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
<!-- Feel free to remove irrelevant items. -->

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.

### ⌛ Dependencies
- Depends on #36112 (for the `meson_python` upgrades, merged here)
- Depends on #35404 (merged here as
part of above)
- Depends on #35810 (merged here as part of above)
- Depends on #36110 (merged here)
- Depends on #36263 (merged here)
- Depends on #36238 (merged here)
- Depends on #36255 (merged here)
<!-- List all open PRs that this PR logically depends on
- #12345: short description why this is a dependency
- #34567: ...
-->

<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: #36123
Reported by: Matthias Köppe
Reviewer(s):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Meta-ticket: Cython 3.0.0 (released July 2023)
9 participants