From 068aa2f69b972566d7d945cc4a3124d432916d92 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 17 Jun 2020 20:27:50 +0900 Subject: [PATCH 1/4] bpo-36346: Raise DeprecationWarning when creating legacy Unicode --- Objects/unicodeobject.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 1433848c81f8e1..b9cb5da3e90f51 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -2176,8 +2176,16 @@ unicode_char(Py_UCS4 ch) PyObject * PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) { - if (u == NULL) + if (u == NULL) { + if (size > 0) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "PyUnicode_FromUnicode(NULL, size) is deprecated; " + "use PyUnicode_New() instead", 1) < 0) { + return NULL; + } + } return (PyObject*)_PyUnicode_New(size); + } if (size < 0) { PyErr_BadInternalCall(); @@ -2263,10 +2271,19 @@ PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) "Negative size passed to PyUnicode_FromStringAndSize"); return NULL; } - if (u != NULL) + if (u != NULL) { return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); - else + } + else { + if (size > 0) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "PyUnicode_FromStringAndSize(NULL, size) is deprecated; " + "use PyUnicode_New() instead", 1) < 0) { + return NULL; + } + } return (PyObject *)_PyUnicode_New(size); + } } PyObject * From 25e7a301308417cf4f89aacab30051703de49e5a Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 17 Jun 2020 20:31:15 +0900 Subject: [PATCH 2/4] Add NEWS --- Misc/NEWS.d/next/C API/2020-06-17-20-31-12.bpo-36346.mwIyxi.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/C API/2020-06-17-20-31-12.bpo-36346.mwIyxi.rst diff --git a/Misc/NEWS.d/next/C API/2020-06-17-20-31-12.bpo-36346.mwIyxi.rst b/Misc/NEWS.d/next/C API/2020-06-17-20-31-12.bpo-36346.mwIyxi.rst new file mode 100644 index 00000000000000..9b0400399beb99 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2020-06-17-20-31-12.bpo-36346.mwIyxi.rst @@ -0,0 +1,2 @@ +Raises DeprecationWarning for ``PyUnicode_FromUnicode(NULL, size)`` and +``PyUnicode_FromStringAndSize(NULL, size)`` with ``size > 0``. From 7dca9f7f6e7e8f4b908ffba50518bc7fab357df5 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 17 Jun 2020 20:33:25 +0900 Subject: [PATCH 3/4] suppress warning in test --- Lib/test/test_unicode.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 2ee4e64d635303..a0a5a0e6bee7a0 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -725,7 +725,9 @@ def test_isidentifier_legacy(self): import _testcapi u = '𝖀𝖓𝖎𝖈𝖔𝖉𝖊' self.assertTrue(u.isidentifier()) - self.assertTrue(_testcapi.unicode_legacy_string(u).isidentifier()) + with support.check_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + self.assertTrue(_testcapi.unicode_legacy_string(u).isidentifier()) def test_isprintable(self): self.assertTrue("".isprintable()) From 2df8cd23b0664f1de37c48a60227ba1036c7efab Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 17 Jun 2020 23:51:00 +0900 Subject: [PATCH 4/4] Add What's New entry --- Doc/whatsnew/3.10.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 9878f7f81cedac..737a1b9bd4f7e8 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -179,5 +179,10 @@ Porting to Python 3.10 for historical reason. It is no longer allowed. (Contributed by Victor Stinner in :issue:`40839`.) +* ``PyUnicode_FromUnicode(NULL, size)`` and ``PyUnicode_FromStringAndSize(NULL, size)`` + raise ``DeprecationWarning`` now. Use :c:func:`PyUnicode_New` to allocate + Unicode object without initial data. + (Contributed by Inada Naoki in :issue:`36346`.) + Removed -------