# PyUnicode\_FromFormat allow \`%%\` format with precision?

**URL:** <https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873>\
**Category:** Core Development\
**Created:** [August 2, 2022, 4:01am UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873 "2022-08-02T04:01:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![philg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/philg/32/8489_2.png) [@philg](https://discuss.python.org/u/philg)\
**Post date:** [August 2, 2022, 4:01am UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873/1 "2022-08-02T04:01:07Z")

</div>

The `%%` format in PyUnicode\_FromFormat recognizes the zero flag and width but not precision:

```nohighlight
// Recognized
PyUnicode_FromFormat( "%% %s", "abc"); // "% abc"
PyUnicode_FromFormat( "%0% %s", "abc"); // "% abc"
PyUnicode_FromFormat("%00% %s", "abc"); // "% abc"
PyUnicode_FromFormat( "%2% %s", "abc"); // "% abc"
PyUnicode_FromFormat("%02% %s", "abc"); // "% abc"

// Not recognized
PyUnicode_FromFormat("%.0% %s", "abc"); // "%.0% %s"
PyUnicode_FromFormat("%.2% %s", "abc"); // "%.2% %s"

```

Can this be changed to allow precision? The original intention was to fix a crash before the `%%` format was supported, see [Issue #10829: Refactor PyUnicode\_FromFormat() · python/cpython@9686545 · GitHub](https://github.com/python/cpython/commit/968654515f5484447d0f28fdaf5c5d7f5495b426).

---

<div class="post-metadata">

**Author:** ![vstinner](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vstinner/32/15130_2.png) [@vstinner](https://discuss.python.org/u/vstinner)\
**Post date:** [August 2, 2022, 2:08pm UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873/2 "2022-08-02T14:08:45Z")

</div>

I don’t see something other than `%%` should be allowed. What’s the use case? Can’t you fix your format string instead?

---

<div class="post-metadata">

**Author:** ![philg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/philg/32/8489_2.png) [@philg](https://discuss.python.org/u/philg)\
**Post date:** [August 6, 2022, 10:59am UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873/3 "2022-08-06T10:59:59Z")

</div>

There is no use case for allowing it but not allowing it is inconsistent with the other format specifiers (the zero flag, width, and precision is always allowed even if it has no effect).

I’d like to make this consistent, not because precision is useful but because there’s no reason for the inconsistency anymore.

---

<div class="post-metadata">

**Author:** ![ericvsmith](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@ericvsmith](https://discuss.python.org/u/ericvsmith)\
**Post date:** [August 6, 2022, 12:26pm UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873/4 "2022-08-06T12:26:43Z")

</div>

I don’t think we should add something without a use case, just for consistencies sake. If no one has ever needed it; why add to the maintenance burden?

---

<div class="post-metadata">

**Author:** ![gpshead](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gpshead/32/54_2.png) [@gpshead](https://discuss.python.org/u/gpshead)\
**Post date:** [August 7, 2022, 12:20am UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873/5 "2022-08-07T00:20:47Z")

</div>

If anything I’d prefer `%%` to not support numbers between the % signs at all given they have no meaning. But only if that is less of a maintenance burden. Nobody _should_ intentionally be using that, most think of `%%` as being a way to escape % to get a % sign.

---

<div class="post-metadata">

**Author:** ![philg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/philg/32/8489_2.png) [@philg](https://discuss.python.org/u/philg)\
**Post date:** [August 8, 2022, 5:04am UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873/6 "2022-08-08T05:04:10Z")

</div>

I think I approached this the wrong way. My overall goal is to improve PyUnicode\_FromFormat and there are two places where there is code left over from a previous version of the function that is no longer necessary:

- Special case for `%%` with precision: [cpython/unicodeobject.c at 330f1d58282517bdf1f19577ab9317fa9810bf95 · python/cpython · GitHub](https://github.com/python/cpython/blob/330f1d58282517bdf1f19577ab9317fa9810bf95/Objects/unicodeobject.c#L2395-L2398)

- Special case for incomplete format specifier: [cpython/unicodeobject.c at 330f1d58282517bdf1f19577ab9317fa9810bf95 · python/cpython · GitHub](https://github.com/python/cpython/blob/330f1d58282517bdf1f19577ab9317fa9810bf95/Objects/unicodeobject.c#L2400-L2403)

Both of these special cases were introduced when parsing format flags were a separate function and not handling them would crash Python ([Issue #10829: Refactor PyUnicode\_FromFormat() · python/cpython@9686545 · GitHub](https://github.com/python/cpython/commit/968654515f5484447d0f28fdaf5c5d7f5495b426)).

Since then PyUnicode\_FromFormat was refactored and not handling the special cases would not cause a crash:

- `%%` with precision would ignore precision ([cpython/unicodeobject.c at 330f1d58282517bdf1f19577ab9317fa9810bf95 · python/cpython · GitHub](https://github.com/python/cpython/blob/330f1d58282517bdf1f19577ab9317fa9810bf95/Objects/unicodeobject.c#L2619-L2622))

- Incomplete format specifier would be unrecognized ([cpython/unicodeobject.c at 330f1d58282517bdf1f19577ab9317fa9810bf95 · python/cpython · GitHub](https://github.com/python/cpython/blob/330f1d58282517bdf1f19577ab9317fa9810bf95/Objects/unicodeobject.c#L2624-L2633))

I believe that removing the special cases would reduce the maintenance burden (less code to think about). If that’s the case can they be removed?

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [August 8, 2022, 12:10pm UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873/7 "2022-08-08T12:10:15Z")

</div>

You are right. Yesterday I wrote a code, and today created a PR: [gh-95781: More strict format string checking in PyUnicode\_FromFormatV() by serhiy-storchaka · Pull Request #95784 · python/cpython · GitHub](https://github.com/python/cpython/pull/95784).

---

<div class="post-metadata">

**Author:** ![philg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/philg/32/8489_2.png) [@philg](https://discuss.python.org/u/philg)\
**Post date:** [August 8, 2022, 12:52pm UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873/8 "2022-08-08T12:52:56Z")

</div>

Ah okay, very cool! I thought a backwards compatible change had the highest chances of getting accepted but this solves all my concerns. Thanks!

---

<div class="post-metadata">

**Author:** ![vstinner](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vstinner/32/15130_2.png) [@vstinner](https://discuss.python.org/u/vstinner)\
**Post date:** [August 8, 2022, 5:21pm UTC](https://discuss.python.org/t/pyunicode-fromformat-allow-format-with-precision/17873/9 "2022-08-08T17:21:32Z")

</div>

I agree. Rejecting invalid format strings is better than copying it unchanged! The old behavior was just weird wand wrong.
