Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pcapkit/dumpkit/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,14 @@ def fields(info: 'Info | MultiDict[Any, Any]') -> 'dict[Any, Any]':
extension header, maps to the :class:`list` of all its values, in
order (GitHub issue :issue:`1484`). The values are left as they
are, so each comes back through :meth:`DictDumper.object_hook` when
written. The keys :func:`excluded` names are left out.
The keys :func:`excluded` names are left out, while a key
renamed for clashing with a builtin name is kept under its original
name (:issue:`2733`).

"""
skip = excluded(info)
pairs = [(key, val) for key, val in info.items(multi=True) if key not in skip]
keymap = getattr(info, '__map__', {})
pairs = [(key, val) for key, val in info.items(multi=True) if keymap.get(key, key) not in skip]
counts = collections.Counter(key for key, _ in pairs)
result = {} # type: dict[Any, Any]
for key, val in pairs:
Expand Down
17 changes: 17 additions & 0 deletions tests/dumpkit/test_common_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ def test_make_dumper_object_hook_conversions_and_fallbacks(self) -> None:
dumper._append_fallback(123, file)
self.assertEqual(file.getvalue(), "{'base': '123'}")

def test_make_dumper_dumps_renamed_builtin_key(self) -> None:
"""A key renamed for clashing with a builtin is kept under its original name (:issue:`2733`)."""
from pcapkit.corekit.infoclass import Info
from pcapkit.dumpkit.common import make_dumper
from pcapkit.foundation.reassembly.data.tcp import Datagram

Dumper = make_dumper(BaseDumper)
dumper = Dumper()

info = Info(items='catalog', normal='value')
self.assertEqual(dumper.object_hook(info), {'items': 'catalog', 'normal': 'value'})

datagram = Datagram(True, 'tcp-id', (0,), b'header', b'payload', b'raw_packet', [])
dumped = dumper.object_hook(datagram)
self.assertIn('packet', dumped)
self.assertEqual(dumped['packet'], b'raw_packet')

def test_make_dumper_object_hook_leaves_strings_and_enum_renderings_unescaped(self) -> None:
"""GitHub issue #772: escaping is now the writer's own job, not this hook's.

Expand Down
Loading