Skip to content

Commit 17da7d8

Browse files
committed
gh-146175: Soft-deprecate outdated macros; convert internal usage
1 parent d5b2681 commit 17da7d8

File tree

16 files changed

+152
-47
lines changed

16 files changed

+152
-47
lines changed

Doc/c-api/intro.rst

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,14 +526,26 @@ to the C language.
526526
Outdated macros
527527
---------------
528528

529-
The following macros have been used to features that have been standardized
530-
in C11.
529+
The following :term:`soft deprecated` macros have been used to features that
530+
have been standardized in C11 (or previous standards).
531531

532532
.. c:macro:: Py_ALIGNED(num)
533533
534-
Specify alignment to *num* bytes on compilers that support it.
534+
On some GCC-like compilers, specify alignment to *num* bytes.
535+
This does nothing on other compilers.
535536

536-
Consider using the C11 standard ``_Alignas`` specifier over this macro.
537+
Use the standard ``alignas`` specifier rather than this macro.
538+
539+
.. deprecated:: next
540+
The macro is :term:`soft deprecated`.
541+
542+
.. c:macro:: PY_FORMAT_SIZE_T
543+
544+
The :c:func:`printf` formatting modifier for :c:type:`size_t`.
545+
Use ``"z"`` directly instead.
546+
547+
.. deprecated:: next
548+
The macro is :term:`soft deprecated`.
537549

538550
.. c:macro:: Py_LL(number)
539551
Py_ULL(number)
@@ -546,6 +558,38 @@ in C11.
546558
547559
Consider using the C99 standard suffixes ``LL`` and ``LLU`` directly.
548560
561+
.. deprecated:: next
562+
The macro is :term:`soft deprecated`.
563+
564+
.. c:macro:: PY_LONG_LONG
565+
PY_INT32_T
566+
PY_UINT32_T
567+
PY_INT64_T
568+
PY_UINT64_T
569+
570+
Aliases for the types :c:type:`!long long`, :c:type:`!int32_t`,
571+
:c:type:`!uint32_t`. :c:type:`!int64_t` and :c:type:`!uint64_t`,
572+
respectively.
573+
Historically, these types needed compiler-specific extensions.
574+
575+
.. deprecated:: next
576+
These macros are :term:`soft deprecated`.
577+
578+
.. c:macro:: PY_LLONG_MIN
579+
PY_LLONG_MAX
580+
PY_ULLONG_MAX
581+
PY_SIZE_MAX
582+
583+
Aliases for the values :c:macro:`!LLONG_MIN`, :c:macro:`!LLONG_MAX`,
584+
:c:macro:`!ULLONG_MAX`, and :c:macro:`!SIZE_MAX`, respectively.
585+
Use these standard names instead.
586+
587+
The required header, ``<limits.h>``,
588+
:ref:`is included <capi-system-includes>` in ``Python.h``.
589+
590+
.. deprecated:: next
591+
These macros are :term:`soft deprecated`.
592+
549593
.. c:macro:: Py_MEMCPY(dest, src, n)
550594
551595
This is a :term:`soft deprecated` alias to :c:func:`!memcpy`.
@@ -554,6 +598,25 @@ in C11.
554598
.. deprecated:: 3.14
555599
The macro is :term:`soft deprecated`.
556600
601+
.. c:macro:: Py_UNICODE_SIZE
602+
603+
Size of the :c:type:`!wchar_t` type.
604+
Use ``sizeof(wchar_t)`` or ``WCHAR_WIDTH/8`` instead.
605+
606+
The required header for the latter, ``<limits.h>``,
607+
:ref:`is included <capi-system-includes>` in ``Python.h``.
608+
609+
.. deprecated:: next
610+
The macro is :term:`soft deprecated`.
611+
612+
.. c:macro:: Py_UNICODE_WIDE
613+
614+
Defined if ``wchar_t`` can hold a Unicode character (UCS-4).
615+
Use ``sizeof(wchar_t) >= 4`` instead
616+
617+
.. deprecated:: next
618+
The macro is :term:`soft deprecated`.
619+
557620
.. c:macro:: Py_VA_COPY
558621
559622
This is a :term:`soft deprecated` alias to the C99-standard ``va_copy``
@@ -564,6 +627,9 @@ in C11.
564627
.. versionchanged:: 3.6
565628
This is now an alias to ``va_copy``.
566629
630+
.. deprecated:: next
631+
The macro is :term:`soft deprecated`.
632+
567633
568634
.. _api-objects:
569635

