Skip to content

Commit 8ed705c

Browse files
vstinnermethane
andauthored
gh-105156: Deprecate the old Py_UNICODE type in C API (#105157)
Deprecate the old Py_UNICODE and PY_UNICODE_TYPE types in the C API: use wchar_t instead. Replace Py_UNICODE with wchar_t in multiple C files. Co-authored-by: Inada Naoki <songofacandy@gmail.com>
1 parent f332594 commit 8ed705c

File tree

9 files changed

+25
-14
lines changed

9 files changed

+25
-14
lines changed

Doc/c-api/unicode.rst

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Python:
5252
whether you selected a "narrow" or "wide" Unicode version of Python at
5353
build time.
5454

55+
.. deprecated-removed:: 3.13 3.15
56+
5557

5658
.. c:type:: PyASCIIObject
5759
PyCompactUnicodeObject

Doc/whatsnew/3.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ Porting to Python 3.13
352352
Deprecated
353353
----------
354354

355+
* Deprecate the old ``Py_UNICODE`` and ``PY_UNICODE_TYPE`` types: use directly
356+
the ``wchar_t`` type instead. Since Python 3.3, ``Py_UNICODE`` and
357+
``PY_UNICODE_TYPE`` are just aliases to ``wchar_t``.
358+
(Contributed by Victor Stinner in :gh:`105156`.)
359+
355360
Removed
356361
-------
357362

Include/cpython/unicodeobject.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
Python and represents a single Unicode element in the Unicode type.
77
With PEP 393, Py_UNICODE is deprecated and replaced with a
88
typedef to wchar_t. */
9-
#define PY_UNICODE_TYPE wchar_t
10-
/* Py_DEPRECATED(3.3) */ typedef wchar_t Py_UNICODE;
9+
Py_DEPRECATED(3.13) typedef wchar_t PY_UNICODE_TYPE;
10+
Py_DEPRECATED(3.13) typedef wchar_t Py_UNICODE;
1111

1212
/* --- Internal Unicode Operations ---------------------------------------- */
1313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Deprecate the old ``Py_UNICODE`` and ``PY_UNICODE_TYPE`` types: use directly
2+
the ``wchar_t`` type instead. Since Python 3.3, ``Py_UNICODE`` and
3+
``PY_UNICODE_TYPE`` are just aliases to ``wchar_t``. Patch by Victor
4+
Stinner.

Modules/_io/fileio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
231231
/*[clinic end generated code: output=23413f68e6484bbd input=588aac967e0ba74b]*/
232232
{
233233
#ifdef MS_WINDOWS
234-
Py_UNICODE *widename = NULL;
234+
wchar_t *widename = NULL;
235235
#else
236236
const char *name = NULL;
237237
#endif

Modules/_testcapi/getargs.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ getargs_y_hash(PyObject *self, PyObject *args)
594594
static PyObject *
595595
getargs_u(PyObject *self, PyObject *args)
596596
{
597-
Py_UNICODE *str;
597+
wchar_t *str;
598598
if (!PyArg_ParseTuple(args, "u", &str)) {
599599
return NULL;
600600
}
@@ -604,7 +604,7 @@ getargs_u(PyObject *self, PyObject *args)
604604
static PyObject *
605605
getargs_u_hash(PyObject *self, PyObject *args)
606606
{
607-
Py_UNICODE *str;
607+
wchar_t *str;
608608
Py_ssize_t size;
609609
if (!PyArg_ParseTuple(args, "u#", &str, &size)) {
610610
return NULL;
@@ -615,7 +615,7 @@ getargs_u_hash(PyObject *self, PyObject *args)
615615
static PyObject *
616616
getargs_Z(PyObject *self, PyObject *args)
617617
{
618-
Py_UNICODE *str;
618+
wchar_t *str;
619619
if (!PyArg_ParseTuple(args, "Z", &str)) {
620620
return NULL;
621621
}
@@ -628,7 +628,7 @@ getargs_Z(PyObject *self, PyObject *args)
628628
static PyObject *
629629
getargs_Z_hash(PyObject *self, PyObject *args)
630630
{
631-
Py_UNICODE *str;
631+
wchar_t *str;
632632
Py_ssize_t size;
633633
if (!PyArg_ParseTuple(args, "Z#", &str, &size)) {
634634
return NULL;

Modules/arraymodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1830,10 +1830,10 @@ typecode_to_mformat_code(char typecode)
18301830
return UNSIGNED_INT8;
18311831

18321832
case 'u':
1833-
if (sizeof(Py_UNICODE) == 2) {
1833+
if (sizeof(wchar_t) == 2) {
18341834
return UTF16_LE + is_big_endian;
18351835
}
1836-
if (sizeof(Py_UNICODE) == 4) {
1836+
if (sizeof(wchar_t) == 4) {
18371837
return UTF32_LE + is_big_endian;
18381838
}
18391839
return UNKNOWN_FORMAT;

Objects/unicodeobject.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1800,14 +1800,14 @@ PyUnicode_FromWideChar(const wchar_t *u, Py_ssize_t size)
18001800

18011801
switch (PyUnicode_KIND(unicode)) {
18021802
case PyUnicode_1BYTE_KIND:
1803-
_PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char,
1803+
_PyUnicode_CONVERT_BYTES(wchar_t, unsigned char,
18041804
u, u + size, PyUnicode_1BYTE_DATA(unicode));
18051805
break;
18061806
case PyUnicode_2BYTE_KIND:
18071807
#if Py_UNICODE_SIZE == 2
18081808
memcpy(PyUnicode_2BYTE_DATA(unicode), u, size * 2);
18091809
#else
1810-
_PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2,
1810+
_PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2,
18111811
u, u + size, PyUnicode_2BYTE_DATA(unicode));
18121812
#endif
18131813
break;
@@ -3809,9 +3809,9 @@ PyUnicode_AsUTF8(PyObject *unicode)
38093809
PyUnicode_GetSize() has been deprecated since Python 3.3
38103810
because it returned length of Py_UNICODE.
38113811
3812-
But this function is part of stable abi, because it don't
3812+
But this function is part of stable abi, because it doesn't
38133813
include Py_UNICODE in signature and it was not excluded from
3814-
stable abi in PEP 384.
3814+
stable ABI in PEP 384.
38153815
*/
38163816
PyAPI_FUNC(Py_ssize_t)
38173817
PyUnicode_GetSize(PyObject *unicode)

Python/modsupport.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ do_mkvalue(const char **p_format, va_list *p_va)
329329
case 'u':
330330
{
331331
PyObject *v;
332-
Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
332+
const wchar_t *u = va_arg(*p_va, wchar_t*);
333333
Py_ssize_t n;
334334
if (**p_format == '#') {
335335
++*p_format;

0 commit comments

Comments
 (0)