aboutsummaryrefslogtreecommitdiff
path: root/pw_trace/py
diff options
context:
space:
mode:
Diffstat (limited to 'pw_trace/py')
-rw-r--r--pw_trace/py/BUILD.gn1
-rwxr-xr-xpw_trace/py/pw_trace/trace.py22
-rw-r--r--pw_trace/py/setup.py18
-rwxr-xr-xpw_trace/py/trace_test.py12
4 files changed, 22 insertions, 31 deletions
diff --git a/pw_trace/py/BUILD.gn b/pw_trace/py/BUILD.gn
index dd50814ce..733ad1c10 100644
--- a/pw_trace/py/BUILD.gn
+++ b/pw_trace/py/BUILD.gn
@@ -20,7 +20,6 @@ pw_python_package("py") {
setup = [
"pyproject.toml",
"setup.cfg",
- "setup.py",
]
sources = [
"pw_trace/__init__.py",
diff --git a/pw_trace/py/pw_trace/trace.py b/pw_trace/py/pw_trace/trace.py
index 571d66f0c..7535520c9 100755
--- a/pw_trace/py/pw_trace/trace.py
+++ b/pw_trace/py/pw_trace/trace.py
@@ -73,16 +73,21 @@ def event_has_trace_id(event_type):
def decode_struct_fmt_args(event):
"""Decodes the trace's event data for struct-formatted data"""
args = {}
- # we assume all data is packed, little-endian ordering if not specified
+ # We assume all data is packed, little-endian ordering if not specified.
struct_fmt = event.data_fmt[len("@pw_py_struct_fmt:") :]
if not struct_fmt.startswith(_ORDERING_CHARS):
struct_fmt = "<" + struct_fmt
try:
- # needed in case the buffer is larger than expected
+ # Assert is needed in case the buffer is larger than expected.
assert struct.calcsize(struct_fmt) == len(event.data)
items = struct.unpack_from(struct_fmt, event.data)
for i, item in enumerate(items):
- args["data_" + str(i)] = item
+ # Try to decode the item in case it is a byte array (string) since
+ # the JSON lib cannot serialize byte arrays.
+ try:
+ args["data_" + str(i)] = item.decode()
+ except (UnicodeDecodeError, AttributeError):
+ args["data_" + str(i)] = item
except (AssertionError, struct.error):
args["error"] = (
f"Mismatched struct/data format {event.data_fmt} "
@@ -98,7 +103,7 @@ def decode_map_fmt_args(event):
args = {}
fmt = event.data_fmt[len("@pw_py_map_fmt:") :]
- # we assume all data is packed, little-endian ordering if not specified
+ # We assume all data is packed, little-endian ordering if not specified.
if not fmt.startswith(_ORDERING_CHARS):
fmt = '<' + fmt
@@ -115,11 +120,16 @@ def decode_map_fmt_args(event):
args["error"] = f"Invalid map format {event.data_fmt}"
else:
try:
- # needed in case the buffer is larger than expected
+ # Assert is needed in case the buffer is larger than expected.
assert struct.calcsize(fmt_bytes) == len(event.data)
items = struct.unpack_from(fmt_bytes, event.data)
for i, item in enumerate(items):
- args[names[i]] = item
+ # Try to decode the item in case it is a byte array (string)
+ # since the JSON lib cannot serialize byte arrays.
+ try:
+ args[names[i]] = item.decode()
+ except (UnicodeDecodeError, AttributeError):
+ args[names[i]] = item
except (AssertionError, struct.error):
args["error"] = (
f"Mismatched map/data format {event.data_fmt} "
diff --git a/pw_trace/py/setup.py b/pw_trace/py/setup.py
deleted file mode 100644
index 9e3cda1f5..000000000
--- a/pw_trace/py/setup.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Copyright 2021 The Pigweed Authors
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may not
-# use this file except in compliance with the License. You may obtain a copy of
-# the License at
-#
-# https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations under
-# the License.
-"""pw_trace"""
-
-import setuptools # type: ignore
-
-setuptools.setup() # Package definition in setup.cfg
diff --git a/pw_trace/py/trace_test.py b/pw_trace/py/trace_test.py
index 60249623d..181824553 100755
--- a/pw_trace/py/trace_test.py
+++ b/pw_trace/py/trace_test.py
@@ -194,8 +194,8 @@ class TestTraceGenerateJson(unittest.TestCase):
label="counter",
timestamp_us=10,
has_data=True,
- data_fmt="@pw_py_struct_fmt:Hl",
- data=struct.pack("<Hl", 5, 2),
+ data_fmt="@pw_py_struct_fmt:Hl3s",
+ data=struct.pack("<Hl3s", 5, 2, b'abc'),
)
json_lines = trace.generate_trace_json([event])
self.assertEqual(1, len(json_lines))
@@ -207,7 +207,7 @@ class TestTraceGenerateJson(unittest.TestCase):
"name": "counter",
"ts": 10,
"s": "p",
- "args": {"data_0": 5, "data_1": 2},
+ "args": {"data_0": 5, "data_1": 2, "data_2": 'abc'},
},
)
@@ -298,8 +298,8 @@ class TestTraceGenerateJson(unittest.TestCase):
label="label",
timestamp_us=10,
has_data=True,
- data_fmt="@pw_py_map_fmt:{Field: l, Field2: l }",
- data=struct.pack("<ll", 20, 40),
+ data_fmt="@pw_py_map_fmt:{Field: l, Field2: l , Field3: 3s}",
+ data=struct.pack("<ll3s", 20, 40, b'abc'),
)
json_lines = trace.generate_trace_json([event])
self.assertEqual(1, len(json_lines))
@@ -311,7 +311,7 @@ class TestTraceGenerateJson(unittest.TestCase):
"name": "label",
"ts": 10,
"s": "p",
- "args": {"Field": 20, "Field2": 40},
+ "args": {"Field": 20, "Field2": 40, "Field3": 'abc'},
},
)