Skip to content

gh-160208: Mark PyObject inner struct as anonymous - #160214

Closed
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:pyobject_anonymous
Closed

vstinner wants to merge 1 commit into
python:mainfrom
vstinner:pyobject_anonymous

Conversation

@vstinner

@vstinner vstinner commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Add __extension__ on the PyObject inner struct to fix the g++ warning:

Include/object.h:131:16: error: ISO C++ prohibits anonymous
structs [-Wpedantic]
131 |         struct {

Add __extension__ on the PyObject inner struct to fix the g++
warning:

    Include/object.h:131:16: error: ISO C++ prohibits anonymous
    structs [-Wpedantic]
    131 |         struct {
@vstinner

Copy link
Copy Markdown
Member Author

Tests / Windows (free-threading) / Build and test (x64, switch-case) (pull_request): Failing after 35m

It's likely a flaky test:

FAIL: test_release_task_refs (test.test_multiprocessing_spawn.test_processes.WithProcessesTestPool.test_release_task_refs)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\a\cpython\cpython\Lib\test\_test_multiprocessing.py", line 3443, in test_release_task_refs
    self.assertEqual(CountedObject.n_instances, 0)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 4 != 0

@vstinner vstinner added the needs backport to 3.15 bugs and security fixes label Oct 22, 2026
@vstinner

Copy link
Copy Markdown
Member Author

The change should be backported manually to 3.14 which doesn't have _Py_ANONYMOUS macro. Maybe this macro should be backported to 3.14 as well.

@vstinner

Copy link
Copy Markdown
Member Author

@mtelka: Would you be able to test this fix?

@mtelka

mtelka commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@mtelka: Would you be able to test this fix?

  building '_test_cppext' extension
  creating build/temp.solaris-2.11-i86pc.64bit-cpython-315
  /usr/gcc/14/bin/g++ -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O3 -Wall -m64 -O3 -g -gdwarf-4 -gstrict-dwarf -msave-args -fno-reorder-functions -fno-reorder-blocks-and-partition -D_XOPEN_SOURCE=600 -D__EXTENSIONS__=1 -D_XPG6 -D_REENTRANT -fPIC -I/data/builds/oi-py315/components/python/python-315/build/amd64/build/test_python_199041æ/tempcwd/env/include -I/data/builds/oi-py315/components/python/python-315/Python-3.15.1/Include -I/data/builds/oi-py315/components/python/python-315/build/amd64 -c extension.cpp -o build/temp.solaris-2.11-i86pc.64bit-cpython-315/extension.o -Wall -Wextra -Werror -DMODULE_NAME=_test_cppext -pedantic-errors -Wno-long-long
  In file included from /data/builds/oi-py315/components/python/python-315/Python-3.15.1/Include/Python.h:79,
                   from extension.c:16,
                   from extension.cpp:3:
  /data/builds/oi-py315/components/python/python-315/Python-3.15.1/Include/object.h:131:30: error: ISO C++ prohibits anonymous structs [-Wpedantic]
    131 |         _Py_ANONYMOUS struct {
        |                              ^
  error: command '/usr/gcc/14/bin/g++' failed with exit code 1
  error: subprocess-exited-with-error

  × Building wheel for internal__test_cppext (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> No available output.

  note: This error originates from a subprocess, and is likely not a problem with pip.

@mtelka

mtelka commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Looking at the _Py_ANONYMOUS macro I think it is obvious that this is not the way to go.

cpython/Include/pymacro.h

Lines 90 to 108 in 784b305

// _Py_ANONYMOUS: modifier for declaring an anonymous union.
// Usage: _Py_ANONYMOUS union { ... };
// Standards/compiler support:
// - C++ allows anonymous unions, but not structs
// - C11 and above allows anonymous unions and structs
// - MSVC has warning(disable: 4201) "nonstandard extension used : nameless
// struct/union". This is specific enough that we disable it for all of
// Python.h.
// - GCC & clang needs __extension__ before C11
// To allow unsupported platforms which need other spellings, we use a
// predefined value of _Py_ANONYMOUS if it exists.
#ifndef _Py_ANONYMOUS
# if (defined(__GNUC__) || defined(__clang__)) \
&& !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
# define _Py_ANONYMOUS __extension__
# else
# define _Py_ANONYMOUS
# endif
#endif

@vstinner

Copy link
Copy Markdown
Member Author

Ok, so this change doesn't work. I wrote a different fix: #160332.

@vstinner vstinner closed this Oct 22, 2026
@vstinner
vstinner deleted the pyobject_anonymous branch October 22, 2026 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants