From 48c2b5a69711cceb1d2f8a7f55a85d1712976cf0 Mon Sep 17 00:00:00 2001 From: Adam Cecile Date: Tue, 12 Sep 2023 16:49:27 +0200 Subject: [PATCH] Fix asyncio hlapi double awaitable returns (Fix #19) Commit 67563a19f028d371efa9683f60eb52908a292b86 introduced a bug while converting legacy asyncio API to new "async def" style. Previous methods where NOT decorated with @asyncio.coroutine and returned a Future object being awaitable. Updated code still returns a Future object but adds async keyword to function definition leading to awaitable function returning awaitable Future. That breaks existing API. --- pysnmp/hlapi/asyncio/cmdgen.py | 20 ++++++++------------ pysnmp/hlapi/asyncio/ntforg.py | 5 ++--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/pysnmp/hlapi/asyncio/cmdgen.py b/pysnmp/hlapi/asyncio/cmdgen.py index 6d93d8f85..b7cca28ce 100644 --- a/pysnmp/hlapi/asyncio/cmdgen.py +++ b/pysnmp/hlapi/asyncio/cmdgen.py @@ -112,14 +112,13 @@ async def getCmd(snmpEngine, authData, transportTarget, contextData, >>> from pysnmp.hlapi.asyncio import * >>> >>> async def run(): - ... result_get = await getCmd( + ... errorIndication, errorStatus, errorIndex, varBinds = await getCmd( ... SnmpEngine(), ... CommunityData('public'), ... UdpTransportTarget(('demo.pysnmp.com', 161)), ... ContextData(), ... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) ... ) - ... errorIndication, errorStatus, errorIndex, varBinds = await result_get ... print(errorIndication, errorStatus, errorIndex, varBinds) >>> >>> asyncio.run(run()) @@ -156,7 +155,7 @@ def __cbFun(snmpEngine, sendRequestHandle, vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun, (options.get('lookupMib', True), future) ) - return future + return await future async def setCmd(snmpEngine, authData, transportTarget, contextData, @@ -218,14 +217,13 @@ async def setCmd(snmpEngine, authData, transportTarget, contextData, >>> from pysnmp.hlapi.asyncio import * >>> >>> async def run(): - ... set_result = await setCmd( + ... errorIndication, errorStatus, errorIndex, varBinds = await setCmd( ... SnmpEngine(), ... CommunityData('public'), ... UdpTransportTarget(('demo.pysnmp.com', 161)), ... ContextData(), ... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0), 'Linux i386') ... ) - ... errorIndication, errorStatus, errorIndex, varBinds = await set_result ... print(errorIndication, errorStatus, errorIndex, varBinds) >>> >>> asyncio.run(run()) @@ -262,7 +260,7 @@ def __cbFun(snmpEngine, sendRequestHandle, vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun, (options.get('lookupMib', True), future) ) - return future + return await future async def nextCmd(snmpEngine, authData, transportTarget, contextData, @@ -328,14 +326,13 @@ async def nextCmd(snmpEngine, authData, transportTarget, contextData, >>> from pysnmp.hlapi.asyncio import * >>> >>> async def run(): - ... next_result = await nextCmd( + ... errorIndication, errorStatus, errorIndex, varBinds = await nextCmd( ... SnmpEngine(), ... CommunityData('public'), ... UdpTransportTarget(('demo.pysnmp.com', 161)), ... ContextData(), ... ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')) ... ) - ... errorIndication, errorStatus, errorIndex, varBinds = await next_result ... print(errorIndication, errorStatus, errorIndex, varBinds) >>> >>> asyncio.run(run()) @@ -374,7 +371,7 @@ def __cbFun(snmpEngine, sendRequestHandle, vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun, (options.get('lookupMib', True), future) ) - return future + return await future async def bulkCmd(snmpEngine, authData, transportTarget, contextData, @@ -468,7 +465,7 @@ async def bulkCmd(snmpEngine, authData, transportTarget, contextData, >>> from pysnmp.hlapi.asyncio import * >>> >>> async def run(): - ... result_bulk = await bulkCmd( + ... errorIndication, errorStatus, errorIndex, varBinds = await bulkCmd( ... SnmpEngine(), ... CommunityData('public'), ... UdpTransportTarget(('demo.pysnmp.com', 161)), @@ -476,7 +473,6 @@ async def bulkCmd(snmpEngine, authData, transportTarget, contextData, ... 0, 2, ... ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')) ... ) - ... errorIndication, errorStatus, errorIndex, varBinds = await result_bulk ... print(errorIndication, errorStatus, errorIndex, varBinds) >>> >>> asyncio.run(run()) @@ -515,4 +511,4 @@ def __cbFun(snmpEngine, sendRequestHandle, vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun, (options.get('lookupMib', True), future) ) - return future + return await future diff --git a/pysnmp/hlapi/asyncio/ntforg.py b/pysnmp/hlapi/asyncio/ntforg.py index 5b5915aac..e52687b82 100644 --- a/pysnmp/hlapi/asyncio/ntforg.py +++ b/pysnmp/hlapi/asyncio/ntforg.py @@ -101,14 +101,13 @@ async def sendNotification(snmpEngine, authData, transportTarget, contextData, >>> from pysnmp.hlapi.asyncio import * >>> >>> async def run(): - ... send_result = await sendNotification( + ... errorIndication, errorStatus, errorIndex, varBinds = await sendNotification( ... SnmpEngine(), ... CommunityData('public'), ... UdpTransportTarget(('demo.pysnmp.com', 162)), ... ContextData(), ... 'trap', ... NotificationType(ObjectIdentity('IF-MIB', 'linkDown'))) - ... errorIndication, errorStatus, errorIndex, varBinds = await send_result ... print(errorIndication, errorStatus, errorIndex, varBinds) ... >>> asyncio.run(run()) @@ -159,4 +158,4 @@ def __trapFun(future): loop = asyncio.get_event_loop() loop.call_soon(__trapFun, future) - return future + return await future