From ae027267057464e7d9b306995686025f1744da91 Mon Sep 17 00:00:00 2001 From: Rahul Ranjan Date: Fri, 13 Nov 2026 06:45:23 +0000 Subject: [PATCH 1/2] fix: don't assign None when a value's group did not participate in the match A rule may match via an alternation branch that does not contain the value's named group (e.g. '^|zz'). The engine then assigned None, which appended a literal None to List values and overwrote Filldown memory, silently losing data. Values are now left unchanged when the group did not participate in the match. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- tests/textfsm_test.py | 37 +++++++++++++++++++++++++++++++++++++ textfsm/parser.py | 10 ++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/tests/textfsm_test.py b/tests/textfsm_test.py index 9177e56..0078bdc 100755 --- a/tests/textfsm_test.py +++ b/tests/textfsm_test.py @@ -749,6 +749,43 @@ def testList(self): result = t.ParseText(data) self.assertListEqual(result, [[['one'], 'two']]) + def testAlternationWithoutValueGroup(self): + """A rule matching via a branch without the value group keeps the value. + + Rules may contain alternations where the value's named group does not + participate in the matched branch. Assigning None in that case appends + None to List values and overwrites Filldown memory. + """ + + # List values do not accumulate None when the group does not match. + tplt = ( + 'Value List boo (on.)\n' + 'Value hoo (tw.)\n\n' + 'Start\n' + ' ^$boo\n' + ' ^$boo|zz -> Next.Record\n\n' + 'EOF' + ) + + t = textfsm.TextFSM(io.StringIO(tplt)) + result = t.ParseText('one\nzz') + self.assertListEqual(result, [[['one'], '']]) + + # Filldown memory is not wiped by a non-matching alternation branch. + tplt = ( + 'Value Filldown boo (on.)\n' + 'Value hoo (tw.)\n\n' + 'Start\n' + ' ^$boo\n' + ' ^$boo|zz -> Next.Record\n' + ' ^$hoo -> Next.Record\n\n' + 'EOF' + ) + + t = textfsm.TextFSM(io.StringIO(tplt)) + result = t.ParseText('one\nzz\ntw2') + self.assertListEqual(result, [['one', ''], ['one', 'tw2']]) + def testNestedMatching(self): """List-type values with nested regex capture groups are parsed correctly. diff --git a/textfsm/parser.py b/textfsm/parser.py index 07efb96..e47880a 100755 --- a/textfsm/parser.py +++ b/textfsm/parser.py @@ -991,13 +991,19 @@ def _AssignVar(self, matched, value): If a record entry is a list then append, otherwise values are replaced. + A named group that did not participate in the match (e.g. the rule + matched via an alternation branch that does not contain the group) + leaves the value unchanged, rather than assigning None - which would + append None to List values and overwrite Filldown memory. + Args: matched: (regexp.match) Named group for each matched value. value: (str) The matched value. """ self._value = self._GetValue(value) - if self._value is not None: - self._value.AssignVar(matched.group(value)) + matched_value = matched.group(value) + if self._value is not None and matched_value is not None: + self._value.AssignVar(matched_value) def _Operations(self, rule, line): """Operators on the data record. From 3a7a3da21cb699abe399d818276175e85863a978 Mon Sep 17 00:00:00 2001 From: Rahul Ranjan Date: Mon, 16 Nov 2026 02:20:44 +0000 Subject: [PATCH 2/2] fix: skip non-participating groups in _CheckLine per review Per review: iterate matched.groupdict() in _CheckLine and skip None groups there, restoring the original _AssignVar body; reword the docstring. Split the Filldown scenario into its own unittest, drop the explicit 'EOF' states, and add the suggested '^${boo}(${hoo})?' optional-group case. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- tests/textfsm_test.py | 27 +++++++++++++++++++++------ textfsm/parser.py | 16 +++++++--------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/tests/textfsm_test.py b/tests/textfsm_test.py index 0078bdc..11939ed 100755 --- a/tests/textfsm_test.py +++ b/tests/textfsm_test.py @@ -763,28 +763,43 @@ def testAlternationWithoutValueGroup(self): 'Value hoo (tw.)\n\n' 'Start\n' ' ^$boo\n' - ' ^$boo|zz -> Next.Record\n\n' - 'EOF' + ' ^$boo|zz -> Next.Record\n' ) t = textfsm.TextFSM(io.StringIO(tplt)) result = t.ParseText('one\nzz') self.assertListEqual(result, [[['one'], '']]) - # Filldown memory is not wiped by a non-matching alternation branch. + # An optional value group that did not participate leaves the + # value unchanged. + tplt = ( + 'Value boo (on.)\n' + 'Value List hoo (tw.)\n\n' + 'Start\n' + ' ^${boo}(${hoo})? -> Next.Record\n' + ) + + t = textfsm.TextFSM(io.StringIO(tplt)) + result = t.ParseText('one\nonetwo') + self.assertListEqual(result, [['one', []], ['one', ['two']]]) + + def testFilldownAlternationWithoutValueGroup(self): + """Filldown memory is not wiped by a non-participating group.""" + tplt = ( 'Value Filldown boo (on.)\n' 'Value hoo (tw.)\n\n' 'Start\n' ' ^$boo\n' ' ^$boo|zz -> Next.Record\n' - ' ^$hoo -> Next.Record\n\n' - 'EOF' + ' ^$hoo -> Next.Record\n' ) t = textfsm.TextFSM(io.StringIO(tplt)) result = t.ParseText('one\nzz\ntw2') - self.assertListEqual(result, [['one', ''], ['one', 'tw2']]) + self.assertListEqual( + result, [['one', ''], ['one', 'tw2'], ['one', '']] + ) def testNestedMatching(self): """List-type values with nested regex capture groups are parsed correctly. diff --git a/textfsm/parser.py b/textfsm/parser.py index e47880a..07dab49 100755 --- a/textfsm/parser.py +++ b/textfsm/parser.py @@ -960,8 +960,10 @@ def _CheckLine(self, line): for rule in self._cur_state: matched = self._CheckRule(rule, line) if matched: - for value in matched.groupdict(): - self._AssignVar(matched, value) + for name, text in matched.groupdict().items(): + if text is None: + continue # Group not part of the matched branch. + self._AssignVar(matched, name) if self._Operations(rule, line): # Not a Continue so check for state transition. @@ -991,19 +993,15 @@ def _AssignVar(self, matched, value): If a record entry is a list then append, otherwise values are replaced. - A named group that did not participate in the match (e.g. the rule - matched via an alternation branch that does not contain the group) - leaves the value unchanged, rather than assigning None - which would - append None to List values and overwrite Filldown memory. + Groups that did not participate in the match leave the value unchanged. Args: matched: (regexp.match) Named group for each matched value. value: (str) The matched value. """ self._value = self._GetValue(value) - matched_value = matched.group(value) - if self._value is not None and matched_value is not None: - self._value.AssignVar(matched_value) + if self._value is not None: + self._value.AssignVar(matched.group(value)) def _Operations(self, rule, line): """Operators on the data record.