]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36346: Raise DeprecationWarning when creating legacy Unicode (GH-20933)
authorInada Naoki <songofacandy@gmail.com>
Tue, 30 Jun 2020 06:26:56 +0000 (15:26 +0900)
committerGitHub <noreply@github.com>
Tue, 30 Jun 2020 06:26:56 +0000 (15:26 +0900)
Doc/whatsnew/3.10.rst
Lib/test/test_unicode.py
Misc/NEWS.d/next/C API/2020-06-17-20-31-12.bpo-36346.mwIyxi.rst [new file with mode: 0644]
Objects/unicodeobject.c

index 0674ce8cff17763832b5406552b869b381bba7c5..a3b53ba48e9b7c8dcfe305c01d8b509bd2be3b1c 100644 (file)
@@ -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
 -------
 
index 6e397161fd98d9186b22ab17cbefe52a033ed81f..59697935fe5cd841c42a2f27178bdaf41d5a1392 100644 (file)
@@ -725,7 +725,9 @@ class UnicodeTest(string_tests.CommonTest,
         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())
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 (file)
index 0000000..9b04003
--- /dev/null
@@ -0,0 +1,2 @@
+Raises DeprecationWarning for ``PyUnicode_FromUnicode(NULL, size)`` and
+``PyUnicode_FromStringAndSize(NULL, size)`` with ``size > 0``.
index db3f55e02b98b56c321e068d46c274754cb46736..fe46de2ae47435650a44bcdcce690a27811fd231 100644 (file)
@@ -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();
@@ -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 *