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

Hard disable Py_LIMITED_API under free-threaded build #125

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
6 changes: 5 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ jobs:
omit: ${{ env.skip_ci_redundant_jobs }}

- spec: cp313-manylinux_x86_64

- spec: cp313t-manylinux_x86_64
skip_artifact_upload: 'true'

- spec: cp38-manylinux_i686
omit: ${{ env.skip_ci_redundant_jobs }}
Expand Down Expand Up @@ -290,6 +293,7 @@ jobs:
CIBW_MUSLLINUX_I686_IMAGE: ${{ matrix.musllinux_img || 'musllinux_1_1' }}
CIBW_MUSLLINUX_AARCH64_IMAGE: ${{ matrix.musllinux_img || 'musllinux_1_1' }}
CIBW_PRERELEASE_PYTHONS: 'True'
CIBW_FREE_THREADED_SUPPORT: 'True'
CIBW_TEST_REQUIRES: pytest setuptools # 3.12+ no longer includes distutils, just always ensure setuptools is present
CIBW_TEST_COMMAND: PYTHONUNBUFFERED=1 python -m pytest ${{ matrix.test_args || '{project}' }} # default to test all
run: |
Expand All @@ -311,7 +315,7 @@ jobs:
name: ${{ steps.build.outputs.artifact_name }}
path: dist/*.whl
if-no-files-found: error
if: ${{ env.skip_artifact_upload != 'true' }}
if: ${{ (matrix.skip_artifact_upload != 'true') && (env.skip_artifact_upload != 'true') }}

make_macos_matrix:
runs-on: ubuntu-22.04
Expand Down
8 changes: 4 additions & 4 deletions src/cffi/recompiler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, sys, io
import io, os, sys, sysconfig
from . import ffiplatform, model
from .error import VerificationError
from .cffi_opcode import *
Expand All @@ -7,9 +7,9 @@
VERSION_EMBEDDED = 0x2701
VERSION_CHAR16CHAR32 = 0x2801

USE_LIMITED_API = (sys.platform != 'win32' or sys.version_info < (3, 0) or
sys.version_info >= (3, 5))

USE_LIMITED_API = ((sys.platform != 'win32' or sys.version_info < (3, 0) or
sys.version_info >= (3, 5)) and
not sysconfig.get_config_var("Py_GIL_DISABLED")) # free-threaded doesn't yet support limited API

class GlobalExpr:
def __init__(self, name, address, type_op, size=0, check_value=0):
Expand Down
8 changes: 8 additions & 0 deletions src/cffi/setuptools_ext.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import sysconfig

try:
basestring
Expand Down Expand Up @@ -87,6 +88,7 @@ def _set_py_limited_api(Extension, kwds):
Recently (2020) we started shipping only >= 3.5 wheels, though. So
we'll give it another try and set py_limited_api on Windows >= 3.5.
"""
from cffi._shimmed_dist_utils import log
from cffi import recompiler

if ('py_limited_api' not in kwds and not hasattr(sys, 'gettotalrefcount')
Expand All @@ -102,6 +104,12 @@ def _set_py_limited_api(Extension, kwds):
# warning.
kwds['py_limited_api'] = True

if sysconfig.get_config_var("Py_GIL_DISABLED"):
if kwds.get('py_limited_api'):
log.info("Ignoring py_limited_api=True for free-threaded build.")

kwds['py_limited_api'] = False

if kwds.get('py_limited_api') is False:
# avoid setting Py_LIMITED_API if py_limited_api=False
# which _cffi_include.h does unless _CFFI_NO_LIMITED_API is defined
Expand Down
4 changes: 3 additions & 1 deletion testing/cffi0/test_zintegration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import py, os, sys, shutil
import subprocess
import sysconfig
import textwrap
from testing.udir import udir
import pytest
Expand Down Expand Up @@ -179,7 +180,8 @@ def test_set_py_limited_api(self):
except ImportError as e:
pytest.skip(str(e))
orig_version = setuptools.__version__
expecting_limited_api = not hasattr(sys, 'gettotalrefcount')
# free-threaded Python does not yet support limited API
expecting_limited_api = not hasattr(sys, 'gettotalrefcount') and not sysconfig.get_config_var("Py_GIL_DISABLED")
try:
setuptools.__version__ = '26.0.0'
from setuptools import Extension
Expand Down
7 changes: 4 additions & 3 deletions testing/cffi1/test_ffi_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ def test_ffi_cache_type():
def test_ffi_type_not_immortal():
import weakref, gc
ffi = _cffi1_backend.FFI()
t1 = ffi.typeof("int **")
t2 = ffi.typeof("int *")
# this test can fail on free-threaded builds lazier GC if the type was used by another test
t1 = ffi.typeof("unsigned short int **")
t2 = ffi.typeof("unsigned short int *")
w1 = weakref.ref(t1)
w2 = weakref.ref(t2)
del t1, ffi
gc.collect()
assert w1() is None
assert w2() is t2
ffi = _cffi1_backend.FFI()
assert ffi.typeof(ffi.new("int **")[0]) is t2
assert ffi.typeof(ffi.new("unsigned short int **")[0]) is t2
#
ffi = _cffi1_backend.FFI()
t1 = ffi.typeof("int ***")
Expand Down
Loading