Skip to content

TestNewFFI1.test_import_from_lib started failing with the latest 3.13 #150

Closed

Description

@stefanor

Since python/cpython#127775 landed in cPython's 3.13 branch, TestNewFFI1.test_import_from_lib has started to fail:

$ PYTHONPATH=build/lib.linux-x86_64-cpython-313/ python3.13 -m pytest testing/cffi1/test_new_ffi_1.py -k test_import_from_lib
==================================== test session starts =====================================
platform linux -- Python 3.13.1, pytest-8.3.3, pluggy-1.5.0
rootdir: /home/stefanor/git/upstream/cffi
configfile: pyproject.toml
plugins: asyncio-0.20.3, flaky-3.8.1, cov-5.0.0, mock-3.14.0, typeguard-4.4.1, anyio-4.6.2, xonsh-0.18.4, hypothesis-6.122.1
asyncio: mode=Mode.STRICT
collected 124 items / 123 deselected / 1 selected                                            

testing/cffi1/test_new_ffi_1.py F                                                      [100%]

========================================== FAILURES ==========================================
______________________________ TestNewFFI1.test_import_from_lib ______________________________

self = <testing.cffi1.test_new_ffi_1.TestNewFFI1 object at 0x7f13dee63200>

    def test_import_from_lib(self):
        ffi2 = cffi.FFI()
        ffi2.cdef("int myfunc(int); extern int myvar;\n#define MYFOO ...\n")
        outputfilename = recompile(ffi2, "_test_import_from_lib",
                                   "int myfunc(int x) { return x + 1; }\n"
                                   "int myvar = -5;\n"
                                   "#define MYFOO 42", tmpdir=str(udir))
        load_dynamic("_test_import_from_lib", outputfilename)
        from _test_import_from_lib.lib import myfunc, myvar, MYFOO
        assert MYFOO == 42
        assert myfunc(43) == 44
        assert myvar == -5     # but can't be changed, so not very useful
        with pytest.raises(ImportError):
>           from _test_import_from_lib.lib import bar
E           TypeError: bad argument type for built-in operation

testing/cffi1/test_new_ffi_1.py:1799: TypeError
----------------------------------- Captured stdout setup ------------------------------------
generating /tmp/ffi-2/test_new_ffi_1.c
setting the current directory to '/tmp/ffi-2'
------------------------------------- Captured log setup -------------------------------------
INFO     root:dist.py:970 running build_ext
INFO     root:build_ext.py:530 building 'test_new_ffi_1' extension
INFO     root:spawn.py:60 x86_64-linux-gnu-gcc -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O2 -Wall -fPIC -I/usr/include/python3.13 -c test_new_ffi_1.c -o ./test_new_ffi_1.o
INFO     root:spawn.py:60 x86_64-linux-gnu-gcc -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 ./test_new_ffi_1.o -L/usr/lib/x86_64-linux-gnu -o ./test_new_ffi_1.cpython-313-x86_64-linux-gnu.so
------------------------------------ Captured stdout call ------------------------------------
generating /tmp/ffi-2/_test_import_from_lib.c
setting the current directory to '/tmp/ffi-2'
------------------------------------- Captured log call --------------------------------------
INFO     root:dist.py:970 running build_ext
INFO     root:build_ext.py:530 building '_test_import_from_lib' extension
INFO     root:spawn.py:60 x86_64-linux-gnu-gcc -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O2 -Wall -fPIC -I/usr/include/python3.13 -c _test_import_from_lib.c -o ./_test_import_from_lib.o
INFO     root:spawn.py:60 x86_64-linux-gnu-gcc -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 ./_test_import_from_lib.o -L/usr/lib/x86_64-linux-gnu -o ./_test_import_from_lib.cpython-313-x86_64-linux-gnu.so
====================================== warnings summary ======================================
../../../../../usr/lib/python3/dist-packages/zombie_imp/__init__.py:1
  /usr/lib/python3/dist-packages/zombie_imp/__init__.py:1: DeprecationWarning: the imp module was removed in favour of importlib. Someone brought it back, but it's not a good idea to use it.
    from .imp_3_11 import *

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================== short test summary info ===================================
FAILED testing/cffi1/test_new_ffi_1.py::TestNewFFI1::test_import_from_lib - TypeError: bad argument type for built-in operation
======================== 1 failed, 123 deselected, 1 warning in 2.26s ========================

This looks like a CPython regression. But the behaviour is rather subtle.

Activity

  1. arigo commented on Dec 17, 2024

    @arigo
    Contributor

    It could be the new PyModule_GetFilenameObject() call added in the error path: in the execution of the line from _test_import_from_lib.lib import bar, the _test_import_from_lib.lib is not actually a standard module object. If that's correct, then there is nothing we can reasonably do in CFFI to prevent that bogus exception from showing up in this case. It must be fixed in CPython.

  2. stefanor commented on Dec 17, 2024

    @stefanor
    ContributorAuthor

    CPython is fixing this: python/cpython#128047

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions