X Tutup
The Wayback Machine - https://web.archive.org/web/20241223195924/https://github.com/python/cpython/issues/121647
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define _Py_TYPEOF on MSVC and other compilers supporting __typeof__ #121647

Open
fuhsnn opened this issue Jul 12, 2024 · 0 comments
Open

Define _Py_TYPEOF on MSVC and other compilers supporting __typeof__ #121647

fuhsnn opened this issue Jul 12, 2024 · 0 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-windows type-feature A feature request or enhancement

Comments

@fuhsnn
Copy link

fuhsnn commented Jul 12, 2024

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:

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:

Linked PRs

@fuhsnn fuhsnn added the type-feature A feature request or enhancement label Jul 12, 2024
fuhsnn added a commit to fuhsnn/cpython that referenced this issue Jul 12, 2024
Extend the _Py_TYPEOF macro to more implementations of __typeof__ and standardized equivalents on C23 and C++11.
On MSVC, __typeof__ is enabled through _MSC_VER check; for others, an autoconf probe is implemented.
@Eclips4 Eclips4 added OS-windows interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-windows type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants
X Tutup