python-cffi / cffi

A Foreign Function Interface package for calling C libraries from Python.
https://cffi.readthedocs.io/en/latest/
Other
114 stars 41 forks source link

Python 3.13 support TODOs #77

Closed nitzmahone closed 4 months ago

nitzmahone commented 4 months ago

Now that Python 3.13 has hit beta, we're overdue for at least a pre-release that supports it. Prerequisite to getting that out the door:

arigo commented 4 months ago

re test failures from null pointer subtraction errors: I've got a quick hack that seems to work fine with MSVC, can you check if it does the trick on clang? The hack is:

index 4167bc05..ac6c163e 100644
--- a/src/cffi/recompiler.py
+++ b/src/cffi/recompiler.py
@@ -953,7 +953,7 @@ class Recompiler:
                 if cname is None or fbitsize >= 0:
                     offset = '(size_t)-1'
                 elif named_ptr is not None:
-                    offset = '((char *)&((%s)0)->%s) - (char *)0' % (
+                    offset = '((char *)&((%s)4096)->%s) - (char *)4096' % (
                         named_ptr.name, fldname)
                 else:
                     offset = 'offsetof(%s, %s)' % (tp.get_c_name(''), fldname)

It's not handling null pointers any more... and this expression is only used in a struct initialization, not in real code.

arigo commented 4 months ago

If it doesn't help, we can try to make the clang warning not be an error for the tests (like it would likely be in production):

index a7ac14d3..8dd93c16 100644
--- a/testing/cffi1/test_verify1.py
+++ b/testing/cffi1/test_verify1.py
@@ -729,7 +729,7 @@ def test_full_enum():
     assert [lib.EE1, lib.EE2, lib.EE3] == [0, 1, 2]

 def test_enum_usage():
-    ffi = FFI()
+    ffi = FFI_warnings_not_error()
     ffi.cdef("enum ee { EE1,EE2 }; typedef struct { enum ee x; } *sp;")
     lib = ffi.verify("enum ee { EE1,EE2 }; typedef struct { enum ee x; } *sp;")
     assert lib.EE2 == 1
alan-isaac commented 4 months ago

Apologies if this proves irrelevant; I'm just a user. Currently trying to pip install (Python 3.13.0b1, Win 11) fails with errors that include: _cffi_backend.obj : error LNK2001: unresolved external symbol _PyErr_WriteUnraisableMsg

nitzmahone commented 4 months ago

@alan-isaac Yeah, that's expected from released bits- assuming your box has the right tools already installed, you should be able to do pip install https://github.com/python-cffi/cffi/archive/refs/heads/main.tar.gz to build the extension from the 3.13-compatible bits we're preparing to release.

nitzmahone commented 4 months ago

@arigo yep, changing the named pointer stuff in recompiler as you suggested to use matching non-zero offsets prevents the null pointer subtraction warnings/errors on the newer clang releases.

I've filed that as PR #78, since it's not just a test/build change.

Thanks!

ofek commented 4 months ago

For the static linking release criteria, this issue I opened a few weeks ago might provide useful context https://github.com/python-cffi/cffi/issues/75

jaraco commented 3 months ago

Are there plans to cut a release with these fixes? My system (Python 3.13b2 on macOS ARM) fails to install cffi. Building against github works around the problem.

I see now there is a pre-release, so passing --pre to pip is sufficient.

sydney-runkle commented 3 months ago

Also curious about this - working on adding support to pydantic for 3.13, and we need the fixes you've added as well :).