Doc/deprecations/c-api-pending-removal-in-3.15.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ Pending removal in Python 3.15
77
Use :c:func:`PyWeakref_GetRef` instead. The `pythoncapi-compat project
88
<https://github.com/python/pythoncapi-compat/>`__ can be used to get
99
:c:func:`PyWeakref_GetRef` on Python 3.12 and older.
10-
* :c:type:`Py_UNICODE` type and the :c:macro:`!Py_UNICODE_WIDE` macro:
11-
Use :c:type:`wchar_t` instead.
1210
* :c:func:`!PyUnicode_AsDecodedObject`:
1311
Use :c:func:`PyCodec_Decode` instead.
1412
* :c:func:`!PyUnicode_AsDecodedUnicode`:

Doc/whatsnew/3.15.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,24 @@ Deprecated C APIs
18611861
use the C11 standard ``<math.h>`` :c:macro:`!INFINITY` instead.
18621862
(Contributed by Sergey B Kirpichev in :gh:`141004`.)
18631863

1864+
* The following macros are :term:`soft deprecated`:
1865+
1866+
- :c:macro:`Py_ALIGNED`: Prefer ``alignas`` instead.
1867+
- :c:macro:`PY_FORMAT_SIZE_T`: Use ``"z"`` directly.
1868+
- :c:macro:`Py_LL` & :c:macro:`Py_ULL`:
1869+
Use standard suffixes, ``LL`` & ``ULL``.
1870+
- :c:macro:`PY_LONG_LONG`, :c:macro:`PY_LLONG_MIN`, :c:macro:`PY_LLONG_MAX`,
1871+
:c:macro:`PY_ULLONG_MAX`, :c:macro:`PY_INT32_T`, :c:macro:`PY_UINT32_T`,
1872+
:c:macro:`PY_INT64_T`, :c:macro:`PY_UINT64_T`, :c:macro:`PY_SIZE_MAX`:
1873+
Use C99 types/limits.
1874+
- :c:macro:`Py_UNICODE_SIZE`: Use ``sizeof(wchar_t)`` directly.
1875+
- :c:macro:`Py_VA_COPY`: Use ``va_copy`` directly.
1876+
1877+
The macro :c:macro:`Py_UNICODE_WIDE`, which was scheduled for removal,
1878+
is :term:`soft deprecated` instead.
1879+
1880+
(Contributed by Petr Viktorin in :gh:`146175`.)
1881+
18641882
* :c:macro:`!Py_MATH_El` and :c:macro:`!Py_MATH_PIl` are deprecated
18651883
since 3.15 and will be removed in 3.20.
18661884
(Contributed by Sergey B Kirpichev in :gh:`141004`.)

Include/internal/pycore_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n)
144144
new_refcnt = _Py_IMMORTAL_INITIAL_REFCNT;
145145
}
146146
# if SIZEOF_VOID_P > 4
147-
op->ob_refcnt = (PY_UINT32_T)new_refcnt;
147+
op->ob_refcnt = (uint32_t)new_refcnt;
148148
# else
149149
op->ob_refcnt = new_refcnt;
150150
# endif

