Skip to content

Commit be7e341

Browse files
committed
Revert "pythongh-111089: PyUnicode_AsUTF8() now raises on embedded NUL (python#111091)"
This reverts commit d731579.
1 parent 938652f commit be7e341

File tree

8 files changed

+31
-49
lines changed

8 files changed

+31
-49
lines changed

Doc/c-api/unicode.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -992,19 +992,11 @@ These are the UTF-8 codec APIs:
992992
993993
As :c:func:`PyUnicode_AsUTF8AndSize`, but does not store the size.
994994
995-
Raise an exception if the *unicode* string contains embedded null
996-
characters. To accept embedded null characters and truncate on purpose
997-
at the first null byte, ``PyUnicode_AsUTF8AndSize(unicode, NULL)`` can be
998-
used instead.
999-
1000995
.. versionadded:: 3.3
1001996
1002997
.. versionchanged:: 3.7
1003998
The return type is now ``const char *`` rather of ``char *``.
1004999
1005-
.. versionchanged:: 3.13
1006-
Raise an exception if the string contains embedded null characters.
1007-
10081000
10091001
UTF-32 Codecs
10101002
"""""""""""""

Doc/whatsnew/3.13.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,12 +1222,6 @@ Porting to Python 3.13
12221222
Note that ``Py_TRASHCAN_BEGIN`` has a second argument which
12231223
should be the deallocation function it is in.
12241224

1225-
* The :c:func:`PyUnicode_AsUTF8` function now raises an exception if the string
1226-
contains embedded null characters. To accept embedded null characters and
1227-
truncate on purpose at the first null byte,
1228-
``PyUnicode_AsUTF8AndSize(unicode, NULL)`` can be used instead.
1229-
(Contributed by Victor Stinner in :gh:`111089`.)
1230-
12311225
* On Windows, ``Python.h`` no longer includes the ``<stddef.h>`` standard
12321226
header file. If needed, it should now be included explicitly. For example, it
12331227
provides ``offsetof()`` function, and ``size_t`` and ``ptrdiff_t`` types.

Include/cpython/unicodeobject.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,22 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData(
440440
const void *buffer,
441441
Py_ssize_t size);
442442

443+
/* --- Manage the default encoding ---------------------------------------- */
444+
445+
/* Returns a pointer to the default encoding (UTF-8) of the
446+
Unicode object unicode.
447+
448+
Like PyUnicode_AsUTF8AndSize(), this also caches the UTF-8 representation
449+
in the unicodeobject.
450+
451+
_PyUnicode_AsString is a #define for PyUnicode_AsUTF8 to
452+
support the previous internal function with the same behaviour.
453+
454+
Use of this API is DEPRECATED since no size information can be
455+
extracted from the returned data.
456+
*/
457+
458+
PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
443459

444460
/* === Characters Type APIs =============================================== */
445461

Include/unicodeobject.h

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -443,25 +443,17 @@ PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(
443443
PyObject *unicode /* Unicode object */
444444
);
445445

446-
// Returns a pointer to the UTF-8 encoding of the Unicode object unicode.
447-
//
448-
// Raise an exception if the string contains embedded null characters.
449-
// Use PyUnicode_AsUTF8AndSize() to accept embedded null characters.
450-
//
451-
// This function caches the UTF-8 encoded string in the Unicode object
452-
// and subsequent calls will return the same string. The memory is released
453-
// when the Unicode object is deallocated.
454-
PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
455-
456-
// Returns a pointer to the UTF-8 encoding of the
457-
// Unicode object unicode and the size of the encoded representation
458-
// in bytes stored in `*size` (if size is not NULL).
459-
//
460-
// On error, `*size` is set to 0 (if size is not NULL).
461-
//
462-
// This function caches the UTF-8 encoded string in the Unicode object
463-
// and subsequent calls will return the same string. The memory is released
464-
// when the Unicode object is deallocated.
446+
/* Returns a pointer to the default encoding (UTF-8) of the
447+
Unicode object unicode and the size of the encoded representation
448+
in bytes stored in *size.
449+
450+
In case of an error, no *size is set.
451+
452+
This function caches the UTF-8 encoded string in the unicodeobject
453+
and subsequent calls will return the same string. The memory is released
454+
when the unicodeobject is deallocated.
455+
*/
456+
465457
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
466458
PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize(
467459
PyObject *unicode,

Lib/test/test_capi/test_unicode.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -914,10 +914,7 @@ def test_asutf8(self):
914914
self.assertEqual(unicode_asutf8('abc', 4), b'abc\0')
915915
self.assertEqual(unicode_asutf8('абв', 7), b'\xd0\xb0\xd0\xb1\xd0\xb2\0')
916916
self.assertEqual(unicode_asutf8('\U0001f600', 5), b'\xf0\x9f\x98\x80\0')
917-
918-
# disallow embedded null characters
919-
self.assertRaises(ValueError, unicode_asutf8, 'abc\0', 0)
920-
self.assertRaises(ValueError, unicode_asutf8, 'abc\0def', 0)
917+
self.assertEqual(unicode_asutf8('abc\0def', 8), b'abc\0def\0')
921918

922919
self.assertRaises(UnicodeEncodeError, unicode_asutf8, '\ud8ff', 0)
923920
self.assertRaises(TypeError, unicode_asutf8, b'abc', 0)

Misc/NEWS.d/next/C API/2023-10-20-01-42-43.gh-issue-111089.VIrd5q.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

Objects/typeobject.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,14 +3501,13 @@ type_new_set_doc(PyTypeObject *type)
35013501
return 0;
35023502
}
35033503

3504-
Py_ssize_t doc_size;
3505-
const char *doc_str = PyUnicode_AsUTF8AndSize(doc, &doc_size);
3504+
const char *doc_str = PyUnicode_AsUTF8(doc);
35063505
if (doc_str == NULL) {
35073506
return -1;
35083507
}
35093508

35103509
// Silently truncate the docstring if it contains a null byte
3511-
Py_ssize_t size = doc_size + 1;
3510+
Py_ssize_t size = strlen(doc_str) + 1;
35123511
char *tp_doc = (char *)PyObject_Malloc(size);
35133512
if (tp_doc == NULL) {
35143513
PyErr_NoMemory();

Objects/unicodeobject.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3844,13 +3844,7 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
38443844
const char *
38453845
PyUnicode_AsUTF8(PyObject *unicode)
38463846
{
3847-
Py_ssize_t size;
3848-
const char *utf8 = PyUnicode_AsUTF8AndSize(unicode, &size);
3849-
if (utf8 != NULL && strlen(utf8) != (size_t)size) {
3850-
PyErr_SetString(PyExc_ValueError, "embedded null character");
3851-
return NULL;
3852-
}
3853-
return utf8;
3847+
return PyUnicode_AsUTF8AndSize(unicode, NULL);
38543848
}
38553849

38563850
/*

0 commit comments

Comments
 (0)