Skip to content

Commit 7e70fd2

Browse files
committed
Added section about packet data lifetime
1 parent 6205a90 commit 7e70fd2

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ while not reader.is_exhausted():
6868
pass
6969
```
7070

71+
### Packet Data Lifetime
72+
73+
> **Note:** Accessing `packet.data` returns a copy of the raw packet bytes as a Python `bytes`
74+
> object. The copy is made each time the property is accessed. Internally, the `Packet` holds a
75+
> pointer into the `PacketReader`'s file buffer, so always access `.data` while the reader is
76+
> still alive:
77+
78+
```python
79+
# Safe: access .data while reader exists
80+
reader = fpcap.PacketReader("capture.pcap")
81+
for packet in reader:
82+
raw_bytes = packet.data # copies bytes — safe to keep
83+
84+
# Unsafe: accessing .data after the reader is destroyed
85+
reader = fpcap.PacketReader("capture.pcap")
86+
packets = list(reader)
87+
del reader
88+
packets[0].data # undefined behavior — reader's buffer is freed
89+
```
90+
7191
### Writing packets
7292

7393
Copy packets from one file to another:

0 commit comments

Comments
 (0)