bpo-36346: Raise DeprecationWarning when creating legacy Unicode (GH-… · python/cpython@038dd0f (original) (raw)

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -213,6 +213,11 @@ Porting to Python 3.10
213 213 for historical reason. It is no longer allowed.
214 214 (Contributed by Victor Stinner in :issue:`40839`.)
215 215
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 +
216 221 Removed
217 222 -------
218 223
Original file line number Diff line number Diff line change
@@ -725,7 +725,9 @@ def test_isidentifier_legacy(self):
725 725 import _testcapi
726 726 u = '𝖀𝖓𝖎𝖈𝖔𝖉𝖊'
727 727 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())
729 731
730 732 def test_isprintable(self):
731 733 self.assertTrue("".isprintable())
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 +Raises DeprecationWarning for ``PyUnicode_FromUnicode(NULL, size)`` and
2 +``PyUnicode_FromStringAndSize(NULL, size)`` with ``size > 0``.
Original file line number Diff line number Diff line change
@@ -2179,8 +2179,16 @@ unicode_char(Py_UCS4 ch)
2179 2179 PyObject *
2180 2180 PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
2181 2181 {
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 + }
2183 2190 return (PyObject*)_PyUnicode_New(size);
2191 + }
2184 2192
2185 2193 if (size < 0) {
2186 2194 PyErr_BadInternalCall();
@@ -2266,10 +2274,19 @@ PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
2266 2274 "Negative size passed to PyUnicode_FromStringAndSize");
2267 2275 return NULL;
2268 2276 }
2269 -if (u != NULL)
2277 +if (u != NULL) {
2270 2278 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 + }
2272 2288 return (PyObject *)_PyUnicode_New(size);
2289 + }
2273 2290 }
2274 2291
2275 2292 PyObject *