etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
576 stars 196 forks source link

Python 3.10 compatibility #413

Open mheppner opened 2 years ago

mheppner commented 2 years ago

String comparison between the Python tuple version is not working with Python 3.10.

Located here:

IS_PYTHON_344_PLUS = platform.python_version_tuple() >= ('3', '4', '4')

platform.python_version_tuple() returns ('3', '10', '0') and '10' is not greater than '4'.

This causes the following error in this section:

AttributeError: module 'asyncio' has no attribute 'async'
caused by <class 'AttributeError'>: module 'asyncio' has no attribute 'async'

I think the comparison should be something like this instead: IS_PYTHON_344_PLUS = [int(v) for v in platform.python_version_tuple()] < [3, 4, 4]

antonc42 commented 2 years ago

Thanks @mheppner and @flaviut.

I ran into this issue as soon as I upgraded to Python 3.10. I applied the patch that @flaviut made to the latest release version (v4.4.12) if anyone is interested: https://github.com/antonc42/pysnmp/tree/antonc42-4.4.12-fix-python3.10

You can install with: python3 -m pip install --force-reinstall git+https://github.com/antonc42/pysnmp@antonc42-4.4.12-fix-python3.10

Or put this in your requirements.txt: git+https://github.com/antonc42/pysnmp@antonc42-4.4.12-fix-python3.10

Or put this in your install_requires in setup.py if your module depends on pysnmp: 'pysnmp @ git+https://github.com/antonc42/pysnmp@antonc42-4.4.12-fix-python3.10'

roumano commented 2 years ago

I just tested it for the https://github.com/home-assistant/core/issues/60350

But it's still crash for me :

