Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_ctypes/test_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def test_PyOS_snprintf(self):
PyOS_snprintf(buf, sizeof(buf), b"Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3)
self.assertEqual(buf.value, b"Hello from ctypes (1, 2, 3)")

# truncated output: C99 return value and trailing NUL
small = create_string_buffer(b"x" * 8, 8)
rv = PyOS_snprintf(small, sizeof(small), b"Hello from %s", b"ctypes")
self.assertEqual(rv, len(b"Hello from ctypes"))
self.assertEqual(small.raw, b"Hello f\0")
Comment on lines +71 to +75

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.

I would prefer to check truncated string on the %s argument rather than on the format string. Also, you should test the invalid format string (-1 return value):

Suggested change
# truncated output: C99 return value and trailing NUL
small = create_string_buffer(b"x" * 8, 8)
rv = PyOS_snprintf(small, sizeof(small), b"Hello from %s", b"ctypes")
self.assertEqual(rv, len(b"Hello from ctypes"))
self.assertEqual(small.raw, b"Hello f\0")
# Test truncated output
small = create_string_buffer(14)
rv = PyOS_snprintf(small, sizeof(small), b"Hello from %s", b"ctypes")
self.assertEqual(rv, len(b"Hello from ctypes"))
self.assertEqual(small.raw, b"Hello from ct\0")
# Test invalid format string
buf = create_string_buffer(14)
rv = PyOS_snprintf(buf, sizeof(buf), b"Hello from %Z", 123)
self.assertEqual(rv, -1)

Usually, we test the Python C API in test_capi. But well, there are already some PyOS_snprintf() tests in test_ctypes, so I'm ok to add a few more there.


# not enough arguments
self.assertRaises(TypeError, PyOS_snprintf, buf)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:c:func:`PyOS_vsnprintf` now calls :c:func:`!vsnprintf` on Windows, instead of

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.

Suggested change
:c:func:`PyOS_vsnprintf` now calls :c:func:`!vsnprintf` on Windows, instead of
:c:func:`PyOS_vsnprintf` now calls standard :c:func:`!vsnprintf` on Windows, instead of

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.

Maybe even use the standard C99?

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.

https://peps.python.org/pep-0007/ says that Python requires C11. MSVC doesn't support C99. So if we mention a standard, it would be better to refer to C11.

C11 is basically C99 without features not supported by MSVC.

the non-standard :c:func:`!_vsnprintf`. When the output is truncated, it now
returns the number of characters that would have been written, as on other

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.

Suggested change
returns the number of characters that would have been written, as on other
returns the number of bytes that would have been written (excluding the null byte), as on other

platforms, instead of a negative value.
20 changes: 5 additions & 15 deletions Python/mysnprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

/* snprintf() and vsnprintf() wrappers.

If the platform has vsnprintf, we use it, else we
emulate it in a half-hearted way. Even if the platform has it, we wrap
it because platforms differ in what vsnprintf does in case the buffer
is too small: C99 behavior is to return the number of characters that
would have been written had the buffer not been too small, and to set
the last byte of the buffer to \0. At least MS _vsnprintf returns a
negative value instead, and fills the entire buffer with non-\0 data.
We wrap vsnprintf() to guarantee consistent behavior in corner cases:
C99 behavior is to return the number of characters that would have been
written had the buffer not been too small, and to set the last byte of the
buffer to \0.
Unlike C99, our wrappers do not support passing a null buffer.

The wrappers ensure that str[size-1] is always \0 upon return.
Expand All @@ -28,10 +25,7 @@

When rv < 0, "something bad happened". str[size-1] is \0 in this
case too, but the rest of str is unreliable. It could be that
an error in format codes was detected by libc, or on platforms
with a non-C99 vsnprintf simply that the buffer wasn't big enough
to avoid truncation, or on platforms without any vsnprintf that
PyMem_Malloc couldn't obtain space for a temp buffer.
an error in format codes was detected by libc.

CAUTION: Unlike C99, str != NULL and size > 0 are required.
Also, size must be smaller than INT_MAX.
Expand Down Expand Up @@ -66,11 +60,7 @@ PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
goto Done;
}

#if defined(_MSC_VER)
len = _vsnprintf(str, size, format, va);
#else
len = vsnprintf(str, size, format, va);
#endif

Done:
if (size > 0) {
Expand Down
Loading