Skip to content

Add pickle test for PacketList [ready] #3113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2021
Merged
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
19 changes: 18 additions & 1 deletion scapy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,24 @@ class Sized(object): # type: ignore
if sys.version_info >= (3, 7):
from typing import NamedTuple
else:
NamedTuple = lambda name, params: collections.namedtuple(name, list(x[0] for x in params)) # noqa: E501
# Hack for Python < 3.7 - Implement NamedTuple pickling
def _unpickleNamedTuple(name, len_params, *args):
return collections.namedtuple(
name,
args[:len_params]
)(*args[len_params:])

def NamedTuple(name, params):
tup_params = tuple(x[0] for x in params)
cls = collections.namedtuple(name, tup_params)

class _NT(cls):
def __reduce__(self):
"""Used by pickling methods"""
return (_unpickleNamedTuple,
(name, len(tup_params)) + tup_params + tuple(self))
_NT.__name__ = cls.__name__
return _NT

# Python 3.8 Only
if sys.version_info >= (3, 8):
Expand Down
22 changes: 7 additions & 15 deletions scapy/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def __init__(self,
self.post_transforms = [post_transform]

_PickleType = Tuple[
bytes,
Union[EDecimal, float],
Optional[Union[EDecimal, float, None]],
Optional[int],
Expand All @@ -195,31 +194,24 @@ def __init__(self,
]

def __reduce__(self):
# type: () -> Tuple[Type[Packet], Tuple[()], Packet._PickleType]
# type: () -> Tuple[Type[Packet], Tuple[bytes], Packet._PickleType]
"""Used by pickling methods"""
return (self.__class__, (), (
self.build(),
return (self.__class__, (self.build(),), (
self.time,
self.sent_time,
self.direction,
self.sniffed_on,
self.wirelen,
))

def __getstate__(self):
# type: () -> Packet._PickleType
"""Mark object as pickable"""
return self.__reduce__()[2]

def __setstate__(self, state):
# type: (Packet._PickleType) -> Packet
"""Rebuild state using pickable methods"""
self.__init__(state[0]) # type: ignore
self.time = state[1]
self.sent_time = state[2]
self.direction = state[3]
self.sniffed_on = state[4]
self.wirelen = state[5]
self.time = state[0]
self.sent_time = state[1]
self.direction = state[2]
self.sniffed_on = state[3]
self.wirelen = state[4]
return self

def __deepcopy__(self,
Expand Down
16 changes: 16 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -4187,6 +4187,22 @@ srl, rl = pl.sr(lookahead=None)
assert len(srl) == 1
assert len(rl) == 7

= pickle test
import pickle
import io

srl, rl = PacketList([Raw(b"1"), Raw(b"1"), Raw(b"2"), Raw(b"3"), Raw(b"4"), Raw(b"3"), Raw(b"1"), Raw(b"1"), Raw(b"4")]).sr()
assert len(srl) == 4

f = io.BytesIO()

pickle.dump(srl, f)

unp = pickle.loads(f.getvalue())

assert len(unp) == len(srl)
assert all(bytes(a[0]) == bytes(b[0]) for a, b in zip(unp, srl))

= plot()

import mock
Expand Down