File tree Expand file tree Collapse file tree 2 files changed +41
-12
lines changed
Expand file tree Collapse file tree 2 files changed +41
-12
lines changed Original file line number Diff line number Diff line change @@ -140,23 +140,34 @@ def makecall(call: SimplugImplCall, async_hook: bool = False):
140140 Returns:
141141 The result of the call
142142 """
143+ spec_prefix = "[async]" if async_hook else ""
144+ err_msg = (
145+ "Error while calling hook implementation, "
146+ f"plugin={ call .plugin } ; spec={ spec_prefix } { call .impl .__name__ } "
147+ )
148+ if inspect .iscoroutinefunction (call .impl ):
149+
150+ async def coro ():
151+ try :
152+ return await call .impl (* call .args , ** call .kwargs )
153+ except Exception as exc :
154+ raise ResultError (err_msg ) from exc
155+
156+ return coro ()
157+
143158 try :
144159 out = call .impl (* call .args , ** call .kwargs )
145160 except Exception as exc :
146- raise ResultError (
147- f"Error while calling { call .plugin } .{ call .impl .__name__ } (...)"
148- ) from exc
161+ raise ResultError (err_msg ) from exc
149162
150- if not async_hook :
151- return out
163+ if async_hook :
152164
153- if inspect . iscoroutine ( out ):
154- return out
165+ async def coro ( ):
166+ return out
155167
156- async def coro ():
157- return out
168+ return coro ()
158169
159- return coro ()
170+ return out
160171
161172
162173class SimplugWrapper :
Original file line number Diff line number Diff line change @@ -1264,22 +1264,40 @@ class Specs:
12641264 def hook (arg ):
12651265 ...
12661266
1267+ @simplug .spec
1268+ async def ahook (arg ):
1269+ ...
1270+
12671271 class Plugin1 :
12681272 @simplug .impl
12691273 def hook (arg ):
12701274 return 1
12711275
1276+ @simplug .impl
1277+ async def ahook (arg ):
1278+ return 1 / 0
1279+
12721280 class Plugin2 :
12731281 name = "SomePlugin"
12741282
12751283 @simplug .impl
12761284 def hook (arg ):
12771285 return 1 / 0
12781286
1279- simplug .register (Plugin1 , Plugin2 )
1280- with pytest .raises (ResultError , match = r"SomePlugin\.hook" ):
1287+ @simplug .impl
1288+ def ahook (arg ):
1289+ return 1
1290+
1291+ with pytest .warns (SyncImplOnAsyncSpecWarning ):
1292+ simplug .register (Plugin1 , Plugin2 )
1293+
1294+ with pytest .raises (ResultError , match = r"plugin=SomePlugin; spec=hook" ):
12811295 simplug .hooks .hook (1 )
12821296
1297+ with pytest .raises (ResultError , match = r"plugin=plugin1; spec=\[async\]ahook" ):
1298+ asyncio .run (simplug .hooks .ahook (1 ))
1299+
1300+
12831301def test_async_impl_on_sync_spec ():
12841302 simplug = Simplug ("test_async_impl_on_sync_spec" )
12851303
You can’t perform that action at this time.
0 commit comments