Skip to content

Commit d8f32be

Browse files
authored
pythongh-111089: Add PyUnicode_AsUTF8() to the limited C API (python#111121)
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.
1 parent 264f4af commit d8f32be

File tree

9 files changed

+23
-16
lines changed

9 files changed

+23
-16
lines changed

Doc/data/stable_abi.dat

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/whatsnew/3.13.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,9 @@ New Features
10691069
limited C API.
10701070
(Contributed by Victor Stinner in :gh:`85283`.)
10711071

1072+
* Add :c:func:`PyUnicode_AsUTF8` function to the limited C API.
1073+
(Contributed by Victor Stinner in :gh:`111089`.)
1074+
10721075

10731076
Porting to Python 3.13
10741077
----------------------

Include/cpython/unicodeobject.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -440,19 +440,6 @@ 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-
// 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-
456443

457444
/* === Characters Type APIs =============================================== */
458445

Include/unicodeobject.h

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

446-
// Returns a pointer to the default encoding (UTF-8) of the
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
447457
// Unicode object unicode and the size of the encoded representation
448458
// in bytes stored in `*size` (if size is not NULL).
449459
//

Lib/test/test_stable_abi_ctypes.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:func:`PyUnicode_AsUTF8` function to the limited C API. Patch by
2+
Victor Stinner.

Misc/stable_abi.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,3 +2478,5 @@
24782478
added = '3.13'
24792479
[function.PySys_AuditTuple]
24802480
added = '3.13'
2481+
[function.PyUnicode_AsUTF8]
2482+
added = '3.13'

Modules/_multiprocessing/posixshmem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ _posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
4444
{
4545
int fd;
4646
int async_err = 0;
47-
const char *name = PyUnicode_AsUTF8AndSize(path, NULL);
47+
const char *name = PyUnicode_AsUTF8(path);
4848
if (name == NULL) {
4949
return -1;
5050
}
@@ -83,7 +83,7 @@ _posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
8383
{
8484
int rv;
8585
int async_err = 0;
86-
const char *name = PyUnicode_AsUTF8AndSize(path, NULL);
86+
const char *name = PyUnicode_AsUTF8(path);
8787
if (name == NULL) {
8888
return NULL;
8989
}

PC/python3dll.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)