GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-121647: Prefer decltype() for _Py_TYPEOF() on C++ by vstinner · Pull Request #157267 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Sep 10, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Using MSVC (on Windows), prefer decltype() over typeof() on C++.

  • Issue: Define _Py_TYPEOF on MSVC and other compilers supporting __typeof__ #121647

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
pythongh-121647: Prefer decltype() for _Py_TYPEOF() on C++
85a6ba9
Using MSVC (on Windows), prefer decltype() over __typeof__() on C++.
@vstinner vstinner added OS-windows skip news topic-C-API labels Sep 10, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Sep 10, 2026
Define _Py_TYPEOF on MSVC and other compilers supporting __typeof__ #121647
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Sep 10, 2026
@vstinner vstinner mentioned this pull request Sep 10, 2026
gh-121647: Define _Py_TYPEOF macro on more compilers #121648
Merged
@vstinner

vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

!buildbot AMD64 Windows11 Non-Debug 3.x PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Sep 10, 2026

Copy link
Copy Markdown

The regex 'AMD64 Windows11 Non-Debug 3.x PR' did not match any buildbot builder. Is the requested builder in the list of stable builders?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Right now, I don't access to a Windows machine to test my change, so I will rely on Windows buildbots for now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chris-eibl

chris-eibl commented Sep 10, 2026

Copy link
Copy Markdown
Member

!buildbot AMD64 Windows11 Non-Debug 3.x

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Sep 10, 2026

Copy link
Copy Markdown

The regex 'AMD64 Windows11 Non-Debug 3.x' did not match any buildbot builder. Is the requested builder in the list of stable builders?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chris-eibl

chris-eibl commented Sep 10, 2026

Copy link
Copy Markdown
Member

!buildbot AMD64 Windows11 Non-Debug

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Sep 10, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @chris-eibl for commit 85a6ba9 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F157267%2Fmerge

The command will test the builders whose names match following regular expression: AMD64 Windows11 Non-Debug

The builders matched are:

  • AMD64 Windows11 Non-Debug PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Oh, test_cppext failed on "Ubuntu / build and test (ubuntu-26.04-arm)" CI:

ERROR: test_build (test.test_cppext.TestInteralCAPI.test_build)
(...)
  /usr/bin/g++ -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fno-strict-overflow -Wsign-compare -g -Og -Wall -fPIC -I/home/runner/work/cpython/cpython-builddir/build/test_python_61202æ/tempcwd/env/include -I/home/runner/work/cpython/cpython-ro-srcdir/Include -I/home/runner/work/cpython/cpython-builddir -c extension.cpp -o build/temp.linux-aarch64-cpython-316-pydebug/extension.o -Werror -DMODULE_NAME=_testcppext_internal -DTEST_INTERNAL_C_API=1
  In file included from /home/runner/work/cpython/cpython-ro-srcdir/Include/object.h:742,
                   from /home/runner/work/cpython/cpython-ro-srcdir/Include/Python.h:79,
                   from extension.cpp:13:
  /home/runner/work/cpython/cpython-ro-srcdir/Include/internal/pycore_object.h: In function ‘void _PyObject_XSetRefDelayed(PyObject**, PyObject*)’:
Error:   /home/runner/work/cpython/cpython-ro-srcdir/Include/cpython/object.h:377:26: error: cannot declare pointer to ‘PyObject*&’ {aka ‘struct _object*&’}
    377 |         _Py_TYPEOF(dst)* _tmp_dst_ptr = &(dst); \
        |                          ^~~~~~~~~~~~
  /home/runner/work/cpython/cpython-ro-srcdir/Include/internal/pycore_object.h:757:5: note: in expansion of macro ‘Py_XSETREF’
    757 |     Py_XSETREF(*p_obj, obj);
        |     ^~~~~~~~~~
  error: command '/usr/bin/g++' failed with exit code 1

test.pythoninfo says CC.version: gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

buildbot/AMD64 Windows11 Non-Debug PR

Sadly, it still fails with the same error. I suppose that MSVC doesn't target C++11 by default.

test_build (test.test_cppext.TestInteralCAPI.test_build) ... Processing b:\uildarea\pull_request.ware-win11.nondebug\build\Lib\test\wheeldata\setuptools-79.0.1-py3-none-any.whl

  extension.cpp(299): error C3861: '__typeof__': identifier not found

Even internal__testcpp14ext which uses /std:c++14 also fails:

extension.cpp(299): error C3861: '__typeof__': identifier not found

By the way, test_limited_cpp03ext logs a compiler warning:

