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 — Define `_Py_TYPEOF` on MSVC and other compilers supporting `__typeof__` · Issue #121647 · python/cpython

Define _Py_TYPEOF on MSVC and other compilers supporting __typeof__ #121647

New issue
Copy link
New issue
Copy link
Closed
Closed
Define _Py_TYPEOF on MSVC and other compilers supporting __typeof__#121647
Copy link
Labels
OS-windowsinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Description

@fuhsnn
fuhsnn
opened on Jul 12, 2024
Issue body actions

Feature or enhancement

Proposal:

Recently, MSVC implemented __typeof__, which currently is defined as _Py_TYPEOF only on GCC and Clang.

cpython/Include/pyport.h

Lines 553 to 560 in 65feded

// _Py_TYPEOF(expr) gets the type of an expression.
//
// Example: _Py_TYPEOF(x) x_copy = (x);
//
// The macro is only defined if GCC or clang compiler is used.
#if defined(__GNUC__) || defined(__clang__)
# define _Py_TYPEOF(expr) __typeof__(expr)
#endif

The only users of _Py_TYPEOF are macros Py_CLEAR(), Py_SETREF() and Py_XSETREF().
If _Py_TYPEOF is not defined, these macros fall back to slightly uglier implementations with memcpy calls.

cpython/Include/refcount.h

Lines 421 to 442 in 65feded

#ifdef _Py_TYPEOF
#define Py_CLEAR(op) \
do { \
_Py_TYPEOF(op)* _tmp_op_ptr = &(op); \
_Py_TYPEOF(op) _tmp_old_op = (*_tmp_op_ptr); \
if (_tmp_old_op != NULL) { \
*_tmp_op_ptr = _Py_NULL; \
Py_DECREF(_tmp_old_op); \
} \
} while (0)
#else
#define Py_CLEAR(op) \
do { \
PyObject **_tmp_op_ptr = _Py_CAST(PyObject**, &(op)); \
PyObject *_tmp_old_op = (*_tmp_op_ptr); \
if (_tmp_old_op != NULL) { \
PyObject *_null_ptr = _Py_NULL; \
memcpy(_tmp_op_ptr, &_null_ptr, sizeof(PyObject*)); \
Py_DECREF(_tmp_old_op); \
} \
} while (0)
#endif

The proposal is to define _Py_TYPEOF on:

  • latest MSVC versions that support __typeof__
  • arbitrary compilers that support __typeof__
  • arbitrary compilers that support C23 typeof or C++11 decltype

By defining _Py_TYPEOF, uses of Py_CLEAR(), Py_SETREF() and Py_XSETREF() are switched to the _Py_TYPEOF implementation; on these compilers, the benefits would be:

  • Less worrying about miscompilation and machine code quality.
  • Fewer surprises with hidden memcpy as seen in
    • gh-98724: Fix the Py_CLEAR() macro in the limited C API #100121
    • memcpy() usage without #include <string.h> in pwdmodule.c #121617
  • Reduced variance between optimization settings thanks to more straightforward implementation.

Additionally, this removes a differing detail between MSVC and GCC/Clang builds, which should be generally desirable.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

  • Macro Py_CLEAR references argument two times. #98724

Linked PRs

  • gh-121647: Define _Py_TYPEOF macro on more compilers #121648
  • gh-121647: Prefer decltype() for _Py_TYPEOF() on C++ #157267

Activity

  1. fuhsnn
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jul 12, 2024
  2. fuhsnn
    added a commit that references this issue on Jul 12, 2024

    pythongh-121647: Define _Py_TYPEOF macro on more compilers

    85e5cbd
  3. Eclips4
    added
    OS-windows
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Jul 12, 2024
  4. sergey-miryanov commented on Feb 27, 2025

    @sergey-miryanov
    sergey-miryanov
    on Feb 27, 2025
    Contributor
    More actions

    It seems to have been added

  5. fuhsnn
    added a commit that references this issue on Sep 10, 2026

    pythongh-121647: Define _Py_TYPEOF macro on more compilers

    8d78fea
  6. vstinner
    added a commit that references this issue on Sep 10, 2026

    gh-121647: Define _Py_TYPEOF macro on more compilers (#121648)

    05b6061
  7. vstinner
    closed this as completedon Sep 10, 2026
  8. vstinner
    added a commit that references this issue on Sep 10, 2026

    gh-121647: Fix _Py_TYPEOF() on C++ (#157267)

    bba99f2
  9. clin1234
    added 2 commits that reference this issue on Sep 12, 2026

    pythongh-121647: Define _Py_TYPEOF macro on more compilers (python#12…

    d21c9c1

    pythongh-121647: Fix _Py_TYPEOF() on C++ (python#157267)

    6defe81
  10. johnslavik
    added 2 commits that reference this issue on Sep 14, 2026

    pythongh-121647: Define _Py_TYPEOF macro on more compilers (python#12…

    7c2cf14

    pythongh-121647: Fix _Py_TYPEOF() on C++ (python#157267)

    6c6295e
  11. vstinner
    added a commit that references this issue on Oct 10, 2026

    [3.15] gh-157649: Fix Py_CLEAR()/Py_SETREF() on C++ (#159143)

    7ba9a77
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

    OS-windowsinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.