Skip to content

Commit

Permalink
Fix asyncio hlapi double awaitable returns (Fix #19)
Browse files Browse the repository at this point in the history
Commit 67563a1 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.
  • Loading branch information
eLvErDe committed Sep 12, 2023
1 parent 498d36b commit 48c2b5a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
20 changes: 8 additions & 12 deletions pysnmp/hlapi/asyncio/cmdgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -468,15 +465,14 @@ 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)),
... ContextData(),
... 0, 2,
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'system'))
... )
... errorIndication, errorStatus, errorIndex, varBinds = await result_bulk
... print(errorIndication, errorStatus, errorIndex, varBinds)
>>>
>>> asyncio.run(run())
Expand Down Expand Up @@ -515,4 +511,4 @@ def __cbFun(snmpEngine, sendRequestHandle,
vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun,
(options.get('lookupMib', True), future)
)
return future
return await future
5 changes: 2 additions & 3 deletions pysnmp/hlapi/asyncio/ntforg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -159,4 +158,4 @@ def __trapFun(future):
loop = asyncio.get_event_loop()
loop.call_soon(__trapFun, future)

return future
return await future

0 comments on commit 48c2b5a

Please sign in to comment.