From 7665a220a0e18c19d35a4a476e15e3e828ecb08f Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Tue, 11 May 2021 17:15:29 +0200 Subject: [PATCH] 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 --- esphome/components/logger/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/esphome/components/logger/__init__.py b/esphome/components/logger/__init__.py index 5e7383cca7..d865be856b 100644 --- a/esphome/components/logger/__init__.py +++ b/esphome/components/logger/__init__.py @@ -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]):