-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
[FIX] Handle decimal and thousand separator in 'round_trip' converer #35377
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2da9aa7
Handle `decimal` and `tsep` in `round_trip` converter (#35365)
ales-erjavec dcd8de1
Simplify str_copy_decimal_str.
ales-erjavec cd23ba1
Add test for float_precision parsers equality
ales-erjavec bee7fdf
Simplify round_trip converter
ales-erjavec 66f8be5
Use strncpy
ales-erjavec 42c509c
Just define the endptr and endpc
ales-erjavec 8703ad6
Add whatsnew entry
ales-erjavec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1778,20 +1778,73 @@ double precise_xstrtod(const char *str, char **endptr, char decimal, | |
return number; | ||
} | ||
|
||
/* copy a decimal number string with `decimal`, `tsep` as decimal point | ||
and thousands separator to an equivalent c-locale decimal string (striping | ||
`tsep`, replacing `decimal` with '.'). The returned memory should be free-d | ||
with a call to `free`. | ||
*/ | ||
|
||
char* _str_copy_decimal_str_c(const char *s, char **endpos, char decimal, | ||
char tsep) { | ||
const char *p = s; | ||
size_t length = strlen(s); | ||
char *s_copy = malloc(length + 1); | ||
char *dst = s_copy; | ||
// Copy Leading sign | ||
if (*p == '+' || *p == '-') { | ||
*dst++ = *p++; | ||
} | ||
// Copy integer part dropping `tsep` | ||
while (isdigit_ascii(*p)) { | ||
*dst++ = *p++; | ||
p += (tsep != '\0' && *p == tsep); | ||
} | ||
// Replace `decimal` with '.' | ||
if (*p == decimal) { | ||
*dst++ = '.'; | ||
p++; | ||
} | ||
// Copy the remainder of the string as is. | ||
strncpy(dst, p, length + 1 - (p - s)); | ||
if (endpos != NULL) | ||
*endpos = (char *)(s + length); | ||
return s_copy; | ||
} | ||
|
||
|
||
double round_trip(const char *p, char **q, char decimal, char sci, char tsep, | ||
int skip_trailing, int *error, int *maybe_int) { | ||
// 'normalize' representation to C-locale; replace decimal with '.' and | ||
// remove t(housand)sep. | ||
char *endptr; | ||
char *pc = _str_copy_decimal_str_c(p, &endptr, decimal, tsep); | ||
// This is called from a nogil block in parsers.pyx | ||
// so need to explicitly get GIL before Python calls | ||
PyGILState_STATE gstate; | ||
gstate = PyGILState_Ensure(); | ||
|
||
double r = PyOS_string_to_double(p, q, 0); | ||
char *endpc; | ||
double r = PyOS_string_to_double(pc, &endpc, 0); | ||
// PyOS_string_to_double needs to consume the whole string | ||
if (endpc == pc + strlen(pc)) { | ||
if (q != NULL) { | ||
// report endptr from source string (p) | ||
*q = (char *) endptr; | ||
} | ||
} else { | ||
*error = -1; | ||
if (q != NULL) { | ||
// p and pc are different len due to tsep removal. Can't report | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a test that hits this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. The "1,2e1_0" for instance. I.e something that is prefixed with a number but has extra trailing characters. |
||
// how much it has consumed of p. Just rewind to beginning. | ||
*q = (char *)p; | ||
} | ||
} | ||
if (maybe_int != NULL) *maybe_int = 0; | ||
if (PyErr_Occurred() != NULL) *error = -1; | ||
else if (r == Py_HUGE_VAL) *error = (int)Py_HUGE_VAL; | ||
PyErr_Clear(); | ||
|
||
PyGILState_Release(gstate); | ||
free(pc); | ||
return r; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the null byte check here serve a purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
tsep
is null when no thousands separator is defined. But the loop must not skip the terminating null byte in the input string (the second condition) which would happen on e.g. '123\0'