Skip to content

Commit 9e3cc96

Browse files
committed
+ cleanup and docs
1 parent 8790325 commit 9e3cc96

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

Doc/library/string.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,11 @@ following:
398398

399399
.. index:: single: z; in string formatting
400400

401-
The ``'z'`` option coerces negative zero floating-point values to positive
402-
zero after rounding to the format precision. This option is only valid for
403-
floating-point presentation types.
401+
For floating-point presentation types the ``'z'`` option coerces negative zero
402+
floating-point values to positive zero after rounding to the format precision.
403+
For integer types ``'b'``, ``'o'``, ``'x'`` and ``'X'`` it can be used to
404+
interpret integer value as two's complement. This option is invalid for other
405+
presentation types.
404406

405407
.. versionchanged:: 3.11
406408
Added the ``'z'`` option (see also :pep:`682`).
@@ -464,10 +466,11 @@ used from the field content.
464466

465467
For integer presentation types (excluding ``'c'``), the precision gives the
466468
minimal number of digits to appear, expanded with an appropriate number of
467-
leading zeros. Note that for non-decimal presentation types --- integer value
468-
interpreted as ``max(k*precision, number.bit_length())``-bit two's complement,
469-
where ``k=1,3,4`` for ``'b'``, ``'o'`` and ``'x'``/``'X'`` types, respectively.
470-
A precision of ``0`` is treated as equivalent to a precision of ``1`` here.
469+
leading zeros. If ``'z'`` option specified for non-decimal presentation types
470+
--- integer value interpreted as ``max(k*precision, number.bit_length())``-bit
471+
two's complement, where ``k=1,3,4`` for ``'b'``, ``'o'`` and ``'x'``/``'X'``
472+
types, respectively. A precision of ``0`` is treated as equivalent to a
473+
precision of ``1`` here.
471474

472475
.. versionchanged:: next
473476
Precision specification allowed for integer presentation types.

Lib/test/test_long.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ def test__format__(self):
745745
self.assertEqual(format(73, 'z.8b'), '01001001')
746746
self.assertEqual(format(73, 'z#.8b'), '0b01001001')
747747
self.assertEqual(format(300, 'z.8b'), '100101100')
748+
self.assertEqual(format(200, '.8b'), '11001000')
749+
self.assertEqual(format(200, 'z.8b'), '011001000')
748750
self.assertEqual(format(-200, 'z.8b'), '100111000')
749751
self.assertEqual(format(128, 'z.8b'), '010000000')
750752
self.assertEqual(format(-129, 'z.8b'), '101111111')

Python/formatter_unicode.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,33 +1141,35 @@ format_long_internal(PyObject *value, const InternalFormatSpec *format,
11411141
goto done;
11421142
}
11431143

1144-
/* Prepend enough leading zeros (after the sign) */
1144+
/* Prepend enough leading zeros (after sign and prefix) */
11451145

1146-
int sign = PyUnicode_READ_CHAR(tmp, leading_chars_to_skip) == '-';
1146+
int sign = PyUnicode_READ_CHAR(tmp, 0) == '-';
11471147
Py_ssize_t tmp2_len = precision + leading_chars_to_skip + sign;
11481148
Py_ssize_t tmp_len = PyUnicode_GET_LENGTH(tmp);
11491149
Py_ssize_t gap = tmp2_len - tmp_len;
11501150

11511151
if (gap > 0) {
11521152
PyObject *tmp2 = PyUnicode_New(tmp2_len, 127);
1153+
Py_ssize_t value_start = leading_chars_to_skip + sign;
11531154

1154-
if (PyUnicode_CopyCharacters(tmp2, leading_chars_to_skip + gap + sign,
1155-
tmp, leading_chars_to_skip + sign,
1156-
tmp2_len - leading_chars_to_skip - sign) == -1) {
1155+
if (PyUnicode_CopyCharacters(tmp2, value_start + gap,
1156+
tmp, value_start,
1157+
precision) == -1) {
11571158
Py_DECREF(tmp2);
11581159
goto done;
11591160
}
1160-
if (PyUnicode_Fill(tmp2, leading_chars_to_skip + sign, gap, '0') == -1) {
1161+
if (PyUnicode_Fill(tmp2, value_start, gap, '0') == -1) {
11611162
Py_DECREF(tmp2);
11621163
goto done;
11631164
}
1164-
if (sign && PyUnicode_WriteChar(tmp2, leading_chars_to_skip, '-') == -1) {
1165+
if (sign && PyUnicode_WriteChar(tmp2, 0, '-') == -1) {
11651166
Py_DECREF(tmp2);
11661167
goto done;
11671168
}
11681169
if (leading_chars_to_skip
1169-
&& PyUnicode_CopyCharacters(tmp2, 0, tmp, 0,
1170-
leading_chars_to_skip) == -1) {
1170+
&& PyUnicode_CopyCharacters(tmp2, sign, tmp, sign,
1171+
leading_chars_to_skip) == -1)
1172+
{
11711173
Py_DECREF(tmp2);
11721174
goto done;
11731175
}

0 commit comments

Comments
 (0)