Skip to content

gh-129173: simplify PyCodec_XMLCharRefReplaceErrors logic - #129894

Merged
picnixz merged 15 commits into
python:mainfrom
picnixz:feat/codecs/xmlcharrefreplace-handler-129173
Mar 3, 2025
Merged

gh-129173: simplify PyCodec_XMLCharRefReplaceErrors logic#129894
picnixz merged 15 commits into
python:mainfrom
picnixz:feat/codecs/xmlcharrefreplace-handler-129173

Conversation

@picnixz

@picnixzpicnixz commented Feb 9, 2025

Copy link
Copy Markdown
Member

@picnixz

Copy link
Copy Markdown
MemberAuthor

Huh, I probably messed up my index somewhere. Will fix it later or tomorrow.

Comment threadPython/codecs.c Outdated
Comment threadPython/codecs.c Outdated
Comment threadPython/codecs.c Outdated
Comment threadPython/codecs.c Outdated
Comment threadPython/codecs.c Outdated
Comment threadPython/codecs.c Outdated
Comment threadPython/codecs.c Outdated
@picnixzpicnixz changed the title gh-129173: Use new helpers in the xmlcharrefreplace handler.gh-129173: Use new helpers in the xmlcharrefreplace handlerFeb 24, 2025
@picnixz
picnixz marked this pull request as ready for review February 25, 2025 13:28
@picnixz
picnixz requested a review from encukouFebruary 25, 2025 13:28
@encukou

Copy link
Copy Markdown
Member

I filed my suggestion as https://github.com/picnixz/cpython/pull/1/files

@picnixz

Copy link
Copy Markdown
MemberAuthor

I've applied your suggestion and tweaked it a bit. By the way, I observe that I forgot to remove a "object is ready" comment. Do you mind that after this PR and the following one for backslashreplace, I skim through the code base to remove the un-necessary related comments? there are a few occurrences of saying that some Unicode object is "ready"

@picnixz
picnixz requested review from encukou and removed request for encukouMarch 2, 2025 12:01
@encukou

Copy link
Copy Markdown
Member

That's a style change, which we generally only do when touching neighboring code.
On the other hand, the comments will be getting increasingly confusing as PyUnicode_READY/PyUnicode_WCHAR_KIND is forgotten and people don't associate “ready” with them.

How many are there? I'd need to review each of those removals in context.

Comment threadPython/codecs.c Outdated
Comment threadPython/codecs.c Outdated
@picnixz

picnixz commented Mar 3, 2025

Copy link
Copy Markdown
MemberAuthor

How many are there? I'd need to review each of those removals in context.

Apart from those already in codecs.c that I forgot to remove, not many:

/* result is guaranteed to be ready, as it is compact. */
kind=PyUnicode_KIND(result);
data=PyUnicode_DATA(result);

result=nfd_nfkd(self, input, k);
if (!result)
returnNULL;
/* result will be "ready". */
kind=PyUnicode_KIND(result);
data=PyUnicode_DATA(result);
len=PyUnicode_GET_LENGTH(result);

kind=PyUnicode_KIND(modified);
out=PyUnicode_DATA(modified);
PyUnicode_WRITE(kind, out, 0, '\r');
memcpy(out+kind, PyUnicode_DATA(output), kind*output_len);
Py_SETREF(output, modified); /* output remains ready */
self->pendingcr=0;
output_len++;

/* decoded_chars is guaranteed to be "ready". */
avail= (PyUnicode_GET_LENGTH(self->decoded_chars)
-self->decoded_chars_used);

/* Verify that the identifier follows PEP 3131.
All identifier strings are guaranteed to be "ready" unicode objects.
*/
staticint
verify_identifier(structtok_state*tok)

cpython/Parser/pegen.c

Lines 505 to 513 in a85eeb9

PyObject*
_PyPegen_new_identifier(Parser*p, constchar*n)
{
PyObject*id=PyUnicode_DecodeUTF8(n, (Py_ssize_t)strlen(n), NULL);
if (!id) {
goto error;
}
/* PyUnicode_DecodeUTF8 should always return a ready string. */
assert(PyUnicode_IS_READY(id));

if (!PyUnicode_IS_READY(filename)) {
/* Don't make a Unicode string ready to avoid reentrant calls
to tracemalloc_alloc() or tracemalloc_realloc() */
#ifdefTRACE_DEBUG
tracemalloc_error("filename is not a ready unicode string");
#endif
return;
}

The tracemalloc one seems to be dead code.

@picnixz
picnixz requested a review from encukouMarch 3, 2025 10:39

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

Thanks!

@encukou

Copy link
Copy Markdown
Member

You might want to split the comment removals to 5 PRs (each to be seen by different subject experts), and combine them with removing the remaining calls to PyUnicode_IS_READY.

Careful, the one in unicodeobject.c refers to a different kind of "ready", PyUnicode_Type.tp_flags & Py_TPFLAGS_READY.

@picnixz

Copy link
Copy Markdown
MemberAuthor

Careful, the one in unicodeobject.c refers to a different kind of "ready", PyUnicode_Type.tp_flags & Py_TPFLAGS_READY.

Oups, you're right.

Comment threadPython/codecs.c
@picnixz
picnixz enabled auto-merge (squash) March 3, 2025 11:16
@picnixzpicnixz changed the title gh-129173: Use new helpers in the xmlcharrefreplace handlergh-129173: simplify PyCodec_XMLCharRefReplaceErrors logicMar 3, 2025
@picnixz
picnixz disabled auto-merge March 3, 2025 11:16
@picnixz
picnixz enabled auto-merge (squash) March 3, 2025 11:17
@picnixz

Copy link
Copy Markdown
MemberAuthor

I really want to be able to preview the commit message when I'm enabling auto-merge...

@picnixz
picnixz merged commit f693f84 into python:mainMar 3, 2025
@picnixz
picnixz deleted the feat/codecs/xmlcharrefreplace-handler-129173 branch March 3, 2025 12:17
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@picnixz@encukou