Include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ whose size is determined when the object is allocated.
127127
struct _object {
128128
_Py_ANONYMOUS union {
129129
#if SIZEOF_VOID_P > 4
130-
PY_INT64_T ob_refcnt_full; /* This field is needed for efficient initialization with Clang on ARM */
130+
int64_t ob_refcnt_full; /* This field is needed for efficient initialization with Clang on ARM */
131131
struct {
132132
# if PY_BIG_ENDIAN
133133
uint16_t ob_flags;

Include/pyport.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ extern "C" {
446446
/*
447447
* Specify alignment on compilers that support it.
448448
*/
449-
#if defined(__GNUC__) && __GNUC__ >= 3
449+
#ifdef Py_BUILD_CORE
450+
// always use _Py_ALIGNED_DEF instead
451+
#elif defined(__GNUC__) && __GNUC__ >= 3
450452
#define Py_ALIGNED(x) __attribute__((aligned(x)))
451453
#else
452454
#define Py_ALIGNED(x)

Include/refcount.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op)
126126
return (_Py_atomic_load_uint32_relaxed(&op->ob_ref_local) ==
127127
_Py_IMMORTAL_REFCNT_LOCAL);
128128
#elif SIZEOF_VOID_P > 4
129-
return _Py_CAST(PY_INT32_T, op->ob_refcnt) < 0;
129+
return _Py_CAST(int32_t, op->ob_refcnt) < 0;
130130
#else
131131
return op->ob_refcnt >= _Py_IMMORTAL_MINIMUM_REFCNT;
132132
#endif
@@ -164,7 +164,7 @@ static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
164164
}
165165
#ifndef Py_GIL_DISABLED
166166
#if SIZEOF_VOID_P > 4
167-
ob->ob_refcnt = (PY_UINT32_T)refcnt;
167+
ob->ob_refcnt = (uint32_t)refcnt;
168168
#else
169169
ob->ob_refcnt = refcnt;
170170
#endif
@@ -278,7 +278,7 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
278278
_Py_atomic_add_ssize(&op->ob_ref_shared, (1 << _Py_REF_SHARED_SHIFT));
279279
}
280280
#elif SIZEOF_VOID_P > 4
281-
PY_UINT32_T cur_refcnt = op->ob_refcnt;
281+
uint32_t cur_refcnt = op->ob_refcnt;
282282
if (cur_refcnt >= _Py_IMMORTAL_INITIAL_REFCNT) {
283283
// the object is immortal
284284
_Py_INCREF_IMMORTAL_STAT_INC();
@@ -387,7 +387,7 @@ static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
387387
#if SIZEOF_VOID_P > 4
388388
/* If an object has been freed, it will have a negative full refcnt
389389
* If it has not it been freed, will have a very large refcnt */
390-
if (op->ob_refcnt_full <= 0 || op->ob_refcnt > (((PY_UINT32_T)-1) - (1<<20))) {
390+
if (op->ob_refcnt_full <= 0 || op->ob_refcnt > (((uint32_t)-1) - (1<<20))) {
391391
#else
392392
if (op->ob_refcnt <= 0) {
393393
#endif

Include/unicodeobject.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,9 @@ Copyright (c) Corporation for National Research Initiatives.
6464
#error Must define SIZEOF_WCHAR_T
6565
#endif
6666

67+
/* Soft-deprecated defines */
6768
#define Py_UNICODE_SIZE SIZEOF_WCHAR_T
68-
69-
/* If wchar_t can be used for UCS-4 storage, set Py_UNICODE_WIDE.
70-
Otherwise, Unicode strings are stored as UCS-2 (with limited support
71-
for UTF-16) */
72-
73-
#if Py_UNICODE_SIZE >= 4
69+
#if SIZEOF_WCHAR_T >= 4
7470
#define Py_UNICODE_WIDE
7571
#endif
7672

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The following macros are :term:`soft deprecated`:
2+
:c:macro:`Py_ALIGNED`,
3+
:c:macro:`PY_FORMAT_SIZE_T`,
4+
:c:macro:`Py_LL`, :c:macro:`Py_ULL`,
5+
:c:macro:`PY_LONG_LONG`, :c:macro:`PY_LLONG_MIN`, :c:macro:`PY_LLONG_MAX`,
6+
:c:macro:`PY_ULLONG_MAX`, :c:macro:`PY_INT32_T`, :c:macro:`PY_UINT32_T`,
7+
:c:macro:`PY_INT64_T`, :c:macro:`PY_UINT64_T`, :c:macro:`PY_SIZE_MAX`,
8+
:c:macro:`Py_UNICODE_SIZE`,
9+
:c:macro:`Py_VA_COPY`.
10+
11+
The macro :c:macro:`Py_UNICODE_WIDE`, which was scheduled for removal, is
12+
:term:`soft deprecated` instead.

Modules/_io/stringio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ resize_buffer(stringio *self, size_t size)
111111
alloc = size + 1;
112112
}
113113

114-
if (alloc > PY_SIZE_MAX / sizeof(Py_UCS4))
114+
if (alloc > SIZE_MAX / sizeof(Py_UCS4))
115115
goto overflow;
116116
new_buf = (Py_UCS4 *)PyMem_Realloc(self->buf, alloc * sizeof(Py_UCS4));
117117
if (new_buf == NULL) {

0 commit comments

Comments
 (0)