janv. 10 18:37:13 rasp hass[488]: 2022-01-10 18:37:13 ERROR (MainThread) [aiohttp.server] Error handling request
janv. 10 18:37:13 rasp hass[488]: Traceback (most recent call last):
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/pysnmp/carrier/asyncio/dgram/base.py", line 93, in openClientMode
janv. 10 18:37:13 rasp hass[488]:     self._lport = getattr(asyncio, 'async')(c)
janv. 10 18:37:13 rasp hass[488]: AttributeError: module 'asyncio' has no attribute 'async'
janv. 10 18:37:13 rasp hass[488]: During handling of the above exception, another exception occurred:
janv. 10 18:37:13 rasp hass[488]: Traceback (most recent call last):
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
janv. 10 18:37:13 rasp hass[488]:     resp = await request_handler(request)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
janv. 10 18:37:13 rasp hass[488]:     resp = await handler(request)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/aiohttp/web_middlewares.py", line 117, in impl
janv. 10 18:37:13 rasp hass[488]:     return await handler(request)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
janv. 10 18:37:13 rasp hass[488]:     return await handler(request)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/components/http/forwarded.py", line 98, in forwarded_middleware
janv. 10 18:37:13 rasp hass[488]:     return await handler(request)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
janv. 10 18:37:13 rasp hass[488]:     return await handler(request)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/components/http/ban.py", line 78, in ban_middleware
janv. 10 18:37:13 rasp hass[488]:     return await handler(request)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/components/http/auth.py", line 181, in auth_middleware
janv. 10 18:37:13 rasp hass[488]:     return await handler(request)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/components/http/view.py", line 137, in handle
janv. 10 18:37:13 rasp hass[488]:     result = await result
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/components/config/config_entries.py", line 157, in post
janv. 10 18:37:13 rasp hass[488]:     return await super().post(request, flow_id)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/components/http/data_validator.py", line 62, in wrapper
janv. 10 18:37:13 rasp hass[488]:     result = await method(view, request, *args, **kwargs)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/helpers/data_entry_flow.py", line 110, in post
janv. 10 18:37:13 rasp hass[488]:     result = await self._flow_mgr.async_configure(flow_id, data)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/data_entry_flow.py", line 252, in async_configure
janv. 10 18:37:13 rasp hass[488]:     result = await self._async_handle_step(flow, cur_step["step_id"], user_input)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/data_entry_flow.py", line 325, in _async_handle_step
janv. 10 18:37:13 rasp hass[488]:     result: FlowResult = await getattr(flow, method)(user_input)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/homeassistant/components/brother/config_flow.py", line 62, in async_step_user
janv. 10 18:37:13 rasp hass[488]:     await brother.async_update()
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/brother/__init__.py", line 99, in async_update
janv. 10 18:37:13 rasp hass[488]:     if not (raw_data := await self._get_data()):
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/brother/__init__.py", line 233, in _get_data
janv. 10 18:37:13 rasp hass[488]:     await self._init_device()
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/brother/__init__.py", line 303, in _init_device
janv. 10 18:37:13 rasp hass[488]:     errindication, errstatus, errindex, _ = await hlapi.getCmd(*request_args, *oids)
janv. 10 18:37:13 rasp hass[488]:   File "/usr/local/lib/python3.10/asyncio/coroutines.py", line 124, in coro
janv. 10 18:37:13 rasp hass[488]:     res = func(*args, **kw)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/pysnmp/hlapi/asyncio/cmdgen.py", line 151, in getCmd
janv. 10 18:37:13 rasp hass[488]:     addrName, paramsName = lcd.configure(
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/pysnmp/hlapi/lcd.py", line 91, in configure
janv. 10 18:37:13 rasp hass[488]:     transport = transportTarget.openClientMode()
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/pysnmp/hlapi/transport.py", line 53, in openClientMode
janv. 10 18:37:13 rasp hass[488]:     self.transport = self.protoTransport().openClientMode(self.iface)
janv. 10 18:37:13 rasp hass[488]:   File "/data/homeassistant/lib/python3.10/site-packages/pysnmp/carrier/asyncio/dgram/base.py", line 96, in openClientMode
janv. 10 18:37:13 rasp hass[488]:     raise error.CarrierError(';'.join(traceback.format_exception(*sys.exc_info())))
janv. 10 18:37:13 rasp hass[488]: pysnmp.carrier.error.CarrierError: Traceback (most recent call last):
janv. 10 18:37:13 rasp hass[488]: ;  File "/data/homeassistant/lib/python3.10/site-packages/pysnmp/carrier/asyncio/dgram/base.py", line 93, in openClientMode
janv. 10 18:37:13 rasp hass[488]:     self._lport = getattr(asyncio, 'async')(c)
janv. 10 18:37:13 rasp hass[488]: ;AttributeError: module 'asyncio' has no attribute 'async'
janv. 10 18:37:13 rasp hass[488]: caused by <class 'AttributeError'>: module 'asyncio' has no attribute 'async'
antonc42 commented 2 years ago

@roumano If you are using v4.4.12 from pip, you may have to do a python3 -m pip install --force-reinstall git+https://github.com/antonc42/pysnmp@antonc42-4.4.12-fix-python3.10 (assuming you are using my branch). This is because the version number is the same and pip won't reinstall the same version by default.

It also might be that your Home Assistant is using a Python virtualenv or something rather than the system Python modules. Sorry, I'm not familiar with how Home Assistant does things. From the output above, it looks like something other than the standard system Python module location. Yours is /data/homeassistant/lib/python3.10/site-packages. You will have to figure out how to enter the proper virtual environment to remove/add the module.

flaviut commented 2 years ago

I manually applied the patch to my HASS installation without going through pip, and I can confirm that it fixes the issue.

On Mon, Jan 10, 2022, 14:12 antonc42 @.***> wrote:

@roumano https://github.com/roumano If you are using v4.4.12 from pip, you may have to do a python3 -m pip install --force-reinstall git+ @.*** (assuming you are using my branch). This is because the version number is the same and pip won't reinstall the same version by default.

It also might be that your Home Assistant is using a Python virtualenv or something rather than the system Python modules. Sorry, I'm not familiar with how Home Assistant does things. From the output above, it looks like something other than the standard system Python module location. Yours is /data/homeassistant/lib/python3.10/site-packages. You will have to figure out how to enter the proper virtual environment to remove/add the module.

— Reply to this email directly, view it on GitHub https://github.com/etingof/pysnmp/issues/413#issuecomment-1009258132, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHY2HO7GASBGJWNI4DMQSDUVMVSXANCNFSM5IWPPTXQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

roumano commented 2 years ago

Forgot to restart HA, it's working after, sorry for the noise...

lextm commented 1 year ago

Various forks (pysnmp/pysnmp or lextudio/pysnmp) have fixed this.

lextm commented 4 weeks ago

Time to revisit this after closing #429.

Recent releases by my team (6.x/7.x) have fixed this. Please upgrade to them, https://docs.lextudio.com/pysnmp/upgrade