Skip to content
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

bugfix: deprecated asyncio usage since python 3.9 #642

Merged
merged 1 commit into from
Apr 26, 2023

Conversation

mattia-lecci
Copy link

See #583 . Reported here:

Specs

  • System: Windows 10
  • Python: 3.10.5
  • pyshark: 0.5.3
  • Environment: venv

Origins of the issue

The issues seems to be related to this update.

The asyncio module has received many new features, usability and performance improvements. Notable changes include:

[...]
The new asyncio.current_task() function returns the currently running Task instance, and the new asyncio.all_tasks() function returns a set of all existing Task instances in a given loop. The Task.current_task() and Task.all_tasks() methods have been deprecated. (Contributed by Andrew Svetlov in bpo-32250.)

From asyncio doc:

Deprecated since version 3.7, will be removed in version 3.9: Do not call this as a task method. Use the asyncio.all_tasks() function instead.

Working fix

Modified pyshark\capture\capture.py:176 in my local .venv from:

if asyncio.Task.all_tasks():

to

if asyncio.all_tasks():

@amitesh-c
Copy link

amitesh-c commented Mar 3, 2023

Have you verified the fix on windows with jupyter notebook ? It seems to throw below error. Any ideas ?

RuntimeError Traceback (most recent call last)
c:\my_projects\23_pyshark_snoop.ipynb Cell 1 in 8
4 # filename = 'data/CIS_TIMEOUT_with_uuids.pcapng'
5 # filename = 'data/test.cap'
7 cap = pyshark.FileCapture(filename)
----> 8 print(cap[0])

hand edited the fix to capture.py

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyshark\capture\file_capture.py:72, in FileCapture.getitem(self, packet_index)
70 while packet_index >= len(self._packets):
71 try:
---> 72 self.next()
73 except StopIteration:
74 # We read the whole file, and there's still not such packet.
75 raise KeyError("Packet of index %d does not exist in capture" % packet_index)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyshark\capture\file_capture.py:62, in FileCapture.next(self)
60 return self._packet_generator.send(None)
61 elif self._current_packet >= len(self._packets):
...
578 if events._get_running_loop() is not None:
--> 579 raise RuntimeError(
580 'Cannot run the event loop while another loop is running')

@mattia-lecci
Copy link
Author

i had the same problem when running it into a jupyter notebook.
In #542 they say that it is expected and pyshark is not supported by jupyter notebooks.
Running it from a normal python script works for me, have you tried that?

@amitesh-c
Copy link

Indeed the issue looks related to asyncio event loop by pyshark conflicts with jupyter notebook. It would be nice to have a solution to this. Normal script works fine. Thanks for the prompt response.

@KimiNewt
Copy link
Owner

KimiNewt commented Apr 26, 2023

Note that this drops compatibility with Python 3.5/6. Since we're way past the EoL for those I'm fine with it, but I'm gonna make the appropriate changes to denote that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants