]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111089: Add PyUnicode_AsUTF8() to the limited C API (#111121)
authorVictor Stinner <vstinner@python.org>
Fri, 20 Oct 2023 17:29:27 +0000 (19:29 +0200)
committerGitHub <noreply@github.com>
Fri, 20 Oct 2023 17:29:27 +0000 (19:29 +0200)
Add PyUnicode_AsUTF8() function to the limited C API.

multiprocessing posixshmem now uses PyUnicode_AsUTF8() instead of
PyUnicode_AsUTF8AndSize(): the extension is built with the limited C
API. The function now raises an exception if the filename contains an
embedded null character instead of truncating silently the filename.

Doc/data/stable_abi.dat
Doc/whatsnew/3.13.rst
Include/cpython/unicodeobject.h
Include/unicodeobject.h
Lib/test/test_stable_abi_ctypes.py
Misc/NEWS.d/next/C API/2023-10-20-18-07-24.gh-issue-111089.RxkyrQ.rst [new file with mode: 0644]
Misc/stable_abi.toml
Modules/_multiprocessing/posixshmem.c
PC/python3dll.c

index 811b1bd84d24174e068ec489cc16c4a24758c5e8..52d6d967d663270bd27ce5c530fa043f4959fed2 100644 (file)
@@ -726,6 +726,7 @@ function,PyUnicode_AsUCS4,3.7,,
 function,PyUnicode_AsUCS4Copy,3.7,,
 function,PyUnicode_AsUTF16String,3.2,,
 function,PyUnicode_AsUTF32String,3.2,,
+function,PyUnicode_AsUTF8,3.13,,
 function,PyUnicode_AsUTF8AndSize,3.10,,
 function,PyUnicode_AsUTF8String,3.2,,
 function,PyUnicode_AsUnicodeEscapeString,3.2,,
index 34e4d67224b02c1b03a6b33a65aff2d00a501be9..5da5f9380618ebfb44ec47baebe31a67e6ca4140 100644 (file)
@@ -1069,6 +1069,9 @@ New Features
   limited C API.
   (Contributed by Victor Stinner in :gh:`85283`.)
 
+* Add :c:func:`PyUnicode_AsUTF8` function to the limited C API.
+  (Contributed by Victor Stinner in :gh:`111089`.)
+
 
 Porting to Python 3.13
 ----------------------
index d67553c6657891941a3a46af1940ef09709edec2..d200fa0622cef590ca02abd465a9d102326a5385 100644 (file)
@@ -440,19 +440,6 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData(
     const void *buffer,
     Py_ssize_t size);
 
-/* --- Manage the default encoding ---------------------------------------- */
-
-// Returns a pointer to the default encoding (UTF-8) of the
-// Unicode object unicode.
-//
-// Raise an exception if the string contains embedded null characters.
-// Use PyUnicode_AsUTF8AndSize() to accept embedded null characters.
-//
-// This function caches the UTF-8 encoded string in the Unicode object
-// and subsequent calls will return the same string. The memory is released
-// when the Unicode object is deallocated.
-PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
-
 
 /* === Characters Type APIs =============================================== */
 
index 1e5753dae6ca79acbdf30842f665d1234cdf8ae8..ee7b769ce5a6fc2d194f1229798c6daf20f60702 100644 (file)
@@ -443,7 +443,17 @@ PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(
     PyObject *unicode           /* Unicode object */
     );
 
-// Returns a pointer to the default encoding (UTF-8) of the
+// Returns a pointer to the UTF-8 encoding of the Unicode object unicode.
+//
+// Raise an exception if the string contains embedded null characters.
+// Use PyUnicode_AsUTF8AndSize() to accept embedded null characters.
+//
+// This function caches the UTF-8 encoded string in the Unicode object
+// and subsequent calls will return the same string. The memory is released
+// when the Unicode object is deallocated.
+PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
+
+// Returns a pointer to the UTF-8 encoding of the
 // Unicode object unicode and the size of the encoded representation
 // in bytes stored in `*size` (if size is not NULL).
 //
index 6d5353c22764df1342db4df2745a496b462116e2..88bc0fd4025a171365a8a661a6b3cea82bebe116 100644 (file)
@@ -745,6 +745,7 @@ SYMBOL_NAMES = (
     "PyUnicode_AsUCS4Copy",
     "PyUnicode_AsUTF16String",
     "PyUnicode_AsUTF32String",
+    "PyUnicode_AsUTF8",
     "PyUnicode_AsUTF8AndSize",
     "PyUnicode_AsUTF8String",
     "PyUnicode_AsUnicodeEscapeString",
diff --git a/Misc/NEWS.d/next/C API/2023-10-20-18-07-24.gh-issue-111089.RxkyrQ.rst b/Misc/NEWS.d/next/C API/2023-10-20-18-07-24.gh-issue-111089.RxkyrQ.rst
new file mode 100644 (file)
index 0000000..fe32e06
--- /dev/null
@@ -0,0 +1,2 @@
+Add :c:func:`PyUnicode_AsUTF8` function to the limited C API. Patch by
+Victor Stinner.
index 75c260c8f1b2be869ed34d07dbddfa674e98258a..0601de20fe0f46b9f189d1a09e5623180dd5daa1 100644 (file)
     added = '3.13'
 [function.PySys_AuditTuple]
     added = '3.13'
+[function.PyUnicode_AsUTF8]
+    added = '3.13'
index 317381a47ee65f367adc1d8066ebcb6af9189074..c4d1138534d8c58cdc3aa1554f94547e26ad77c1 100644 (file)
@@ -44,7 +44,7 @@ _posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
 {
     int fd;
     int async_err = 0;
-    const char *name = PyUnicode_AsUTF8AndSize(path, NULL);
+    const char *name = PyUnicode_AsUTF8(path);
     if (name == NULL) {
         return -1;
     }
@@ -83,7 +83,7 @@ _posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
 {
     int rv;
     int async_err = 0;
-    const char *name = PyUnicode_AsUTF8AndSize(path, NULL);
+    const char *name = PyUnicode_AsUTF8(path);
     if (name == NULL) {
         return NULL;
     }
index d12889f44d65b60262ab5ec8f9b0f802581c1a27..7f5d97ae4dc83f9c52e964ef5114c20cc71db890 100755 (executable)
@@ -661,6 +661,7 @@ EXPORT_FUNC(PyUnicode_AsUCS4Copy)
 EXPORT_FUNC(PyUnicode_AsUnicodeEscapeString)
 EXPORT_FUNC(PyUnicode_AsUTF16String)
 EXPORT_FUNC(PyUnicode_AsUTF32String)
+EXPORT_FUNC(PyUnicode_AsUTF8)
 EXPORT_FUNC(PyUnicode_AsUTF8AndSize)
 EXPORT_FUNC(PyUnicode_AsUTF8String)
 EXPORT_FUNC(PyUnicode_AsWideChar)