Skip to content

Commit ad58f9e

Browse files
authored
fix: backslash escaping in serialization to Line protocol (#303)
1 parent 569223c commit ad58f9e

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1. [#283](https://github.com/influxdata/influxdb-client-python/pull/283): Set proxy server in config file
1313
1. [#290](https://github.com/influxdata/influxdb-client-python/pull/290): `Threshold` domain models mapping
1414
1. [#290](https://github.com/influxdata/influxdb-client-python/pull/290): `DashboardService` responses types
15+
1. [#303](https://github.com/influxdata/influxdb-client-python/pull/303): Backslash escaping in serialization to Line protocol
1516

1617
## 1.19.0 [2021-07-09]
1718

influxdb_client/client/write/point.py

-2
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

+10-1
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)