Skip to content

Commit

Permalink
bpo-36346: Raise DeprecationWarning when creating legacy Unicode (pyt…
Browse files Browse the repository at this point in the history
  • Loading branch information
methane authored and arun-mani-j committed Jul 21, 2020
1 parent ad54630 commit ac78c82
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ 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
-------

Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raises DeprecationWarning for ``PyUnicode_FromUnicode(NULL, size)`` and
``PyUnicode_FromStringAndSize(NULL, size)`` with ``size > 0``.
23 changes: 20 additions & 3 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2179,8 +2179,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();
Expand Down Expand Up @@ -2266,10 +2274,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 *
Expand Down

0 comments on commit ac78c82

Please sign in to comment.