cl : Command line warning D9002 : ignoring unknown option '/std:c++03'

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chris-eibl

chris-eibl commented Sep 10, 2026

Copy link
Copy Markdown
Member

I suppose that MSVC doesn't target C++11 by default.

Without /Zc:__cplusplus MSVC always sets __cplusplus=199711L.

Detect whether the /std option is in effect during a C++ compilation with the _MSVC_LANG preprocessor macro. Because some existing code depends on the value of the macro __cplusplus being 199711L, the MSVC compiler doesn't change the value of this macro unless you explicitly opt in by setting /Zc:__cplusplus. Specify /Zc:__cplusplus and the /std option to set __cplusplus to the appropriate value.

Maybe we can use the _MSVC_LANG preprocessor macro, because many people do not use /Zc:__cplusplus?

cl : Command line warning D9002 : ignoring unknown option '/std:c++03'

Yupp, that's not a valid option per https://learn.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=msvc-170. It "starts" with /std:c++14.

The Microsoft C++ compiler in Visual Studio 2017 and later versions doesn't support C++ standards modes earlier than C++14 (/std:c++14). Such support isn't planned.

But again, without /Zc:__cplusplus we won't see anything else than __cplusplus=199711L and would need to query _MSVC_LANG.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Use _MSVC_LANG macro
13de0a0
@vstinner

vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

!buildbot AMD64 Windows11 Non-Debug PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Sep 10, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit 13de0a0 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F157267%2Fmerge

The command will test the builders whose names match following regular expression: AMD64 Windows11 Non-Debug PR

The builders matched are:

  • AMD64 Windows11 Non-Debug PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

But again, without /Zc:__cplusplus we won't see anything else than __cplusplus=199711L and would need to query _MSVC_LANG.

Ok, let me retry with _MSVC_LANG macro :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Fix Py_CLEAR/Py_SETREF macros for C++
ae56459
Replace "_Py_TYPEOF(dst)*" with "_Py_TYPEOF(&(dst))". In C++,
"_Py_TYPEOF(dst)*" can fail with a compiler error.
@vstinner

vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

For the Ubuntu failure, I can reproduce it locally on Fedora 44 (GCC 16.2.1) with the C program:

struct object {
    int data;
};

typedef struct object PyObject;

#define Py_CLEAR(obj) \
    do { \
        decltype(obj)* ptr = &obj; \
        *ptr = nullptr; \
    } while (0)


void func(PyObject **obj)
{
    Py_CLEAR(*obj);
}

int main()
{
    PyObject *obj = nullptr;
    func(&obj);
}

It fails to build with:

x.cpp: In function ‘void func(PyObject**)’:
x.cpp:9:24: error: cannot declare pointer to ‘PyObject*&’ {aka ‘struct object*&’}
    9 |         decltype(obj)* ptr = &obj; \
      |                        ^~~
x.cpp:16:5: note: in expansion of macro ‘Py_CLEAR’
   16 |     Py_CLEAR(*obj);
      |     ^~~~~~~~

The fix is to replace decltype(obj)* ptr with decltype(&(obj)) ptr. C++ is more strict about types, especially when using & in the type declaration.

chris-eibl reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chris-eibl

chris-eibl commented Sep 10, 2026

Copy link
Copy Markdown
Member

Since _MSVC_LANG is always set when __cplusplus is defined, I've just successfully tried

#elif defined(__cplusplus) && (__cplusplus >= 201103L ||  _MSVC_LANG >= 201103L)

locally.

Given that MSVC doesn't support anything older than /std:c++14, I'd also mark the other tests like

    @unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c++11")
    def test_build_cpp11(self):
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chris-eibl

chris-eibl commented Sep 10, 2026

Copy link
Copy Markdown
Member

!buildbot AMD64 Windows11 Non-Debug PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Sep 10, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @chris-eibl for commit ae56459 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F157267%2Fmerge

The command will test the builders whose names match following regular expression: AMD64 Windows11 Non-Debug PR

The builders matched are:

  • AMD64 Windows11 Non-Debug PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

The fix is to replace decltype(obj)* ptr with decltype(&(obj)) ptr.

I pushed a change for that.

chris-eibl reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Ah good, test_cppext passed on "Ubuntu / build and test (ubuntu-26.04-arm)" CI.

chris-eibl reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Aha, test_cppext also passed on the Windows buildbot.

chris-eibl reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Use Chris's suggestion
dec5373
@vstinner

vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

!buildbot Windows

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Sep 10, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit dec5373 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F157267%2Fmerge

