New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PyLong_FromString documentation should state that the string must be null-terminated #59200
Comments
PyLong_FromString will raise a ValueError if the given string doesn't contain a null byte after the digits. For example, this will result in a ValueError char *pend;
PyLong_FromString("1234 extra", &pend, 10) While this will successfully read the number and set the pointer to the extra data: char *pend;
PyLong_FromString("1234\0extra", &pend, 10) The requirement for a null-terminated string of digits is not clear from the docs. Suggested re-wording attached. |
Patch doesn't apply to current Python trunk (18 months later). Do you know what version you wrote this against? The current wording is different. |
The description is nonsensical as is; not sure the patch goes far enough. C-style strings are *defined* to end at the NUL terminator; if it really needs a NUL after the int, saying it "points to the first character which follows the representation of the number" is highly misleading; the NUL isn't logically a character in the C-string way of looking at things. The patch is also wrong; the digits need not end in a NUL byte (trailing whitespace is allowed). AFAICT, the function really uses pend for two purposes:
#1 is a mostly useless bit of info (strlen would be equally informative, and if the value parsed, you rarely care how long it was anyway), so pend is, practically speaking, solely for error-checking/reporting. The rewrite should basically say what is allowed (making it clear anything beyond the single parsable integer value with optional leading/trailing whitespace is illegal), and making it clear that pend always points to the end of the string on success (not just after the representation of the number, it's after the trailing whitespace too), and on failure indicates where parsing failed. |
Has it been considered to instead change the function to fit its description, i.e. not require a NULL-terminated string? I have a use case that would benefit from this (decoding arbitrary ints when parsing JSON without |
It's documented as taking a |
@Rosuav No, the behaviour does not fit the documentation. The problem isn't the end of |
Hmm, I'm not convinced that it implies that |
Hmm, I suppose it could also be read differently, although at least two people have understood it that way (the original issue submitter and me). #26774 has been stale for almost a year, although the reviews requesting changes were only added a few months ago. Fine if I create a replacement PR for it? |
Yeah, I think a replacement PR would be the way to go here - I'm not aware of a way to contribute to someone else's PR when you're not part of the organization. |
* Indicate return value on error * Explain in what `*pend` points to exactly both on success and on error * Mention that trailing whitespace is ignored; also replace 'spaces' with 'whitespace' since all ASCII whitespace is skipped, not just spaces * Add that `str` must be NULL-terminated
Thanks, looks like this has been completed |
rfk mannequin commentedJun 4, 2012
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: