From b22273ec5d1992b0cbe078b887427ae9977dfb78 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 13 Oct 2017 16:02:23 +0900 Subject: bpo-31672: Fix string.Template accidentally matched non-ASCII identifiers (GH-3872) Pattern `[a-z]` with `IGNORECASE` flag can match to some non-ASCII characters. Straightforward solution for this is using `IGNORECASE | ASCII` flag. But users may subclass `Template` and override only `idpattern`. So we want to avoid changing `Template.flags`. So this commit uses local flag `-i` for `idpattern` and change `[a-z]` to `[a-zA-Z]`. --- Lib/string.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'Lib/string.py') diff --git a/Lib/string.py b/Lib/string.py index b46e60c38f..a3e6d91bb4 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -79,7 +79,11 @@ class Template(metaclass=_TemplateMetaclass): """A string class for supporting $-substitutions.""" delimiter = '$' - idpattern = r'[_a-z][_a-z0-9]*' + # r'[a-z]' matches to non-ASCII letters when used with IGNORECASE, + # but without ASCII flag. We can't add re.ASCII to flags because of + # backward compatibility. So we use local -i flag and [a-zA-Z] pattern. + # See https://bugs.python.org/issue31672 + idpattern = r'(?-i:[_a-zA-Z][_a-zA-Z0-9]*)' braceidpattern = None flags = _re.IGNORECASE -- cgit v1.2.1