The command will test the builders whose names match following regular expression: Windows

The builders matched are:

  • AMD64 Windows PGO Tailcall PR
  • AMD64 Windows11 Non-Debug PR
  • AMD64 Windows Server 2025 Refleaks PR
  • AMD64 Windows Server 2025 Clang PR
  • AMD64 Windows Server 2022 NoGIL PR
  • AMD64 Windows PGO NoGIL Tailcall PR
  • ARM64 Windows PR
  • ARM64 Windows Non-Debug PR
  • AMD64 Windows10 PR
  • AMD64 Windows PGO PR
  • AMD64 Windows PGO NoGIL PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner enabled auto-merge (squash) September 10, 2026 20:48
@vstinner
vstinner merged commit bba99f2 into python:main Sep 10, 2026
102 of 105 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner deleted the typeof_test_cppext branch September 10, 2026 21:10
@bedevere-app bedevere-app Bot removed the awaiting core review label Sep 10, 2026
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Sep 12, 2026
@vstinner @clin1234
pythongh-121647: Fix _Py_TYPEOF() on C++ (python#157267)
6defe81
Using MSVC (on Windows), _Py_TYPEOF() prefers decltype()
over __typeof__() on C++.

Fix also _Py_TYPEOF() usage in Py_CLEAR() and Py_SETREF() macros.
Replace "_Py_TYPEOF(dst)*" with "_Py_TYPEOF(&(dst))". In C++,
"_Py_TYPEOF(dst)*" can fail with a compiler error.
johnslavik pushed a commit to johnslavik/cpython that referenced this pull request Sep 14, 2026
@vstinner @johnslavik
pythongh-121647: Fix _Py_TYPEOF() on C++ (python#157267)
6c6295e
Using MSVC (on Windows), _Py_TYPEOF() prefers decltype()
over __typeof__() on C++.

Fix also _Py_TYPEOF() usage in Py_CLEAR() and Py_SETREF() macros.
Replace "_Py_TYPEOF(dst)*" with "_Py_TYPEOF(&(dst))". In C++,
"_Py_TYPEOF(dst)*" can fail with a compiler error.
@lucascolley

lucascolley commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

hello @vstinner @chris-eibl, I'm afraid I have bisected a new SEGV under ASan to this PR: gh-157649. Please see that issue for a one-line reproducer (tested on osx-arm64 so far).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lucascolley lucascolley mentioned this pull request Sep 16, 2026
Py_CLEAR/Py_SETREF() regression on the main branch with C++: SEGV with simple numpy program #157649
Closed
@StanFromIreland

StanFromIreland commented Sep 17, 2026

Copy link
Copy Markdown
Member

I'm afraid I have bisected a new SEGV under ASan to this PR: #157649. Please see that issue for a one-line reproducer (tested on osx-arm64 so far).

This seems like a false positive, you're testing with Python 3.15 which does not have this commit?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chris-eibl

chris-eibl commented Sep 17, 2026

Copy link
Copy Markdown
Member

Quoting #157649

This produces the SEGV on CPython main branch, but passes on the 3.15 branch.

So yeah, reproducible and legit on the main branch ...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 17, 2026

Copy link
Copy Markdown
Member Author

Let's continue the discussion in issue #157649.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner added a commit that referenced this pull request Oct 10, 2026
@vstinner
[3.15] gh-157649: Fix Py_CLEAR()/Py_SETREF() on C++ (#159143)
7ba9a77
* gh-121647: Fix _Py_TYPEOF() on C++ (#157267)

Using MSVC (on Windows), _Py_TYPEOF() prefers decltype()
over __typeof__() on C++.

Fix also _Py_TYPEOF() usage in Py_CLEAR() and Py_SETREF() macros.
Replace "_Py_TYPEOF(dst)*" with "_Py_TYPEOF(&(dst))". In C++,
"_Py_TYPEOF(dst)*" can fail with a compiler error.

(cherry picked from commit bba99f2)

* gh-157649: Fix Py_CLEAR()/Py_SETREF() on C++ (#158070)

On C++, do not use decltype() in _Py_TYPEOF since it produces invalid
code in Py_CLEAR() and Py_SETREF(). Instead, implement Py_CLEAR(),
Py_SETREF() and Py_XSETREF() using "auto" on C++11 and newer.

Add Py_CLEAR(), Py_SETREF() and Py_XSETREF() tests on an array.

(cherry picked from commit 3330712)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

OS-windows skip news topic-C-API

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@vstinner @bedevere-bot @chris-eibl @lucascolley @StanFromIreland
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.