Skip to content

gh-119609: Add PyUnicode_Export() and PyUnicode_Import() functions - #119610

Closed
vstinner wants to merge 13 commits into
python:mainfrom
vstinner:unicode_native
Closed

gh-119609: Add PyUnicode_Export() and PyUnicode_Import() functions#119610
vstinner wants to merge 13 commits into
python:mainfrom
vstinner:unicode_native

Conversation

@vstinner

@vstinnervstinner commented May 27, 2024

Copy link
Copy Markdown
Member

@vstinner

Copy link
Copy Markdown
MemberAuthor

Docs build failed because of #119607

@vstinner

Copy link
Copy Markdown
MemberAuthor

Docs build failed because of #119607

PR rebased on top of this fix.

Comment threadObjects/unicodeobject.c Outdated
@zooba

Copy link
Copy Markdown
Member

Petr indicated on the issue that he's got Thoughts and will write them up this week.

@vstinner
vstinner marked this pull request as draft May 29, 2024 12:21
@vstinnervstinner changed the title gh-119609: Add PyUnicode_AsNativeFormat() functiongh-119609: Add PyUnicode_Export() and PyUnicode_Import() functionsJun 13, 2024
@vstinner
vstinner marked this pull request as ready for review June 21, 2024 08:59
@vstinner

Copy link
Copy Markdown
MemberAuthor

@encukou: Please review the updated PR.

@encukou: I prefer to make sure that the exported string ends with a NUL character, rather than making sure that it's not the case. It's convenient and cheap.

I rebased the PR on the main branch, I fixed merge conflicts, I updated the doc for new function names, I included some of Petr's suggestions. I marked the PR as ready for review (it's no longer a draft).

@encukouencukou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!
(I don't know about the other C API WG members.)

Comment threadDoc/c-api/unicode.rst Outdated
Comment threadDoc/c-api/unicode.rst
Comment threadDoc/c-api/unicode.rst
Comment threadDoc/c-api/unicode.rst Outdated
vstinnerand others added 3 commits June 22, 2024 17:17
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@vstinner

Copy link
Copy Markdown
MemberAuthor

@davidism: Does the proposed API solve your MarkupSafe use case?

@vstinner

Copy link
Copy Markdown
MemberAuthor

I created Add PyUnicode_Export() and PyUnicode_Import() to the limited C API issue in the C API WG Decisions project.

@davidism

davidism commented Jun 24, 2024

Copy link
Copy Markdown

I really appreciate you thinking about this after talking at PyCon! From what I can tell (I'm not very familiar with C), the MarkupSafe code would mostly remain the same, but use the two new abi3 functions instead of the existing functions? Perhaps you could show a brief example or a high level explanation of what I would change? https://github.com/pallets/markupsafe/blob/d12057361ad75c4569e2e61712c234acc69d5d0b/src/markupsafe/_speedups.c

@vstinner

Copy link
Copy Markdown
MemberAuthor

@davidism:

I really appreciate you thinking about this after talking at PyCon!

You're welcome.

From what I can tell (I'm not very familiar with C), the MarkupSafe code would mostly remain the same, but use the two new abi3 functions instead of the existing functions? Perhaps you could show a brief example or a high level explanation of what I would change?

I created a PR to show how these functions can be used: pallets/markupsafe#451

The stable ABI is less efficient since it requires to allocate a UCS1/UCS2/UCS4 buffer first, write into the buffer, and only then create a Python str object from this buffer. That's because PyUnicode_New() is excluded from the stable ABI.

@vstinner

Copy link
Copy Markdown
MemberAuthor

I wrote a different PR using Py_buffer: #123738. I close this PR for now.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@vstinner@zooba@davidism@encukou@erlend-aasland