Skip to content

Commit e265425

Browse files
committed
fix(python-driver): centralize string delimiter trim and clean test docstring
1 parent 033d81a commit e265425

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

drivers/python/age/builder.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,16 @@ def visitAgValue(self, ctx:AgtypeParser.AgValueContext):
105105
return valueCtx.accept(self)
106106

107107

108+
@staticmethod
109+
def _stripStringDelimiters(stringToken):
110+
# The STRING token always has surrounding '"' delimiters per the
111+
# Agtype grammar; slice rather than strip('"') so escaped quotes
112+
# at the boundaries are preserved.
113+
return stringToken.getText()[1:-1]
114+
108115
# Visit a parse tree produced by AgtypeParser#StringValue.
109116
def visitStringValue(self, ctx:AgtypeParser.StringValueContext):
110-
# The STRING token always has surrounding '"' delimiters per the
111-
# Agtype grammar. str.strip('"') would also drop any '"' characters
112-
# that are part of the actual data when the value starts or ends
113-
# with an escaped quote, e.g. '"foo \\"bar\\""' -> 'foo \\"bar\\',
114-
# so trim exactly the first and last character instead.
115-
return ctx.STRING().getText()[1:-1]
117+
return self._stripStringDelimiters(ctx.STRING())
116118

117119

118120
# Visit a parse tree produced by AgtypeParser#IntegerValue.
@@ -187,8 +189,7 @@ def visitPair(self, ctx:AgtypeParser.PairContext):
187189
raise AGTypeError(ctx.getText(), "Missing key in object pair")
188190
if agValNode is None:
189191
raise AGTypeError(ctx.getText(), "Missing value in object pair")
190-
# See visitStringValue() for why we slice instead of using strip('"').
191-
return (strNode.getText()[1:-1] , agValNode)
192+
return (self._stripStringDelimiters(strNode), agValNode)
192193

193194

194195
# Visit a parse tree produced by AgtypeParser#array.

drivers/python/test_agtypes.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,11 @@ def test_array_of_mixed_types(self):
246246
self.assertEqual(result[5], {"key": "val"})
247247

248248
def test_string_value_preserves_inner_quotes(self):
249-
"""Issue #2418: visitStringValue must remove only the outer quote
250-
delimiters, not every '"' on either side, otherwise values that end
251-
with an escaped quote (e.g. '"foo \\"bar\\""') lose data."""
249+
"""Issue #2418: preserve escaped boundary quotes when stripping.
250+
251+
visitStringValue must strip only the outer delimiters; otherwise
252+
values like '"foo \\"bar\\""' lose their trailing escaped quote.
253+
"""
252254
self.assertEqual(self.parse('"foo \\"bar\\""'), 'foo \\"bar\\"')
253255
self.assertEqual(self.parse('"\\"leading"'), '\\"leading')
254256
self.assertEqual(self.parse('"trailing\\""'), 'trailing\\"')

0 commit comments

Comments
 (0)