Skip to content

gh-74756: support precision field for integer formatting types #131926

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

Closed
wants to merge 16 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
+ check if nbits is too big (fix warnings)
  • Loading branch information
skirpichev committed Apr 8, 2025
commit 65a5e7084efd762532694c70ef133d981e93bb33
10 changes: 9 additions & 1 deletion Python/formatter_unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,15 @@
}

int64_t nbits = _PyLong_NumBits(value);
int64_t shift = Py_MAX(precision, (nbits + dbits - 1)/dbits);

if (nbits > PY_SSIZE_T_MAX) {
PyErr_SetString(PyExc_OverflowError,
"int too large to format");
goto done;
}

Py_ssize_t shift = Py_MAX(precision,

Check warning on line 1110 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'initializing': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1110 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'initializing': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1110 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'initializing': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1110 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'initializing': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
((Py_ssize_t)nbits + dbits - 1)/dbits);

shift *= dbits;
shift--;
Expand Down Expand Up @@ -1149,7 +1157,7 @@
/* Prepend enough leading zeros (after sign and prefix) */

int sign = PyUnicode_READ_CHAR(tmp, 0) == '-';
Py_ssize_t tmp2_len = precision + leading_chars_to_skip + sign;

Check warning on line 1160 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'initializing': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1160 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'initializing': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1160 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'initializing': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1160 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'initializing': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
Py_ssize_t tmp_len = PyUnicode_GET_LENGTH(tmp);
Py_ssize_t gap = tmp2_len - tmp_len;

Expand All @@ -1159,7 +1167,7 @@

if (PyUnicode_CopyCharacters(tmp2, value_start + gap,
tmp, value_start,
precision) == -1) {

Check warning on line 1170 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'function': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1170 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'function': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1170 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'function': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1170 in Python/formatter_unicode.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'function': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
Py_DECREF(tmp2);
goto done;
}
Expand Down
Loading