Skip to content

[C API] Add Python PyUnicodeWriter API to the limited C API 3.16 #161172

Closed

Description

@vstinner

Currently, the limited C API has only few functions to create a Unicode string, as such PyUnicode_FromString() (from an UTF-8 string) or PyUnicode_FromWideChar() (from a wide string). The C API optimized for PEP 393 "compact strings" (added to Python 3.3) such as PyUnicode_New() and PyUnicode_WRITE() is excluded from the limited C API.

Python 3.14 adds PyUnicodeWriter API which is an efficient way to create a new str object from various string formats (ASCII, UTF-8, wide string, UCS4 string, etc.). This API doesn't expose implementation details (how strings are stored internally), so Python implementations other than CPython should be able to implement this C API in an efficient way.

So I propose adding these 13 functions to the limited C API:

  • PyUnicodeWriter_Create()
  • PyUnicodeWriter_DecodeUTF8Stateful()
  • PyUnicodeWriter_Discard()
  • PyUnicodeWriter_Finish()
  • PyUnicodeWriter_Format()
  • PyUnicodeWriter_WriteASCII()
  • PyUnicodeWriter_WriteChar()
  • PyUnicodeWriter_WriteRepr()
  • PyUnicodeWriter_WriteStr()
  • PyUnicodeWriter_WriteSubstring()
  • PyUnicodeWriter_WriteUCS4()
  • PyUnicodeWriter_WriteUTF8()
  • PyUnicodeWriter_WriteWideChar()

See the PyUnicodeWriter documentation.

In the PyPI top 15,000 projects (at 2026-09-17), I found 3 projects already using this Python 3.14 API:

  • asyncpg (0.31.0)
  • multidict (6.8.0)
  • simplejson (4.1.2)

Adding this API to the limited C API should help more projects to adopt it.

Note: the pythoncapi-compat project also provides the PyUnicodeWriter API on Python 3.6 - Python 3.13.

This change was approved by C API Working Group.

Linked PRs

Activity

  1. added a commit that references this issue on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions