From 1b89cefbbaa1adba8ebf4483ccae2a809d41864e Mon Sep 17 00:00:00 2001 From: mattip Date: Sun, 13 Oct 2024 18:40:06 +0300 Subject: [PATCH] restore veririfer.sourcefilename, make test_parse_c_tpye tests work with xdist, touch up documentation --- .github/workflows/ci.yaml | 15 ++++++++------- doc/source/installation.rst | 6 +++--- src/cffi/verifier.py | 2 ++ testing/cffi0/test_verify.py | 2 +- testing/cffi1/test_parse_c_type.py | 24 ++++++++++++++++-------- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6dca8cb9..28da8af0 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -298,8 +298,10 @@ jobs: 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 + # 3.12+ no longer includes distutils, just always ensure setuptools is present + CIBW_TEST_REQUIRES: pytest setuptools pytest-xdist filelock + # default to test all + CIBW_TEST_COMMAND: PYTHONUNBUFFERED=1 python -m pytest ${{ matrix.test_args || '{project}' }} -n 4 run: | set -eux @@ -417,8 +419,8 @@ jobs: env: CIBW_BUILD: ${{ matrix.spec }} CIBW_PRERELEASE_PYTHONS: 'True' - CIBW_TEST_REQUIRES: pytest setuptools - CIBW_TEST_COMMAND: pip install pip --upgrade; cd {project}; PYTHONUNBUFFERED=1 pytest + CIBW_TEST_REQUIRES: pytest setuptools pytest-xdist filelock + CIBW_TEST_COMMAND: pip install pip --upgrade; cd {project}; PYTHONUNBUFFERED=1 pytest -n 4 MACOSX_DEPLOYMENT_TARGET: ${{ matrix.deployment_target || '10.9' }} SDKROOT: ${{ matrix.sdkroot || 'macosx' }} run: | @@ -508,10 +510,9 @@ jobs: env: CIBW_BUILD: ${{ matrix.spec }} CIBW_PRERELEASE_PYTHONS: 'True' - CIBW_TEST_REQUIRES: pytest setuptools + CIBW_TEST_REQUIRES: pytest setuptools pytest-xdist filelock CIBW_TEST_COMMAND: 'python -m pytest {package}/src/c' - # FIXME: /testing takes ~45min on Windows and has some failures... - # CIBW_TEST_COMMAND='python -m pytest {package}/src/c {project}/testing' + CIBW_TEST_COMMAND='python -m pytest {package}/src/c {project}/testing -n 4' run: | set -eux diff --git a/doc/source/installation.rst b/doc/source/installation.rst index 8e69b827..647a9049 100644 --- a/doc/source/installation.rst +++ b/doc/source/installation.rst @@ -42,9 +42,10 @@ Requirements: * pycparser >= 2.06: https://github.com/eliben/pycparser (automatically tracked by ``pip install cffi``). -* `pytest`_ is needed to run the tests of CFFI itself. +* `pytest`_ and `filelock` are needed to run the tests of CFFI itself. .. _`pytest`: http://pypi.python.org/pypi/pytest +.. _`filelock`: http://pypi.python.org/pypi/filelock Download and Installation: @@ -54,8 +55,7 @@ Download and Installation: ``git clone https://github.com/python-cffi/cffi`` * running the tests: ``pytest c/ testing/`` (if you didn't - install cffi yet, you need first ``python setup_base.py build_ext -f - -i``) + install cffi yet, you need first ``python -m pip install -e .``) .. _`GitHub`: https://github.com/python-cffi/cffi diff --git a/src/cffi/verifier.py b/src/cffi/verifier.py index 9b41deca..cffbe052 100644 --- a/src/cffi/verifier.py +++ b/src/cffi/verifier.py @@ -203,12 +203,14 @@ def _compile_module(self): tmpdir = os.path.dirname(self.sourcefilename) olddir = os.getcwd() os.chdir(tmpdir) + self.sourcefilename_orig = self.sourcefilename try: self.sourcefilename = os.path.relpath(self.sourcefilename) output_rel_filename = ffiplatform.compile(tmpdir, self.get_extension()) outputfilename = os.path.join(tmpdir, output_rel_filename) finally: os.chdir(olddir) + self.sourcefilename = self.sourcefilename_orig try: same = ffiplatform.samefile(outputfilename, self.modulefilename) except OSError: diff --git a/testing/cffi0/test_verify.py b/testing/cffi0/test_verify.py index 20b20cbe..570d8668 100644 --- a/testing/cffi0/test_verify.py +++ b/testing/cffi0/test_verify.py @@ -2595,7 +2595,7 @@ def test_no_regen(): from cffi.verifier import Verifier, _caller_dir_pycache import os ffi = FFI() - modulename = "_cffi_test_no_regen" + modulename = "_cffi_test_no_regen_%d" % random.randint(0, 1000000000) ffi.cdef("double sin(double x);") lib = ffi.verify('#include ', libraries=lib_m, modulename=modulename) assert lib.sin(1.23) == math.sin(1.23) diff --git a/testing/cffi1/test_parse_c_type.py b/testing/cffi1/test_parse_c_type.py index 49a31d54..f20ae352 100644 --- a/testing/cffi1/test_parse_c_type.py +++ b/testing/cffi1/test_parse_c_type.py @@ -2,6 +2,7 @@ import pytest import cffi from cffi import cffi_opcode +from filelock import FileLock from pathlib import Path if '__pypy__' in sys.builtin_module_names: @@ -26,14 +27,21 @@ ffi = cffi.FFI() ffi.cdef(header) -with open(os.path.join(cffi_dir, '..', 'c', 'parse_c_type.c')) as _body: - body = _body.read() -lib = ffi.verify( - body + """ -static const char *get_common_type(const char *search, size_t search_len) { - return NULL; -} -""", include_dirs=[cffi_dir]) + +def build_lib(): + sourcename = os.path.join(cffi_dir, '..', 'c', 'parse_c_type.c') + with FileLock(sourcename + ".lock"): + with open(sourcename) as _body: + body = _body.read() + lib = ffi.verify( + body + """ + static const char *get_common_type(const char *search, size_t search_len) { + return NULL; + } + """, include_dirs=[cffi_dir]) + return lib + +lib = build_lib() class ParseError(Exception): pass