Skip to content

Commit df7a657

Browse files
committed
fix: backslash escaping in serialization to Line protocol
1 parent cf21862 commit df7a657

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

influxdb_client/client/write/point.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
DEFAULT_WRITE_PRECISION = WritePrecision.NS
1919

2020
_ESCAPE_MEASUREMENT = str.maketrans({
21-
'\\': r'\\', # Note: this is wrong. Backslashes are not escaped like this in measurements.
2221
',': r'\,',
2322
' ': r'\ ',
2423
'\n': r'\n',
@@ -27,7 +26,6 @@
2726
})
2827

2928
_ESCAPE_KEY = str.maketrans({
30-
'\\': r'\\', # Note: this is wrong. Backslashes are not escaped like this in keys.
3129
',': r'\,',
3230
'=': r'\=',
3331
' ': r'\ ',

tests/test_point.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def test_lineprotocol_encode(self):
267267
point._tags = {
268268
"empty_tag": "",
269269
"none_tag": None,
270-
"backslash_tag": "C:\\",
270+
"backslash_tag": "C:\\\\",
271271
"integer_tag": 2,
272272
"string_tag": "hello"
273273
}
@@ -379,6 +379,15 @@ def test_unsupported_field_type(self):
379379

380380
self.assertEqual('Type: "<class \'pytz.UTC\'>" of field: "level" is not supported.', f'{exception}')
381381

382+
def test_backslash(self):
383+
point = Point.from_dict({"measurement": "test",
384+
"tags": {"tag1": "value1", "tag2": "value\2", "tag3": "value\\3",
385+
"tag4": r"value\4", "tag5": r"value\\5"}, "time": 1624989000000000000,
386+
"fields": {"value": 10}}, write_precision=WritePrecision.NS)
387+
self.assertEqual(
388+
"test,tag1=value1,tag2=value\2,tag3=value\\3,tag4=value\\4,tag5=value\\\\5 value=10i 1624989000000000000",
389+
point.to_line_protocol())
390+
382391

383392
if __name__ == '__main__':
384393
unittest.main()

0 commit comments

Comments
 (0)