Fix error when using %% in printf format. (#1713)

For printf formatting, a check is done to see if the number of
arguments matches the number of printf formatting placeholders.

The escape code `%%` that is used for representing a literal `%`
is also counted as a placeholder, but no argument will be provided
for that one.

This makes it impossible to use something like `("%f%%", percentage)`
in the code. In such case, one gets the error:

  `Found 2 printf-patterns (%f, %%), but 1 args were given!`

This commit fixes this behavior by omitting the `%%` from the matches.

Co-authored-by: Maurice Makaay <mmakaay1@xs4all.net>
This commit is contained in:
Maurice Makaay 2021-05-11 17:15:29 +02:00 committed by GitHub
parent 4250af4dd9
commit 7665a220a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -209,14 +209,12 @@ def validate_printf(value):
cfmt = """\
( # start of capture group 1
% # literal "%"
(?: # first option
(?:[-+0 #]{0,5}) # optional flags
(?:\d+|\*)? # width
(?:\.(?:\d+|\*))? # precision
(?:h|l|ll|w|I|I32|I64)? # size
[cCdiouxXeEfgGaAnpsSZ] # type
) | # OR
%%) # literal "%%"
)
""" # noqa
matches = re.findall(cfmt, value[CONF_FORMAT], flags=re.X)
if len(matches) != len(value[CONF_ARGS]):