Skip to content

gh-125196: Use PyUnicodeWriter in parser - #125271

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:writer_pegen
Oct 12, 2024
Merged

vstinner merged 1 commit into
python:mainfrom
vstinner:writer_pegen

Conversation

@vstinner

@vstinner vstinner commented Oct 10, 2024 •

Copy link
Copy Markdown
Member

Replace the private _PyUnicodeWriter API with the public PyUnicodeWriter API in _PyPegen_concatenate_strings().

Replace the private _PyUnicodeWriter API with the public
PyUnicodeWriter API in _PyPegen_concatenate_strings().
Comment thread Parser/action_helpers.c
PyObject *concat_str = _PyUnicodeWriter_Finish(&writer);
PyObject *concat_str = PyUnicodeWriter_Finish(writer);
if (concat_str == NULL) {
_PyUnicodeWriter_Dealloc(&writer);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The writer must not be used after _PyUnicodeWriter_Finish().

@pablogsal pablogsal 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.

LGTM

Comment thread Parser/action_helpers.c
PyObject *kind = elem->v.Constant.kind;

_PyUnicodeWriter_Init(&writer);
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);

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.

What's the 0 here?

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.

IIRC that's the number of pre-allocated characters. If you know that you will need N characters, I think it's better to put N in advance so that you don't have to resize the buffer. Otherwise, 0 is a good choice if you don't know in advance how many characters you'll eventually need (at the cost of resizes).

@vstinner
vstinner merged commit 4a943c3 into python:main Oct 12, 2024
@vstinner
vstinner deleted the writer_pegen branch October 12, 2024 07:28
@vstinner

Copy link
Copy Markdown
Member Author

Merged, thanks for the review @pablogsal.

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

3 participants