omlish 0.0.0.dev410__py3-none-any.whl → 0.0.0.dev412__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- omlish/__about__.py +2 -2
- omlish/lang/__init__.py +3 -0
- omlish/lite/maysyncs.py +45 -46
- {omlish-0.0.0.dev410.dist-info → omlish-0.0.0.dev412.dist-info}/METADATA +1 -1
- {omlish-0.0.0.dev410.dist-info → omlish-0.0.0.dev412.dist-info}/RECORD +9 -9
- {omlish-0.0.0.dev410.dist-info → omlish-0.0.0.dev412.dist-info}/WHEEL +0 -0
- {omlish-0.0.0.dev410.dist-info → omlish-0.0.0.dev412.dist-info}/entry_points.txt +0 -0
- {omlish-0.0.0.dev410.dist-info → omlish-0.0.0.dev412.dist-info}/licenses/LICENSE +0 -0
- {omlish-0.0.0.dev410.dist-info → omlish-0.0.0.dev412.dist-info}/top_level.txt +0 -0
omlish/__about__.py
CHANGED
omlish/lang/__init__.py
CHANGED
omlish/lite/maysyncs.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# ruff: noqa: UP006 UP043 UP045 UP046 UP047
|
1
|
+
# ruff: noqa: UP006 UP007 UP043 UP045 UP046 UP047
|
2
2
|
# @omlish-lite
|
3
3
|
"""
|
4
4
|
A system for writing a python function once which can then be effectively used in both sync and async contexts -
|
@@ -35,6 +35,8 @@ TODO:
|
|
35
35
|
- for debug, mask any running eventloop while running maysync code
|
36
36
|
- `[CO_ASYNC_GENERATOR] = {k for k, v in dis.COMPILER_FLAG_NAMES.items() if v == 'ASYNC_GENERATOR'}` ? inspect is big..
|
37
37
|
- works down to 3.8
|
38
|
+
- make_maysync_from_sync can run with asyncio.run_in_thread
|
39
|
+
- make_maysync_from_async can run with asyncio.run_soon
|
38
40
|
"""
|
39
41
|
import abc
|
40
42
|
import functools
|
@@ -160,10 +162,6 @@ class _MaysyncThreadLocal(threading.local):
|
|
160
162
|
class _MaysyncContext(abc.ABC):
|
161
163
|
mode: ta.ClassVar[ta.Literal['s', 'a']]
|
162
164
|
|
163
|
-
@classmethod
|
164
|
-
def current(cls) -> ta.Optional['_MaysyncContext']:
|
165
|
-
return _MaysyncThreadLocal.context
|
166
|
-
|
167
165
|
@abc.abstractmethod
|
168
166
|
def run(self, fn: ta.Callable[..., T], *args: ta.Any, **kwargs: ta.Any) -> T:
|
169
167
|
raise NotImplementedError
|
@@ -177,14 +175,18 @@ class _SyncMaysyncContext(_MaysyncContext):
|
|
177
175
|
prev = _MaysyncThreadLocal.context
|
178
176
|
_MaysyncThreadLocal.context = self
|
179
177
|
|
180
|
-
ph = sys.get_asyncgen_hooks()
|
181
|
-
|
178
|
+
ph: ta.Any = sys.get_asyncgen_hooks()
|
179
|
+
if ph.firstiter is not None or ph.finalizer is not None:
|
180
|
+
sys.set_asyncgen_hooks(firstiter=None, finalizer=None)
|
181
|
+
else:
|
182
|
+
ph = None
|
182
183
|
|
183
184
|
try:
|
184
185
|
return fn(*args, **kwargs)
|
185
186
|
|
186
187
|
finally:
|
187
|
-
|
188
|
+
if ph is not None:
|
189
|
+
sys.set_asyncgen_hooks(*ph)
|
188
190
|
|
189
191
|
_MaysyncThreadLocal.context = prev
|
190
192
|
|
@@ -220,9 +222,7 @@ class _MaywaitableLike(
|
|
220
222
|
args: ta.Tuple[ta.Any, ...],
|
221
223
|
kwargs: ta.Mapping[str, ta.Any],
|
222
224
|
) -> None:
|
223
|
-
self._x = x
|
224
|
-
self._args = args
|
225
|
-
self._kwargs = kwargs
|
225
|
+
self._x, self._args, self._kwargs = x, args, kwargs
|
226
226
|
|
227
227
|
def __repr__(self) -> str:
|
228
228
|
return f'{self.__class__.__name__}@{id(self):x}({self._x!r})'
|
@@ -299,8 +299,8 @@ class _FpMaywaitable(
|
|
299
299
|
return self._x._s(*self._args, **self._kwargs) # noqa
|
300
300
|
|
301
301
|
def a(self) -> ta.Awaitable[T]:
|
302
|
-
if
|
303
|
-
return _MaysyncFuture(self
|
302
|
+
if _MaysyncThreadLocal.context is not None:
|
303
|
+
return _MaysyncFuture(self)
|
304
304
|
|
305
305
|
return self._x._a(*self._args, **self._kwargs) # noqa
|
306
306
|
|
@@ -332,7 +332,7 @@ class _FpMaysyncGenerator(
|
|
332
332
|
return self._x._s(*self._args, **self._kwargs) # noqa
|
333
333
|
|
334
334
|
def a(self) -> ta.AsyncGenerator[O, I]:
|
335
|
-
if (ctx :=
|
335
|
+
if (ctx := _MaysyncThreadLocal.context) is not None and ctx.mode == 's':
|
336
336
|
async def inner():
|
337
337
|
g = self._x._s(*self._args, **self._kwargs) # noqa
|
338
338
|
|
@@ -411,11 +411,6 @@ class _MgMaysyncFnLike(
|
|
411
411
|
abc.ABC,
|
412
412
|
ta.Generic[T],
|
413
413
|
):
|
414
|
-
"""
|
415
|
-
A maysync object backed by an underlying generator yielding _MaysyncOp's. The _MgDriver and _MgGeneratorDriver
|
416
|
-
classes produce such generators.
|
417
|
-
"""
|
418
|
-
|
419
414
|
def __init__(
|
420
415
|
self,
|
421
416
|
mg: ta.Callable[..., T],
|
@@ -452,13 +447,13 @@ class _MgMaysyncFn(
|
|
452
447
|
|
453
448
|
@ta.final
|
454
449
|
class _MgMaysyncDriver:
|
455
|
-
def __init__(self, ctx, mg):
|
450
|
+
def __init__(self, ctx: _MaysyncContext, mg: ta.Any) -> None:
|
456
451
|
self.ctx = ctx
|
457
452
|
self.mg = mg
|
458
453
|
|
459
454
|
value: ta.Any
|
460
455
|
|
461
|
-
def __iter__(self):
|
456
|
+
def __iter__(self) -> ta.Generator['_MaysyncFuture', None, None]:
|
462
457
|
try:
|
463
458
|
a = self.mg.__await__()
|
464
459
|
try:
|
@@ -478,7 +473,8 @@ class _MgMaysyncDriver:
|
|
478
473
|
del f
|
479
474
|
|
480
475
|
finally:
|
481
|
-
|
476
|
+
if g is not a:
|
477
|
+
self.ctx.run(g.close)
|
482
478
|
|
483
479
|
finally:
|
484
480
|
self.ctx.run(a.close)
|
@@ -502,7 +498,7 @@ class _MgMaywaitable(
|
|
502
498
|
return drv.value
|
503
499
|
|
504
500
|
def a(self) -> ta.Awaitable[T]:
|
505
|
-
if (ctx :=
|
501
|
+
if (ctx := _MaysyncThreadLocal.context) is None or ctx.mode == 'a':
|
506
502
|
return self._x._mg(*self._args, **self._kwargs) # noqa
|
507
503
|
|
508
504
|
async def inner():
|
@@ -527,11 +523,21 @@ class _MgMaysyncGeneratorFn(
|
|
527
523
|
|
528
524
|
@ta.final
|
529
525
|
class _MgMaysyncGeneratorDriver:
|
530
|
-
def __init__(self, ctx, ag):
|
526
|
+
def __init__(self, ctx: _MaysyncContext, ag: ta.Any) -> None:
|
531
527
|
self.ctx = ctx
|
532
528
|
self.ag = ag
|
533
529
|
|
534
|
-
def __iter__(self)
|
530
|
+
def __iter__(self) -> ta.Generator[
|
531
|
+
ta.Union[
|
532
|
+
ta.Tuple[ta.Literal['f'], '_MaysyncFuture'],
|
533
|
+
ta.Tuple[ta.Literal['o'], ta.Any],
|
534
|
+
],
|
535
|
+
ta.Union[
|
536
|
+
ta.Tuple[ta.Any, BaseException],
|
537
|
+
None,
|
538
|
+
],
|
539
|
+
None,
|
540
|
+
]:
|
535
541
|
try:
|
536
542
|
ai = self.ag.__aiter__()
|
537
543
|
try:
|
@@ -553,11 +559,12 @@ class _MgMaysyncGeneratorDriver:
|
|
553
559
|
|
554
560
|
del f
|
555
561
|
|
556
|
-
i, e = yield ('o', drv.value)
|
562
|
+
i, e = yield ('o', drv.value) # type: ignore[misc]
|
557
563
|
|
558
564
|
finally:
|
559
|
-
|
560
|
-
|
565
|
+
if ai is not self.ag:
|
566
|
+
for f in _MgMaysyncDriver(self.ctx, ai.aclose()):
|
567
|
+
yield ('f', f)
|
561
568
|
|
562
569
|
finally:
|
563
570
|
for f in _MgMaysyncDriver(self.ctx, self.ag.aclose()):
|
@@ -572,8 +579,7 @@ class _MgMaysyncGenerator(
|
|
572
579
|
return _MgMaysyncGeneratorDriver(ctx, self._x._mg(*self._args, **self._kwargs)) # noqa
|
573
580
|
|
574
581
|
def s(self) -> ta.Generator[O, I, None]:
|
575
|
-
|
576
|
-
di = iter(drv)
|
582
|
+
di = iter(self._driver(_SyncMaysyncContext()))
|
577
583
|
|
578
584
|
ie: ta.Any = None
|
579
585
|
|
@@ -592,8 +598,7 @@ class _MgMaysyncGenerator(
|
|
592
598
|
|
593
599
|
elif t == 'o':
|
594
600
|
try:
|
595
|
-
|
596
|
-
ie = (y, None)
|
601
|
+
ie = ((yield x), None) # type: ignore[misc]
|
597
602
|
except BaseException as ex: # noqa
|
598
603
|
ie = (None, ex)
|
599
604
|
|
@@ -603,12 +608,11 @@ class _MgMaysyncGenerator(
|
|
603
608
|
del x
|
604
609
|
|
605
610
|
def a(self) -> ta.AsyncGenerator[O, I]:
|
606
|
-
if
|
611
|
+
if _MaysyncThreadLocal.context is not None:
|
607
612
|
return self._x._mg(*self._args, **self._kwargs) # noqa
|
608
613
|
|
609
614
|
async def inner():
|
610
|
-
|
611
|
-
di = iter(drv)
|
615
|
+
di = iter(self._driver(_AsyncMaysyncContext()))
|
612
616
|
|
613
617
|
ie: ta.Any = None
|
614
618
|
|
@@ -627,8 +631,7 @@ class _MgMaysyncGenerator(
|
|
627
631
|
|
628
632
|
elif t == 'o':
|
629
633
|
try:
|
630
|
-
|
631
|
-
ie = (y, None)
|
634
|
+
ie = ((yield x), None)
|
632
635
|
except BaseException as ex: # noqa
|
633
636
|
ie = (None, ex)
|
634
637
|
|
@@ -694,16 +697,12 @@ class _MaysyncFutureNotAwaitedError(RuntimeError):
|
|
694
697
|
class _MaysyncFuture(ta.Generic[T]):
|
695
698
|
def __init__(
|
696
699
|
self,
|
697
|
-
x:
|
698
|
-
args: ta.Tuple[ta.Any, ...],
|
699
|
-
kwargs: ta.Mapping[str, ta.Any],
|
700
|
+
x: Maywaitable[T],
|
700
701
|
) -> None:
|
701
|
-
self.
|
702
|
-
self.args = args
|
703
|
-
self.kwargs = kwargs
|
702
|
+
self._x = x
|
704
703
|
|
705
704
|
def __repr__(self) -> str:
|
706
|
-
return f'{self.__class__.__name__}@{id(self):x}({self.
|
705
|
+
return f'{self.__class__.__name__}@{id(self):x}({self._x!r}, done={self.done!r})'
|
707
706
|
|
708
707
|
done: bool = False
|
709
708
|
result: T
|
@@ -724,7 +723,7 @@ class _MaysyncFuture(ta.Generic[T]):
|
|
724
723
|
return
|
725
724
|
|
726
725
|
try:
|
727
|
-
self.result = self.
|
726
|
+
self.result = self._x.s()
|
728
727
|
except BaseException as ex: # noqa
|
729
728
|
self.error = ex
|
730
729
|
self.done = True
|
@@ -734,7 +733,7 @@ class _MaysyncFuture(ta.Generic[T]):
|
|
734
733
|
return
|
735
734
|
|
736
735
|
try:
|
737
|
-
self.result = await self.
|
736
|
+
self.result = await self._x.a()
|
738
737
|
except BaseException as ex: # noqa
|
739
738
|
self.error = ex
|
740
739
|
self.done = True
|
@@ -1,5 +1,5 @@
|
|
1
1
|
omlish/.manifests.json,sha256=FLw7xkPiSXuImZgqSP8BwrEib2R1doSzUPLUkc-QUIA,8410
|
2
|
-
omlish/__about__.py,sha256=
|
2
|
+
omlish/__about__.py,sha256=3v5VyjqPS3L-ET8qtR0hcOs83kKWIvjEPRXXmdB7Ih8,3601
|
3
3
|
omlish/__init__.py,sha256=SsyiITTuK0v74XpKV8dqNaCmjOlan1JZKrHQv5rWKPA,253
|
4
4
|
omlish/c3.py,sha256=rer-TPOFDU6fYq_AWio_AmA-ckZ8JDY5shIzQ_yXfzA,8414
|
5
5
|
omlish/cached.py,sha256=MLap_p0rdGoDIMVhXVHm1tsbcWobJF0OanoodV03Ju8,542
|
@@ -425,7 +425,7 @@ omlish/iterators/iterators.py,sha256=RxW35yQ5ed8vBQ22IqpDXFx-i5JiLQdp7-pkMZXhJJ8
|
|
425
425
|
omlish/iterators/recipes.py,sha256=wOwOZg-zWG9Zc3wcAxJFSe2rtavVBYwZOfG09qYEx_4,472
|
426
426
|
omlish/iterators/tools.py,sha256=M16LXrJhMdsz5ea2qH0vws30ZvhQuQSCVFSLpRf_gTg,2096
|
427
427
|
omlish/iterators/unique.py,sha256=BSE-eanva8byFCJi09Nt2zzTsVr8LnTqY1PIInGYRs0,1396
|
428
|
-
omlish/lang/__init__.py,sha256=
|
428
|
+
omlish/lang/__init__.py,sha256=H5-XUAfz_qPmo170bskFxQmwNcsGhaQZdfyl1t5YR2U,7436
|
429
429
|
omlish/lang/asyncs.py,sha256=HQrf8oaaZnUYbFwIiVLYvsEshJarMvRNHZ3VN-RyqLY,1789
|
430
430
|
omlish/lang/attrs.py,sha256=zFiVuGVOq88x45464T_LxDa-ZEq_RD9zJLq2zeVEBDc,5105
|
431
431
|
omlish/lang/casing.py,sha256=cFUlbDdXLhwnWwcYx4qnM5c4zGX7hIRUfcjiZbxUD28,4636
|
@@ -490,7 +490,7 @@ omlish/lite/json.py,sha256=m0Ce9eqUZG23-H7-oOp8n1sf4fzno5vtK4AK_4Vc-Mg,706
|
|
490
490
|
omlish/lite/logs.py,sha256=CWFG0NKGhqNeEgryF5atN2gkPYbUdTINEw_s1phbINM,51
|
491
491
|
omlish/lite/marshal.py,sha256=K_wnZwfC8cftGILyE3RlmzQEYuZOfzkMLKey41zuwtM,20296
|
492
492
|
omlish/lite/maybes.py,sha256=0p_fzb6yiOjEpvMKaQ53Q6CH1VPW1or7v7Lt1JIKcgM,4359
|
493
|
-
omlish/lite/maysyncs.py,sha256=
|
493
|
+
omlish/lite/maysyncs.py,sha256=x49PFphBuaFYmBjrSf9xgCzim6U3u3gANfPaTRbrh5I,19841
|
494
494
|
omlish/lite/pycharm.py,sha256=FRHGcCDo42UzZXqNwW_DkhI-6kb_CmJKPiQ8F6mYkLA,1174
|
495
495
|
omlish/lite/reflect.py,sha256=gI-Qlws9V-jND7kvCQFaIhBFrndVpDsikTQ7C6U2z3w,2434
|
496
496
|
omlish/lite/reprs.py,sha256=2Bc7ukhKvYNTKmxPIuv9glZIph13C37y_W4fg9pBnu8,2006
|
@@ -909,9 +909,9 @@ omlish/typedvalues/marshal.py,sha256=AtBz7Jq-BfW8vwM7HSxSpR85JAXmxK2T0xDblmm1HI0
|
|
909
909
|
omlish/typedvalues/of_.py,sha256=UXkxSj504WI2UrFlqdZJbu2hyDwBhL7XVrc2qdR02GQ,1309
|
910
910
|
omlish/typedvalues/reflect.py,sha256=PAvKW6T4cW7u--iX80w3HWwZUS3SmIZ2_lQjT65uAyk,1026
|
911
911
|
omlish/typedvalues/values.py,sha256=ym46I-q2QJ_6l4UlERqv3yj87R-kp8nCKMRph0xQ3UA,1307
|
912
|
-
omlish-0.0.0.
|
913
|
-
omlish-0.0.0.
|
914
|
-
omlish-0.0.0.
|
915
|
-
omlish-0.0.0.
|
916
|
-
omlish-0.0.0.
|
917
|
-
omlish-0.0.0.
|
912
|
+
omlish-0.0.0.dev412.dist-info/licenses/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
|
913
|
+
omlish-0.0.0.dev412.dist-info/METADATA,sha256=XKkH8EZhmV_YWRXB_GKYSwfyY2p4YORV9lhx9UxhyQU,18881
|
914
|
+
omlish-0.0.0.dev412.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
915
|
+
omlish-0.0.0.dev412.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
|
916
|
+
omlish-0.0.0.dev412.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
|
917
|
+
omlish-0.0.0.dev412.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|