pysnmp icon indicating copy to clipboard operation
pysnmp copied to clipboard

Python 3.10 compatibility

Open mheppner opened this issue 2 years ago • 6 comments

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]

mheppner avatar Nov 24 '21 16:11 mheppner

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/[email protected]

Or put this in your requirements.txt: git+https://github.com/antonc42/[email protected]

Or put this in your install_requires in setup.py if your module depends on pysnmp: 'pysnmp @ git+https://github.com/antonc42/[email protected]'

antonc42 avatar Jan 10 '22 15:01 antonc42

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'

roumano avatar Jan 10 '22 17:01 roumano

@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/[email protected] (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.

antonc42 avatar Jan 10 '22 19:01 antonc42

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: @.***>

flaviut avatar Jan 10 '22 20:01 flaviut

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

roumano avatar Jan 10 '22 21:01 roumano

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

lextm avatar Jan 19 '23 23:01 lextm