Skip to content

Commit 037811c

Browse files
authored
Fix parsing empty attribute groups (#650)
1 parent d49d5aa commit 037811c

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

src/pyipp/parser.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -289,26 +289,28 @@ def parse( # noqa: PLR0912, PLR0915
289289

290290
attribute_key = "unsupported-attributes"
291291
offset += 1
292+
else:
293+
attribute, new_offset = parse_attribute(
294+
raw_data,
295+
offset,
296+
previous_attribute_name,
297+
)
292298

293-
attribute, new_offset = parse_attribute(
294-
raw_data, offset, previous_attribute_name,
295-
)
296-
297-
# if attribute has a name -> add it
298-
# if attribute doesn't have a name -> it is part of an array
299-
if attribute["name"]:
300-
tmp_data[attribute["name"]] = attribute["value"]
301-
previous_attribute_name = attribute["name"]
302-
elif previous_attribute_name:
303-
# check if attribute is already an array
304-
# else convert it to an array
305-
if isinstance(tmp_data[previous_attribute_name], list):
306-
tmp_data[previous_attribute_name].append(attribute["value"])
307-
else:
308-
tmp_value = tmp_data[previous_attribute_name]
309-
tmp_data[previous_attribute_name] = [tmp_value, attribute["value"]]
310-
311-
offset = new_offset
299+
# if attribute has a name -> add it
300+
# if attribute doesn't have a name -> it is part of an array
301+
if attribute["name"]:
302+
tmp_data[attribute["name"]] = attribute["value"]
303+
previous_attribute_name = attribute["name"]
304+
elif previous_attribute_name:
305+
# check if attribute is already an array
306+
# else convert it to an array
307+
if isinstance(tmp_data[previous_attribute_name], list):
308+
tmp_data[previous_attribute_name].append(attribute["value"])
309+
else:
310+
tmp_value = tmp_data[previous_attribute_name]
311+
tmp_data[previous_attribute_name] = [tmp_value, attribute["value"]]
312+
313+
offset = new_offset
312314

313315
if isinstance(data[attribute_key], list):
314316
data[attribute_key].append(tmp_data)

tests/__snapshots__/test_parser.ambr

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,31 @@
446446
),
447447
})
448448
# ---
449+
# name: test_parse_empty_attribute_group
450+
dict({
451+
'data': b'',
452+
'jobs': list([
453+
]),
454+
'operation-attributes': dict({
455+
'attributes-charset': 'utf-8',
456+
'attributes-natural-language': 'en-US',
457+
'printer-uri': 'ipp://printer.example.com:361/ipp/print',
458+
'requesting-user-name': 'PythonIPP',
459+
}),
460+
'printers': list([
461+
]),
462+
'request-id': 1,
463+
'status-code': 11,
464+
'unsupported-attributes': list([
465+
dict({
466+
}),
467+
]),
468+
'version': tuple(
469+
2,
470+
0,
471+
),
472+
})
473+
# ---
449474
# name: test_parse_epson_xp6000
450475
dict({
451476
'data': b'',
165 Bytes
Binary file not shown.

tests/test_parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,13 @@ def test_parse_kyocera_ecosys_m2540dn(snapshot: SnapshotAssertion) -> None:
163163

164164
result = parser.parse(response)
165165
assert result == snapshot
166+
167+
168+
def test_parse_empty_attribute_group(snapshot: SnapshotAssertion) -> None:
169+
"""Test the parse method against a sample response with an empty attribute group."""
170+
response = load_fixture_binary(
171+
"get-printer-attributes-empty-attribute-group.bin",
172+
)
173+
174+
result = parser.parse(response)
175+
assert result == snapshot

0 commit comments

Comments
 (0)