Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/ensure-close-async
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiNewt authored Jul 20, 2024
2 parents 7142c5b + 5bef368 commit 4a729cf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/pyshark/capture/capture.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import contextlib
import inspect
import os
import threading
import subprocess
Expand Down Expand Up @@ -292,7 +293,10 @@ async def _go_through_packets_from_fd(self, fd, packet_callback, packet_count=No
if packet:
packets_captured += 1
try:
packet_callback(packet)
if inspect.iscoroutinefunction(packet_callback):
await packet_callback(packet)
else:
packet_callback(packet)
except StopCapture:
self._log.debug("User-initiated capture stop in callback")
break
Expand Down
1 change: 1 addition & 0 deletions src/pyshark/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# (Linux): /usr/sbin/tshark
# (Linux): /usr/lib/tshark/tshark
# (Linux): /usr/local/bin/tshark
# (Linux): /bin/tshark
# (Windows): %ProgramFiles%\Wireshark\tshark.exe
# (Windows): %ProgramFiles(x86)%\Wireshark\tshark.exe
tshark_path = C:\Program Files\Wireshark\tshark.exe
Expand Down
2 changes: 1 addition & 1 deletion src/pyshark/tshark/output_parser/tshark_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, parse_summaries=False):
self._psml_structure = None

async def get_packets_from_stream(self, stream, existing_data, got_first_packet=True):
if self._parse_summaries:
if self._parse_summaries and self._psml_structure is None:
existing_data = await self._get_psml_struct(stream)
return await super().get_packets_from_stream(stream, existing_data, got_first_packet=got_first_packet)

Expand Down
2 changes: 1 addition & 1 deletion src/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="pyshark",
version="0.6",
version="0.6.1",
packages=find_packages(),
package_data={'': ['*.ini', '*.pcapng']},
install_requires=['lxml', 'termcolor', 'packaging', 'appdirs'],
Expand Down
8 changes: 8 additions & 0 deletions tests/test_cap_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def test_packet_callback_called_for_each_packet(lazy_simple_capture):
assert mock_callback.call_count == 24


def test_async_packet_callback_called_for_each_packet(lazy_simple_capture):
# Test cap has 24 packets
mock_callback = mock.AsyncMock()
lazy_simple_capture.apply_on_packets(mock_callback)
assert mock_callback.call_count == 24
mock_callback.assert_awaited()


def test_apply_on_packet_stops_on_timeout(lazy_simple_capture):
def wait(pkt):
time.sleep(5)
Expand Down

0 comments on commit 4a729cf

Please sign in to comment.