Skip to content

Fix PyPy dependency marker - #280

Merged
mattip merged 1 commit into
python-cffi:mainfrom
doe1080:fix-pypy-marker
Oct 6, 2026
Merged

mattip merged 1 commit into
python-cffi:mainfrom
doe1080:fix-pypy-marker

Conversation

@doe1080

@doe1080 doe1080 commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

implementation_name maps to sys.implementation.name, whose value is guaranteed to be lowercase, so the marker should use pypy

https://packaging.python.org/en/latest/specifications/dependency-specifiers/#defined-environment-marker-fields
https://docs.python.org/3/library/sys.html#sys.implementation

@mattip

mattip commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

PyPy ships its own version of cffi which cannot be replaced by one from pip at all. I wonder if there is a way to error out entirely if the pyproject.toml is ever run on PyPy.

@doe1080

doe1080 commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

there doesn't seem to be a standard project-level marker for excluding PyPy since Requires-Python can only specify supported Python versions

setuptools.build_meta executes setup.py for metadata generation and build hooks, so it can explicitly reject PyPy there instead of using the current PyPy-specific no-op setup path

diff --git a/setup.py b/setup.py
index 50b487ea..151717aa 100644
--- a/setup.py
+++ b/setup.py
@@ -168,29 +168,18 @@ if forced_extra_objs:


 if __name__ == '__main__':
-    from setuptools import setup, Distribution, Extension
+    from setuptools import setup, Extension

-    class CFFIDistribution(Distribution):
-        def has_ext_modules(self):
-            # Event if we don't have extension modules (e.g. on PyPy) we want to
-            # claim that we do so that wheels get properly tagged as Python
-            # specific.  (thanks dstufft!)
-            return True
-
-    # On PyPy, cffi is preinstalled and it is not possible, at least for now,
-    # to install a different version.  We work around it by making the setup()
-    # arguments mostly empty in this case.
-    cpython = ('_cffi_backend' not in sys.builtin_module_names)
+    if '_cffi_backend' in sys.builtin_module_names:
+        raise RuntimeError(
+            'CFFI backend is built into this interpreter and cannot be replaced')

     setup(
-        packages=['cffi'] if cpython else [],
+        packages=['cffi'],
         package_dir={"": "src"},
         package_data={'cffi': ['_cffi_include.h', 'parse_c_type.h',
-                               '_embedding.h', '_cffi_errors.h']}
-                     if cpython else {},
+                               '_embedding.h', '_cffi_errors.h']},
         zip_safe=False,
-
-        distclass=CFFIDistribution,
         ext_modules=[Extension(
             name='_cffi_backend',
             include_dirs=include_dirs,
@@ -201,5 +190,5 @@ if __name__ == '__main__':
             extra_compile_args=extra_compile_args,
             extra_link_args=extra_link_args,
             extra_objects=forced_extra_objs,
-        )] if cpython else [],
+        )],
     )

@mattip

mattip commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

My point is that this PR might as well drop the PyPy-specific part of the dependency entirely, since it should never be hit.

@doe1080

doe1080 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

pypy/pypy#5306 shows why this exclusion exists. PyPy vendors pycparser inside CFFI, but bundled CFFI metadata declared it as an external dependency, causing fresh venvs to fail pip check. pypy/pypy#5307 fixed that instance by removing Requires-Dist: pycparser, but did not change how CFFI metadata is imported when PyPy updates its bundled CFFI

a later CFFI sync therefore brought upstream dependency metadata back, and current PyPy 3.11 again carries CFFI's Requires-Dist marker. fixing "PyPy" to "pypy" makes the intended PyPy exclusion correct at its source, while also preventing current PyPy syncs from reintroducing an external pycparser requirement. removing the marker instead would make that dependency unconditional

@mattip

mattip commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

OK, let's go with this. Thanks @doe1080

@mattip
mattip merged commit ffc1783 into python-cffi:main Oct 6, 2026
35 of 36 checks passed
@doe1080
doe1080 deleted the fix-pypy-marker branch October 6, 2026 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants