From d68943041262f5b44f3fadfe4549d793af1d0366 Mon Sep 17 00:00:00 2001 From: Kevin Exton <068ant@gmail.com> Date: Sat, 6 Jul 2019 12:13:02 +1000 Subject: [PATCH 1/3] Added a workaround for Python 3.5 exception on capture eventloop closing. Also added a context manager to the FileCapture class so that managing the asyncio eventloop is easier than explicitly calling close at the end of every script. This commit fixes issue #347 --- src/pyshark/capture/capture.py | 1 + src/pyshark/capture/file_capture.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/pyshark/capture/capture.py b/src/pyshark/capture/capture.py index 3534cab4..e7f4e608 100644 --- a/src/pyshark/capture/capture.py +++ b/src/pyshark/capture/capture.py @@ -426,6 +426,7 @@ async def _cleanup_subprocess(self, process): def close(self): self.eventloop.run_until_complete(self._close_async()) + self.eventloop.close() async def _close_async(self): for process in self._running_processes.copy(): diff --git a/src/pyshark/capture/file_capture.py b/src/pyshark/capture/file_capture.py index 3946b821..cf8c2fd4 100644 --- a/src/pyshark/capture/file_capture.py +++ b/src/pyshark/capture/file_capture.py @@ -86,3 +86,10 @@ def __repr__(self): return '<%s %s>' % (self.__class__.__name__, self.input_filename) else: return '<%s %s (%d packets)>' % (self.__class__.__name__, self.input_filename, len(self._packets)) + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, exc_traceback ): + self.close() + From 408b7212e70f1f069850146a96e08648be58a55d Mon Sep 17 00:00:00 2001 From: Kevin Exton <068ant@gmail.com> Date: Sat, 6 Jul 2019 12:47:32 +1000 Subject: [PATCH 2/3] Explicitly overwrote the close method inherited from capture in FileCapture to avoid breaking inmem capture --- src/pyshark/capture/capture.py | 2 +- src/pyshark/capture/file_capture.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pyshark/capture/capture.py b/src/pyshark/capture/capture.py index e7f4e608..e1d5b8b0 100644 --- a/src/pyshark/capture/capture.py +++ b/src/pyshark/capture/capture.py @@ -426,7 +426,7 @@ async def _cleanup_subprocess(self, process): def close(self): self.eventloop.run_until_complete(self._close_async()) - self.eventloop.close() + #self.eventloop.close() async def _close_async(self): for process in self._running_processes.copy(): diff --git a/src/pyshark/capture/file_capture.py b/src/pyshark/capture/file_capture.py index cf8c2fd4..ad768e6f 100644 --- a/src/pyshark/capture/file_capture.py +++ b/src/pyshark/capture/file_capture.py @@ -87,6 +87,10 @@ def __repr__(self): else: return '<%s %s (%d packets)>' % (self.__class__.__name__, self.input_filename, len(self._packets)) + def close(self): + super(FileCapture,self).close() + self.eventloop.close() + def __enter__(self): return self From 61baa996e359bd72de76327af9db4d156cc190f1 Mon Sep 17 00:00:00 2001 From: Kevin Exton <068ant@gmail.com> Date: Sat, 6 Jul 2019 16:58:42 +1000 Subject: [PATCH 3/3] Removed a residual comment --- src/pyshark/capture/capture.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pyshark/capture/capture.py b/src/pyshark/capture/capture.py index e1d5b8b0..3534cab4 100644 --- a/src/pyshark/capture/capture.py +++ b/src/pyshark/capture/capture.py @@ -426,7 +426,6 @@ async def _cleanup_subprocess(self, process): def close(self): self.eventloop.run_until_complete(self._close_async()) - #self.eventloop.close() async def _close_async(self): for process in self._running_processes.copy():