diff --git a/CHANGELOG.md b/CHANGELOG.md index d99a9f8c..adc34e5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Bug Fixes +1. [#660](https://github.com/influxdata/influxdb-client-python/issues/660): Raise a clear error when serializing a field key with an odd number of backslashes before a space. 1. [#706](https://github.com/influxdata/influxdb-client-python/pull/706): Use logger instead logging. 1. [#710](https://github.com/influxdata/influxdb-client-python/pull/710): Stop `default_tags` from one client leaking into other clients' `WriteApi` (shared mutable default `PointSettings`). 1. [#713](https://github.com/influxdata/influxdb-client-python/pull/713): Replace invalid tuple type annotations `(str, str, str)` with `Tuple[str, str, str]` in batching callbacks, examples and docs. diff --git a/influxdb_client/client/write/point.py b/influxdb_client/client/write/point.py index cc95d204..c551feb1 100644 --- a/influxdb_client/client/write/point.py +++ b/influxdb_client/client/write/point.py @@ -1,6 +1,7 @@ """Point data structure to represent LineProtocol.""" import math +import re import warnings from builtins import int from datetime import datetime, timedelta, timezone @@ -36,6 +37,8 @@ '\\': r'\\', }) +_ODD_BACKSLASHES_BEFORE_SPACE = re.compile(r"(? str: return str(tag).translate(escape_list) +def _escape_field_key(field) -> str: + key = str(field) + # Escaping the space adds a backslash. If the original run is odd, the + # server consumes the resulting backslashes in pairs and sees a delimiter. + if "\\ " in key and _ODD_BACKSLASHES_BEFORE_SPACE.search(key): + raise ValueError(f"Cannot serialize field key {key!r}: an odd number of backslashes before a space " + "is not supported by InfluxDB line protocol.") + return _escape_key(key) + + def _escape_tag_value(value) -> str: ret = _escape_key(value) if ret.endswith('\\'): diff --git a/tests/test_point.py b/tests/test_point.py index e799ae9c..7bd2ee4b 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -114,6 +114,24 @@ def test_FieldEscape(self): self.assertEqual("h2o,location=europe level=\"string esc\\\"ape value\"", point.to_line_protocol()) + def test_field_key_rejects_odd_backslashes_before_space(self): + for key, value in [(r"this doesnt \ work", 666), (r"a\\\ b", 1.5), + (r"a\ b", True), (r"a\ b", "value")]: + with self.subTest(key=key, value=value): + point = Point.from_dict({"measurement": "test", "fields": {key: value}}) + with self.assertRaisesRegex(ValueError, "field key.*odd number of backslashes"): + point.to_line_protocol() + + def test_field_key_escapes_valid_spaces_and_backslashes(self): + for key, escaped in [("a b", r"a\ b"), (r"a\\ b", r"a\\\ b"), (r"a\b", r"a\b")]: + with self.subTest(key=key): + point = Point("test").field(key, 1) + self.assertEqual(f"test {escaped}=1i", point.to_line_protocol()) + + def test_invalid_field_key_is_ignored_when_value_is_omitted(self): + point = Point("test").field(r"a\ b", None).field(r"c\ d", float("nan")).field("valid", 1) + self.assertEqual("test valid=1i", point.to_line_protocol()) + def test_Time(self): point = Point.measurement("h2o") \ .tag("location", "europe") \