Skip to content

gh-101703: Use PyOS_snprintf in bytesobject.c - #161994

Open
sobolevn wants to merge 4 commits into
python:mainfrom
sobolevn:issue-101703-bytesobject
Open

sobolevn wants to merge 4 commits into
python:mainfrom
sobolevn:issue-101703-bytesobject

Conversation

@sobolevn

@sobolevn sobolevn commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

@sobolevn
sobolevn force-pushed the issue-101703-bytesobject branch from ead4b04 to 6122dec Compare November 14, 2026 10:54

@sergey-miryanov sergey-miryanov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

Small nitpick: WDYT about moving assert(1 <= len && len < (Py_ssize_t)sizeof(buffer)); to the macro too?

Comment thread Modules/_datetimemodule.c Outdated
make_freplacement(PyObject *object)
{
char freplacement[64];
int len;

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.

This is churn IMO (the asserts guard nothing even if they did fail), and will probably also result in unused variable warnings.

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.

Sorry, I did not understand this part: "the asserts guard nothing even if they did fail" :)
What do you mean?

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.

PyOS_snprintf already truncates and NUL-terminates, so PyUnicode_FromString(freplacement) is safe regardless.

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.

asserts here are just checking that the buffer write produced the expected result. But, ok, I removed it :)

Not really important.

@cmaloney

cmaloney commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Rather than migrating from sprintf to PyOS_snprintf could we go more PyBytesWriter + PyLong native instead?

I did an experiment (cmaloney@e50228c) and on macOS it comes out faster because snprintf has to build and look at var args + python long objects don't have to allocate a lot of the time.

Sign up for free to 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.

4 participants