From 038f4388bcd85e8ebb194600f480fe779629a401 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 21 Nov 2026 08:15:47 +0000 Subject: [PATCH 1/2] fix: report unsupported backslashes in field keys --- CHANGELOG.md | 1 + influxdb_client/client/write/point.py | 24 ++++++++++++++++++++---- tests/test_point.py | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6d5588b..46b2f2a0 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") \ From 61cf6d730808cfad8b90158a9c780ee2e6c91c5b Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 2 Dec 2026 16:56:21 +0800 Subject: [PATCH 2/2] fix: validate dataframe field keys --- .../client/write/dataframe_serializer.py | 6 +++++- tests/test_WriteApiDataFrame.py | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/influxdb_client/client/write/dataframe_serializer.py b/influxdb_client/client/write/dataframe_serializer.py index c4ae40dd..8f3f1f62 100644 --- a/influxdb_client/client/write/dataframe_serializer.py +++ b/influxdb_client/client/write/dataframe_serializer.py @@ -9,7 +9,8 @@ import re from influxdb_client import WritePrecision -from influxdb_client.client.write.point import _ESCAPE_KEY, _ESCAPE_STRING, _ESCAPE_MEASUREMENT, DEFAULT_WRITE_PRECISION +from influxdb_client.client.write.point import (_ESCAPE_KEY, _ESCAPE_STRING, _ESCAPE_MEASUREMENT, + _escape_field_key, DEFAULT_WRITE_PRECISION) logger = logging.getLogger('influxdb_client.client.write.dataframe_serializer') @@ -179,6 +180,9 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION continue # This column is a field column. + if data_frame.iloc[:, index].notna().any(): + _escape_field_key(key) + # Note: no comma separator is needed for the first field. # It's important to omit it because when the first # field column has no nulls, we don't run the comma-removal diff --git a/tests/test_WriteApiDataFrame.py b/tests/test_WriteApiDataFrame.py index ec667ed9..c6b8ecb8 100644 --- a/tests/test_WriteApiDataFrame.py +++ b/tests/test_WriteApiDataFrame.py @@ -207,6 +207,27 @@ def test_write_field_bool(self): self.assertEqual("measurement status=False 1586046600000000000", points[1]) + def test_rejects_odd_backslashes_in_field_key(self): + from influxdb_client.extras import pd + + data_frame = pd.DataFrame({r"a\ b": [1]}) + + with self.assertRaisesRegex(ValueError, "field key.*odd number of backslashes"): + data_frame_to_list_of_points(data_frame=data_frame, + point_settings=PointSettings(), + data_frame_measurement_name='measurement') + + def test_ignores_odd_backslashes_in_empty_field(self): + from influxdb_client.extras import pd + + data_frame = pd.DataFrame({r"a\ b": [None]}) + + points = data_frame_to_list_of_points(data_frame=data_frame, + point_settings=PointSettings(), + data_frame_measurement_name='measurement') + + self.assertEqual([], points) + def test_escaping_measurement(self): from influxdb_client.extras import pd, np