Skip to content

Commit

Permalink
Relational tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
bswck committed Oct 24, 2024
1 parent 4e9d3ec commit e1d9ea2
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions injection/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
class InjectionKey(str):
__slots__ = ("origin", "hash", "reset", "early")

def __init__(self, key: str, early: EarlyObject[object]) -> None:
self.origin = key
self.hash = hash(key)
def __init__(self, alias: str, early: EarlyObject[object]) -> None:
self.origin = alias
self.hash = hash(alias)
self.reset = False
self.early = early

Expand All @@ -56,7 +56,7 @@ def __eq__(self, other: object) -> bool:

with self.early.__mutex__:
__injection_recursive_guard__ = True # noqa: F841
self.early.__inject__(self)
self.early.__inject__()

return True

Expand Down Expand Up @@ -125,7 +125,7 @@ def assign_to(self, *aliases: str, scope: Locals) -> None:
cache_per_alias=cache_per_alias,
debug_info=debug_info,
)
key = InjectionKey(alias, early)
key = early.__key__

with self._reassignment_lock:
scope.pop(key, None)
Expand Down Expand Up @@ -159,15 +159,15 @@ def __repr__(self) -> str:
include = f" ({debug_info})"
return f"<ObjectState{include}>"

def create(self, scope: Locals, early: EarlyObject[Object_co]) -> None:
def create(self, early: EarlyObject[Object_co]) -> None:
if self.object is SENTINEL or not self.cache:
recursion_key = (id(early), get_ident())
if recursion_key in self.running:
self.recursion_guard(early)
else:
try:
self.running.add(recursion_key)
self.object = self.factory(scope)
self.object = self.factory(self.scope)
finally:
self.running.remove(recursion_key)

Expand All @@ -181,27 +181,23 @@ def __init__(
cache_per_alias: bool,
debug_info: str | None = None,
) -> None:
self.__alias__ = alias
self.__mutex__ = RLock()
self.__cache_per_alias = cache_per_alias
self.__alias = alias
self.__state = state
self.__debug_info = debug_info
self.__key__ = InjectionKey(alias, self)

@property
def __alias__(self) -> str:
return self.__alias

def __inject__(self, key: InjectionKey) -> None:
scope = self.__state.scope

__injection_recursive_guard__ = True # noqa: F841

def __inject__(self) -> None:
# To ever know if we're in a child scope, try:
# >>> req_scope = get_frame(1).f_locals
# >>> in_child_scope = next(filter(self.__alias.__eq__, req_scope), True)

self.__state.create(scope, self)
obj, alias = self.__state.object, self.__alias
__injection_recursive_guard__ = True # noqa: F841
key, alias, scope = (self.__key__, self.__alias__, self.__state.scope)

self.__state.create(self)
obj = self.__state.object

with self.__mutex__:
with suppress(KeyError):
Expand All @@ -218,9 +214,10 @@ def __inject__(self, key: InjectionKey) -> None:
scope[key] = obj

def __repr__(self) -> str:
include = ""
hint = "before __inject__()"
include = f" ({hint})"
if debug_info := self.__debug_info:
include = f" ({debug_info})"
include = f" ({debug_info} {hint})"
return f"<EarlyObject{include}>"


Expand Down

0 comments on commit e1d9ea2

Please sign in to comment.