Skip to content

Commit

Permalink
🍻 version 0.16.2
Browse files Browse the repository at this point in the history
let `ExitState` as both Enum and Exception
  • Loading branch information
RF-Tar-Railt committed Feb 26, 2025
1 parent ff9c0a2 commit f9c9333
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
6 changes: 1 addition & 5 deletions arclet/letoderea/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .exceptions import UnresolvedRequirement as UnresolvedRequirement
from .exceptions import ProviderUnsatisfied as ProviderUnsatisfied
from .exceptions import switch_print_traceback as switch_print_traceback
from .exceptions import ExitState as ExitState
from .exceptions import STOP as STOP
from .exceptions import BLOCK as BLOCK
from .handler import ExceptionEvent as ExceptionEvent
Expand Down Expand Up @@ -39,8 +40,3 @@
post = es.post
on = es.on
use = es.use


class ExitState:
STOP = STOP
BLOCK = BLOCK
21 changes: 9 additions & 12 deletions arclet/letoderea/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pprint
import sys
import traceback
from enum import Enum
from types import CodeType
from typing import Any, Callable, Final

Expand All @@ -17,16 +18,12 @@ def __init__(self, source_key: str):
self.source_key = source_key


class _Exit(Exception):
pass


class _ExitStop(_Exit):
pass

class ExitState(Exception, Enum):
stop = "Handle stopped"
block = "Propagation blocked"

class _ExitBlock(_Exit):
pass
def __str__(self):
return super(Exception, self).__str__()


class InnerHandlerException(Exception):
Expand Down Expand Up @@ -74,7 +71,7 @@ def call(e: Exception, callable_target: Callable, contexts: Contexts, inner: boo
return exc
if inner:
return InnerHandlerException(e)
if isinstance(e, (InnerHandlerException, ProviderUnsatisfied, _ExitBlock, _ExitStop)):
if isinstance(e, (InnerHandlerException, ProviderUnsatisfied, ExitState)):
return e
if ExceptionHandler.print_traceback: # pragma: no cover
traceback.print_exception(e.__class__, e, e.__traceback__)
Expand All @@ -85,5 +82,5 @@ def switch_print_traceback(flag: bool): # pragma: no cover
ExceptionHandler.print_traceback = flag


STOP: Final = _ExitStop()
BLOCK: Final = _ExitBlock()
STOP: Final = ExitState.stop
BLOCK: Final = ExitState.block
8 changes: 4 additions & 4 deletions arclet/letoderea/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ async def dispatch(
for _i, result in enumerate(results):
if result is None:
continue
if result is BLOCK:
return
if result is STOP:
continue
if isinstance(result, BaseException):
if isinstance(event, ExceptionEvent):
return
if result is BLOCK:
return
if result is STOP:
continue
await publish_exc_event(ExceptionEvent(event, grouped[priority][_i], result))
continue
if not return_result:
Expand Down
8 changes: 4 additions & 4 deletions arclet/letoderea/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
ExceptionHandler,
STOP,
BLOCK,
_Exit,
ExitState,
)
from .provider import Param, Provider, ProviderFactory, provide
from .ref import Deref, generate
Expand Down Expand Up @@ -249,7 +249,7 @@ def dispose(self):
while self._propagates:
self._propagates[0].dispose()

async def handle(self, context: Contexts, inner=False) -> R | _Exit:
async def handle(self, context: Contexts, inner=False) -> R | ExitState:
if not inner:
context["$subscriber"] = self
if self.has_cm and "$exit_stack" not in context:
Expand All @@ -258,7 +258,7 @@ async def handle(self, context: Contexts, inner=False) -> R | _Exit:
if self._cursor:
_res = await self._run_propagate(context, self._propagates[: self._cursor])
if _res is STOP or _res is BLOCK:
return _res # type: ignore
return _res
arguments: Contexts = {} # type: ignore
for param in self.params:
if param.depend:
Expand Down Expand Up @@ -315,7 +315,7 @@ async def _run_propagate(self, context: Contexts, propagates: list[Subscriber]):
else:
raise
else:
if isinstance(result, _Exit):
if isinstance(result, ExitState):
return result
if isinstance(result, dict):
context.update(result)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "arclet-letoderea"
version = "0.16.1"
version = "0.16.2"
description = "A high-performance, simple-structured event system, relies on asyncio"
authors = [
{name = "RF-Tar-Railt", email = "rf_tar_railt@qq.com"},
Expand Down
22 changes: 11 additions & 11 deletions tests/test_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,50 +106,50 @@ async def test_exit_state():
executed = []

@le.on(TestEvent, priority=10)
async def _1():
async def s_1():
executed.append(1)

@le.on(TestEvent, priority=12)
async def _2():
async def s_2():
executed.append(2)

await le.publish(TestEvent("f", "b"))
assert executed == [1, 2]

_1.dispose()
s_1.dispose()
executed.clear()

@le.on(TestEvent, priority=10)
async def _3():
async def s_3():
executed.append(1)
return STOP

await le.publish(TestEvent("f", "b"))
assert executed == [1, 2]

_3.dispose()
s_3.dispose()
executed.clear()

@le.on(TestEvent, priority=10)
async def _4():
async def s_4():
executed.append(1)
return ExitState.BLOCK
return ExitState.block

await le.publish(TestEvent("f", "b"))
assert executed == [1]

_4.dispose()
s_4.dispose()
executed.clear()

@le.on(TestEvent, priority=10)
async def _5():
async def s_5():
executed.append(0)
raise STOP

@le.on(TestEvent, priority=11)
async def _6():
async def s_6():
executed.append(1)
raise ExitState.BLOCK
raise ExitState.block

await le.publish(TestEvent("f", "b"))
assert executed == [0, 1]

0 comments on commit f9c9333

Please sign in to comment.