Skip to content

Commit 038dd0f

Browse files
authored
bpo-36346: Raise DeprecationWarning when creating legacy Unicode (GH-20933)
1 parent 349f76c commit 038dd0f

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

Doc/whatsnew/3.10.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ Porting to Python 3.10
213213
for historical reason. It is no longer allowed.
214214
(Contributed by Victor Stinner in :issue:`40839`.)
215215

216+
* ``PyUnicode_FromUnicode(NULL, size)`` and ``PyUnicode_FromStringAndSize(NULL, size)``
217+
raise ``DeprecationWarning`` now. Use :c:func:`PyUnicode_New` to allocate
218+
Unicode object without initial data.
219+
(Contributed by Inada Naoki in :issue:`36346`.)
220+
216221
Removed
217222
-------
218223

Lib/test/test_unicode.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,9 @@ def test_isidentifier_legacy(self):
725725
import _testcapi
726726
u = '𝖀𝖓𝖎𝖈𝖔𝖉𝖊'
727727
self.assertTrue(u.isidentifier())
728-
self.assertTrue(_testcapi.unicode_legacy_string(u).isidentifier())
728+
with support.check_warnings():
729+
warnings.simplefilter('ignore', DeprecationWarning)
730+
self.assertTrue(_testcapi.unicode_legacy_string(u).isidentifier())
729731

730732
def test_isprintable(self):
731733
self.assertTrue("".isprintable())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raises DeprecationWarning for ``PyUnicode_FromUnicode(NULL, size)`` and
2+
``PyUnicode_FromStringAndSize(NULL, size)`` with ``size > 0``.

Objects/unicodeobject.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,8 +2179,16 @@ unicode_char(Py_UCS4 ch)
21792179
PyObject *
21802180
PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
21812181
{
2182-
if (u == NULL)
2182+
if (u == NULL) {
2183+
if (size > 0) {
2184+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2185+
"PyUnicode_FromUnicode(NULL, size) is deprecated; "
2186+
"use PyUnicode_New() instead", 1) < 0) {
2187+
return NULL;
2188+
}
2189+
}
21832190
return (PyObject*)_PyUnicode_New(size);
2191+
}
21842192

21852193
if (size < 0) {
21862194
PyErr_BadInternalCall();
@@ -2266,10 +2274,19 @@ PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
22662274
"Negative size passed to PyUnicode_FromStringAndSize");
22672275
return NULL;
22682276
}
2269-
if (u != NULL)
2277+
if (u != NULL) {
22702278
return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL);
2271-
else
2279+
}
2280+
else {
2281+
if (size > 0) {
2282+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2283+
"PyUnicode_FromStringAndSize(NULL, size) is deprecated; "
2284+
"use PyUnicode_New() instead", 1) < 0) {
2285+
return NULL;
2286+
}
2287+
}
22722288
return (PyObject *)_PyUnicode_New(size);
2289+
}
22732290
}
22742291

22752292
PyObject *

0 commit comments

Comments
 (0)