Skip to content

Commit

Permalink
fix: use context in factory
Browse files Browse the repository at this point in the history
  • Loading branch information
diefans committed Dec 5, 2024
1 parent cc014c8 commit ad180e4
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/buvar/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import contextlib
import contextvars
import functools
import sys

from . import components

Expand All @@ -19,19 +20,36 @@ class StackingTaskFactory:
def __init__(self, *, parent_factory=None):
self.parent_factory = parent_factory

def __call__(self, loop, coro, context=None):
context = current_context().push()
token = buvar_context.set(context)
# with child():
task = (
self.parent_factory
if self.parent_factory is not None
else asyncio.tasks.Task
)(loop=loop, coro=coro)
try:
return task
finally:
buvar_context.reset(token)
if sys.version_info > (3, 10):
# INFO: Task() accepts context
def __call__(self, loop, coro, context=None):
component_context = current_context().push()
token = buvar_context.set(component_context)
# with child():
task = (
self.parent_factory
if self.parent_factory is not None
else asyncio.tasks.Task
)(loop=loop, coro=coro, context=context)
try:
return task
finally:
buvar_context.reset(token)
else:

def __call__(self, loop, coro, context=None):
component_context = current_context().push()
token = buvar_context.set(component_context)
# with child():
task = (
self.parent_factory
if self.parent_factory is not None
else asyncio.tasks.Task
)(loop=loop, coro=coro)
try:
return task
finally:
buvar_context.reset(token)

@classmethod
def set(cls, *, loop=None):
Expand Down

0 comments on commit ad180e4

Please sign in to comment.