From 809fba5f838b3f80aec41c2d8a1716ce43be0593 Mon Sep 17 00:00:00 2001 From: Rahul Ranjan Date: Fri, 13 Nov 2026 06:11:25 +0000 Subject: [PATCH 1/2] fix: don't process Value names for backslash escapes in re.sub A backslash in a Value name escaped the re.sub() replacement as a raw re.PatternError (e.g. 'Value beer\ol1 (\S+)'), instead of a TextFSMTemplateError when the value is used. A callable replacement keeps the name literal. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- tests/textfsm_test.py | 12 ++++++++++++ textfsm/parser.py | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/textfsm_test.py b/tests/textfsm_test.py index 9177e56..57a4897 100755 --- a/tests/textfsm_test.py +++ b/tests/textfsm_test.py @@ -81,6 +81,18 @@ def testFSMValue(self): textfsm.TextFSMTemplateError, v.Parse, 'Value Required beer' ) + # A backslash in the value name is no longer processed as a backslash + # escape by the re.sub() replacement (raw re.PatternError); the + # template is now the literal expansion, and using such a name in a + # rule reports a clean template error instead. + v = textfsm.TextFSMValue(options_class=textfsm.TextFSMOptions) + v.Parse(r'Value beer\ol1 (\S+)') + self.assertEqual(v.template, r'(?P\S+)') + tplt = 'Value beer\\ol1 (\\S+)\n\nStart\n ^$beer\\ol1x -> Next\n' + self.assertRaises( + textfsm.TextFSMTemplateError, textfsm.TextFSM, io.StringIO(tplt) + ) + # Unbalanced parenthesis can exist if within square "[]" braces. v = textfsm.TextFSMValue(options_class=textfsm.TextFSMOptions) v.Parse('Value beer (boo[(]hoo)') diff --git a/textfsm/parser.py b/textfsm/parser.py index 07efb96..8f9b3bd 100755 --- a/textfsm/parser.py +++ b/textfsm/parser.py @@ -319,7 +319,11 @@ def Parse(self, value): except re.error as exc: raise TextFSMTemplateError(str(exc)) from exc - self.template = re.sub(r'^\(', '(?P<%s>' % self.name, self.regex) + # A callable replacement, so that the value name is not interpreted + # for backslash escapes (or group references) by re.sub(). + self.template = re.sub( + r'^\(', lambda match: '(?P<%s>' % self.name, self.regex + ) # Compile and store the regex object only on List-type values for use in # nested matching From af913649f2dfd9a949eaf40d03edad7853284be5 Mon Sep 17 00:00:00 2001 From: Rahul Ranjan Date: Mon, 16 Nov 2026 02:17:06 +0000 Subject: [PATCH 2/2] fix: reject backslash Value names and build the template directly Per review: a backslash in a Value name is now rejected at name-check time, and the template is built by slicing the already-validated leading '(' instead of re.sub() with a callable replacement. Tests trimmed to the name-check error and the full-template case. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- tests/textfsm_test.py | 11 +++++------ textfsm/parser.py | 8 ++------ 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/tests/textfsm_test.py b/tests/textfsm_test.py index 57a4897..25e2de4 100755 --- a/tests/textfsm_test.py +++ b/tests/textfsm_test.py @@ -81,13 +81,12 @@ def testFSMValue(self): textfsm.TextFSMTemplateError, v.Parse, 'Value Required beer' ) - # A backslash in the value name is no longer processed as a backslash - # escape by the re.sub() replacement (raw re.PatternError); the - # template is now the literal expansion, and using such a name in a - # rule reports a clean template error instead. + # A backslash in the value name is rejected at name-check time. v = textfsm.TextFSMValue(options_class=textfsm.TextFSMOptions) - v.Parse(r'Value beer\ol1 (\S+)') - self.assertEqual(v.template, r'(?P\S+)') + self.assertRaises( + textfsm.TextFSMTemplateError, v.Parse, r'Value beer\ol1 (\S+)' + ) + tplt = 'Value beer\\ol1 (\\S+)\n\nStart\n ^$beer\\ol1x -> Next\n' self.assertRaises( textfsm.TextFSMTemplateError, textfsm.TextFSM, io.StringIO(tplt) diff --git a/textfsm/parser.py b/textfsm/parser.py index 8f9b3bd..3a634f6 100755 --- a/textfsm/parser.py +++ b/textfsm/parser.py @@ -300,7 +300,7 @@ def Parse(self, value): self.name = value_line[1] self.regex = ' '.join(value_line[2:]) - if len(self.name) > self.max_name_len: + if len(self.name) > self.max_name_len or '\\' in self.name: raise TextFSMTemplateError( "Invalid Value name '%s' or name too long." % self.name ) @@ -319,11 +319,7 @@ def Parse(self, value): except re.error as exc: raise TextFSMTemplateError(str(exc)) from exc - # A callable replacement, so that the value name is not interpreted - # for backslash escapes (or group references) by re.sub(). - self.template = re.sub( - r'^\(', lambda match: '(?P<%s>' % self.name, self.regex - ) + self.template = '(?P<%s>%s' % (self.name, self.regex[1:]) # Compile and store the regex object only on List-type values for use in # nested matching