Repository navigation
Conversation
Since Visual Studio 2015, the MSVC runtime provides a C99-conforming vsnprintf(), so the _vsnprintf() special case is no longer needed. Add a test for the truncation case and update the outdated comment.
|
According to the comment changes in this PR, the behavior of |
|
Ah! This wasn't meant as a bug fix: the intent was |
|
Is it dead code for everyone or are there versions where it is NOT dead code or extension modules that could be affected when running on an older platform? If this is possible, I prefer not to change anything. |
|
"Dead code" was not the right term, sorry: the branch is really executed on MSVC builds, it is just a special case that is not needed anymore. Starting from python 3.5, The difference is the return value when the output is truncated: -1 before (windows specific), rv >= size now (standard C99). I don't think it can break much: The doc says That said, I will not insist much to get this merged. I like this change, it provides a (very) small value in code-readiness/cognitive load, though at the expense of a low risk of breaking some wrongly-written extensions. I know CPython value the second over everything. |
|
Let me rephrase it as follows: does the current code rely on fragile details and is it still correct with newer MSVC versions? if so, let's keep it that way since there is a slight behavior change (AFAIU) |
|
Current code leaks out an undocumented implementation detail on Windows, but it still correct with newer MSVC. |
|
Yeah so we don't need to change it I think. The docs are still correct, namely "something went wrong" so I would lean towards keeping it as is unless Chris thinks it is better to change (I will leave that to him). |
|
I think it's a cleanup that Python will have to do somewhere in its time (and seemingly so thinks Eryk Sun), but maybe it should be more prepared – maybe at least discussed on the discourse, so everybody is aware of the change. If it does not make it here, I will bring up the discussion on Python's discourse. |
|
Had we started new, I agree we would definitely use the C99 compliant
I agree with all your points, but an abi3 extension only written for Windows, not caring about other platforms and sloppily testing via the implementation detail int rv = PyOS_vsnprintf(buf, sizeof(buf), fmt, va);
if (rv < 0) {
/* error */
}
else {
/* Success */
}would be broken.
No, MS cares a lot about backwards compatibility ...
... and will likely keep |
|
OK. I have a last (bold) suggestion: what about we don't change |
|
I'm in a favor of replacing non-standard MSVC
Using C99 standard
It doesn't prevent us from changing PyOS_vsnprintf() implementation, especially if it's to fix the implementation :-) Hopefully, this function is rarely used. I searched for
libpff-python raises ValueError if scipy() doesn't check the return value! Code: Currently, libpff-python only raises ValueError on Windows if the string is truncated. I suppose that this library was not tested on Windows, or at least truncated strings were not tested on Windows. If MSVC
Correct. But such code doesn't respect PyOS_vsnprintf() documentation. I suggest to only change Python 3.16 (don't backport it) and document the change. If there is impacted code, it can be updated to handle also documented
In the 1,093,987 lines of C code of CPython, I'm not sure if I replied correctly to your question. |
| @@ -0,0 +1,4 @@ | |||
| :c:func:`PyOS_vsnprintf` now calls :c:func:`!vsnprintf` on Windows, instead of | |||
There was a problem hiding this comment.
| :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 |
There was a problem hiding this comment.
Maybe even use the standard C99?
There was a problem hiding this comment.
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.
| @@ -0,0 +1,4 @@ | |||
| :c:func:`PyOS_vsnprintf` now calls :c:func:`!vsnprintf` on Windows, instead of | |||
| 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 | |||
There was a problem hiding this comment.
| 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 |
| # 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") |
There was a problem hiding this comment.
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):
| # 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.
Thanks @vstinner for chiming in. I now also think this is the best way to go. |
Since Visual Studio 2015, the MSVC runtime provides a C99-conforming
vsnprintf(), so the_vsnprintf()special case inPyOS_vsnprintf()is no longer needed. On truncation,_vsnprintf()returned a negative value, unlike the C99 behavior documented forPyOS_vsnprintf().Also adds a test for the truncation case in
test_ctypesand updates the outdated comment.No existing caller relies on the negative return value for truncation. Marked as draft until the Windows CI results are in.
Fixes #162054. See also #101703 (comment)