-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-95504: Fix sign placement in PyUnicode_FromFormat #95505
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
Changes from 1 commit
2d6f3e1
bd72e2a
95b4f83
99b0ebc
63e9afd
d70fa1f
79a8138
e8d0edf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix sign placement when specifying width or precision in | ||
``PyUnicode_FromFormat``. Patch by Philip Georgi. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2482,29 +2482,42 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer, | |
} | ||
assert(len >= 0); | ||
|
||
if (precision < len) | ||
precision = len; | ||
int negative = buffer[0]=='-'?1:0; | ||
philg314 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
len -= negative; | ||
|
||
precision = Py_MAX(precision, len); | ||
width = Py_MAX(width, precision + negative); | ||
|
||
arglen = Py_MAX(precision, width); | ||
if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) | ||
return NULL; | ||
|
||
if (width > precision) { | ||
Py_UCS4 fillchar; | ||
fill = width - precision; | ||
fillchar = zeropad?'0':' '; | ||
if (negative && zeropad) { | ||
if (_PyUnicodeWriter_WriteChar(writer, '-') == -1) | ||
return NULL; | ||
} | ||
|
||
Py_UCS4 fillchar = zeropad?'0':' '; | ||
fill = width - precision - negative; | ||
if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) | ||
return NULL; | ||
writer->pos += fill; | ||
|
||
if (negative && !zeropad) { | ||
if (_PyUnicodeWriter_WriteChar(writer, '-') == -1) | ||
return NULL; | ||
} | ||
Comment on lines
+2507
to
+2510
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. It can be simpler. Instead of writing minus explicitly, you can write it as a part of the buffer. You will get the same result if remove 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. That wouldn't work with precision: |
||
} | ||
|
||
if (precision > len) { | ||
fill = precision - len; | ||
if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) | ||
return NULL; | ||
writer->pos += fill; | ||
} | ||
|
||
if (_PyUnicodeWriter_WriteASCIIString(writer, buffer, len) < 0) | ||
if (_PyUnicodeWriter_WriteASCIIString(writer, &buffer[negative], len) < 0) | ||
return NULL; | ||
break; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.