Skip to content

Regression in 3.13.1 for imports of nonexistant names from CFFI modules #128030

Closed

Description

@stefanor

Bug report

Bug description:

cffi's TestNewFFI1.test_import_from_lib started failing with:

>           from _test_import_from_lib.lib import bar
E           TypeError: bad argument type for built-in operation

This is where _test_import_from_lib is a CFFI C extension, which does not contain a member lib.bar. So in this case _test_import_from_lib.lib probably doesn't have an origin.

More details here: python-cffi/cffi#150

The offending change seems to be the introduction of PyModule_GetFilenameObject in #127775. (@hauntsaninja, @serhiy-storchaka)

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    3.13only security fixes
    3.14bugs and security fixes
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Dec 17, 2024
  2. stefanor commented on Dec 17, 2024

    @stefanor
    ContributorAuthor
    (gdb) run
    Starting program: /home/stefanor/git/upstream/cffi/ve/bin/python -m pytest testing/cffi1/test_new_ffi_1.py -k test_import_from_lib
    
    This GDB supports auto-downloading debuginfo from the following URLs:
      <https://debuginfod.debian.net>
    Enable debuginfod for this session? (y or [n]) y
    Debuginfod has been enabled.
    To make this setting permanent, add 'set debuginfod enabled on' to .gdbinit.
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    
    Breakpoint 1.1, PyModule_GetFilenameObject (mod=mod@entry=<module at remote 0x7ffff7c98090>)
        at ./Include/object.h:336
    336	    return ob->ob_type;
    (gdb) cont
    Continuing.
    ==================================== test session starts =====================================
    platform linux -- Python 3.13.1+, pytest-8.3.4, pluggy-1.5.0
    rootdir: /home/stefanor/git/upstream/cffi
    configfile: pyproject.toml
    collected 124 items / 123 deselected / 1 selected                                            
    
    testing/cffi1/test_new_ffi_1.py [Detaching after vfork from child process 3670695]
    [Detaching after vfork from child process 3670698]
    [Detaching after vfork from child process 3670701]
    [Detaching after vfork from child process 3670704]
    
    Breakpoint 1.1, PyModule_GetFilenameObject (
        mod=mod@entry=<_cffi_backend.Lib at remote 0x7ffff6ccc6d0>) at ./Include/object.h:336
    336	    return ob->ob_type;
    (gdb) s
    604	    if (!PyModule_Check(mod)) {
    (gdb) 
    0x00005555556a28b4 in PyObject_TypeCheck (
        ob=ob@entry=<_cffi_backend.Lib at remote 0x7ffff6ccc6d0>, 
        type=0x555555a7e040 <PyModule_Type>) at ./Include/object.h:542
    542	    return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
    (gdb) p ob
    $1 = <_cffi_backend.Lib at remote 0x7ffff6ccc6d0>
    (gdb) p type
    $2 = (PyTypeObject *) 0x555555a7e040 <PyModule_Type>
    (gdb) s
    604	    if (!PyModule_Check(mod)) {
    (gdb) 
    PyObject_TypeCheck (ob=ob@entry=<_cffi_backend.Lib at remote 0x7ffff6ccc6d0>, 
        type=0x555555a7e040 <PyModule_Type>) at ./Include/object.h:542
    542	    return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
    (gdb) 
    Py_IS_TYPE (ob=ob@entry=<_cffi_backend.Lib at remote 0x7ffff6ccc6d0>, 
        type=0x555555a7e040 <PyModule_Type>) at ./Include/object.h:369
    369	    return Py_TYPE(ob) == type;
    (gdb) 
    0x00005555556a28bf in Py_TYPE (ob=ob@entry=<_cffi_backend.Lib at remote 0x7ffff6ccc6d0>)
        at ./Include/object.h:336
    336	    return ob->ob_type;
    (gdb) 
    PyType_IsSubtype (a=0x7ffff71bbc60 <Lib_Type>, b=b@entry=0x555555a7e040 <PyModule_Type>)
        at Objects/typeobject.c:2479
    2479	    return is_subtype_with_mro(a->tp_mro, a, b);
    (gdb) p a->tp_mro
    $3 = (<type at remote 0x7ffff71bbc60>, <type at remote 0x555555a83f60>)
    (gdb) s
    is_subtype_with_mro (
        a_mro=(<type at remote 0x7ffff71bbc60>, <type at remote 0x555555a83f60>), 
        a=0x7ffff71bbc60 <Lib_Type>, b=0x555555a7e040 <PyModule_Type>)
        at Objects/typeobject.c:2455
    2455	    if (a_mro != NULL) {
    (gdb) 
    2460	        n = PyTuple_GET_SIZE(a_mro);
    (gdb) s
    PyTuple_GET_SIZE (op=(<type at remote 0x7ffff71bbc60>, <type at remote 0x555555a83f60>))
        at ./Include/cpython/tupleobject.h:23
    23	    return Py_SIZE(tuple);
    (gdb) s
    Py_SIZE (ob=(<type at remote 0x7ffff71bbc60>, <type at remote 0x555555a83f60>))
        at ./Include/object.h:349
    349	    return  _PyVarObject_CAST(ob)->ob_size;
    (gdb) p _PyVarObject_CAST(ob)->ob_size
    No symbol "_PyVarObject_CAST" in current context.
    (gdb) s
    is_subtype_with_mro (
        a_mro=(<type at remote 0x7ffff71bbc60>, <type at remote 0x555555a83f60>), 
        a=0x7ffff71bbc60 <Lib_Type>, b=0x555555a7e040 <PyModule_Type>)
        at Objects/typeobject.c:2462
    2462	        for (i = 0; i < n; i++) {
    (gdb) p n
    $4 = 2
    (gdb) s
    2463	            if (PyTuple_GET_ITEM(a_mro, i) == (PyObject *)b) {
    (gdb) s
    2463	            if (PyTuple_GET_ITEM(a_mro, i) == (PyObject *)b) {
    (gdb) s
    PyModule_GetFilenameObject (mod=mod@entry=<_cffi_backend.Lib at remote 0x7ffff6ccc6d0>)
        at Objects/moduleobject.c:605
    605	        PyErr_BadArgument();

    Here's the relevant type: https://github.com/python-cffi/cffi/blob/main/src/c/lib_obj.c#L591-L627

  3. added a commit that references this issue on Dec 17, 2024
  4. hauntsaninja commented on Dec 17, 2024

    @hauntsaninja
    Contributor

    Thanks for tagging me! I've opened #128047 , I will confirm that that fixes the CFFI test case.

  5. added a commit that references this issue on Dec 20, 2024
  6. added a commit that references this issue on Dec 20, 2024
  7. hauntsaninja commented on Dec 20, 2024

    @hauntsaninja
    Contributor

    Thanks for the timely report. Fix is backported to 3.13 and I confirmed it makes cffi's test_import_from_lib pass

  8. added a commit that references this issue on Dec 20, 2024
  9. added a commit that references this issue on Dec 23, 2024
  10. added a commit that references this issue on Jan 8, 2025
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

    3.13only security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions