omlish 0.0.0.dev410__py3-none-any.whl → 0.0.0.dev411__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 CHANGED
@@ -1,5 +1,5 @@
1
- __version__ = '0.0.0.dev410'
2
- __revision__ = 'fa58128e15cab568117709c80765dc8d633cb4e0'
1
+ __version__ = '0.0.0.dev411'
2
+ __revision__ = 'ab397888a161157384b42de969a8c743c5a2c4b6'
3
3
 
4
4
 
5
5
  #
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
@@ -177,14 +179,18 @@ class _SyncMaysyncContext(_MaysyncContext):
177
179
  prev = _MaysyncThreadLocal.context
178
180
  _MaysyncThreadLocal.context = self
179
181
 
180
- ph = sys.get_asyncgen_hooks()
181
- sys.set_asyncgen_hooks(firstiter=None, finalizer=None)
182
+ ph: ta.Any = sys.get_asyncgen_hooks()
183
+ if ph.firstiter is not None or ph.finalizer is not None:
184
+ sys.set_asyncgen_hooks(firstiter=None, finalizer=None)
185
+ else:
186
+ ph = None
182
187
 
183
188
  try:
184
189
  return fn(*args, **kwargs)
185
190
 
186
191
  finally:
187
- sys.set_asyncgen_hooks(*ph)
192
+ if ph is not None:
193
+ sys.set_asyncgen_hooks(*ph)
188
194
 
189
195
  _MaysyncThreadLocal.context = prev
190
196
 
@@ -300,7 +306,7 @@ class _FpMaywaitable(
300
306
 
301
307
  def a(self) -> ta.Awaitable[T]:
302
308
  if _MaysyncContext.current() is not None:
303
- return _MaysyncFuture(self._x, self._args, self._kwargs)
309
+ return _MaysyncFuture(functools.partial(self._x, *self._args, **self._kwargs)) # noqa
304
310
 
305
311
  return self._x._a(*self._args, **self._kwargs) # noqa
306
312
 
@@ -411,11 +417,6 @@ class _MgMaysyncFnLike(
411
417
  abc.ABC,
412
418
  ta.Generic[T],
413
419
  ):
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
420
  def __init__(
420
421
  self,
421
422
  mg: ta.Callable[..., T],
@@ -452,13 +453,13 @@ class _MgMaysyncFn(
452
453
 
453
454
  @ta.final
454
455
  class _MgMaysyncDriver:
455
- def __init__(self, ctx, mg):
456
+ def __init__(self, ctx: _MaysyncContext, mg: ta.Any) -> None:
456
457
  self.ctx = ctx
457
458
  self.mg = mg
458
459
 
459
460
  value: ta.Any
460
461
 
461
- def __iter__(self):
462
+ def __iter__(self) -> ta.Generator['_MaysyncFuture', None, None]:
462
463
  try:
463
464
  a = self.mg.__await__()
464
465
  try:
@@ -478,7 +479,8 @@ class _MgMaysyncDriver:
478
479
  del f
479
480
 
480
481
  finally:
481
- self.ctx.run(g.close)
482
+ if g is not a:
483
+ self.ctx.run(g.close)
482
484
 
483
485
  finally:
484
486
  self.ctx.run(a.close)
@@ -527,11 +529,21 @@ class _MgMaysyncGeneratorFn(
527
529
 
528
530
  @ta.final
529
531
  class _MgMaysyncGeneratorDriver:
530
- def __init__(self, ctx, ag):
532
+ def __init__(self, ctx: _MaysyncContext, ag: ta.Any) -> None:
531
533
  self.ctx = ctx
532
534
  self.ag = ag
533
535
 
534
- def __iter__(self):
536
+ def __iter__(self) -> ta.Generator[
537
+ ta.Union[
538
+ ta.Tuple[ta.Literal['f'], '_MaysyncFuture'],
539
+ ta.Tuple[ta.Literal['o'], ta.Any],
540
+ ],
541
+ ta.Union[
542
+ ta.Tuple[ta.Any, BaseException],
543
+ None,
544
+ ],
545
+ None,
546
+ ]:
535
547
  try:
536
548
  ai = self.ag.__aiter__()
537
549
  try:
@@ -553,11 +565,12 @@ class _MgMaysyncGeneratorDriver:
553
565
 
554
566
  del f
555
567
 
556
- i, e = yield ('o', drv.value)
568
+ i, e = yield ('o', drv.value) # type: ignore[misc]
557
569
 
558
570
  finally:
559
- for f in _MgMaysyncDriver(self.ctx, ai.aclose()):
560
- yield ('f', f)
571
+ if ai is not self.ag:
572
+ for f in _MgMaysyncDriver(self.ctx, ai.aclose()):
573
+ yield ('f', f)
561
574
 
562
575
  finally:
563
576
  for f in _MgMaysyncDriver(self.ctx, self.ag.aclose()):
@@ -572,8 +585,7 @@ class _MgMaysyncGenerator(
572
585
  return _MgMaysyncGeneratorDriver(ctx, self._x._mg(*self._args, **self._kwargs)) # noqa
573
586
 
574
587
  def s(self) -> ta.Generator[O, I, None]:
575
- drv = self._driver(_SyncMaysyncContext())
576
- di = iter(drv)
588
+ di = iter(self._driver(_SyncMaysyncContext()))
577
589
 
578
590
  ie: ta.Any = None
579
591
 
@@ -592,8 +604,7 @@ class _MgMaysyncGenerator(
592
604
 
593
605
  elif t == 'o':
594
606
  try:
595
- y = yield x
596
- ie = (y, None)
607
+ ie = ((yield x), None) # type: ignore[misc]
597
608
  except BaseException as ex: # noqa
598
609
  ie = (None, ex)
599
610
 
@@ -607,8 +618,7 @@ class _MgMaysyncGenerator(
607
618
  return self._x._mg(*self._args, **self._kwargs) # noqa
608
619
 
609
620
  async def inner():
610
- drv = self._driver(_AsyncMaysyncContext())
611
- di = iter(drv)
621
+ di = iter(self._driver(_AsyncMaysyncContext()))
612
622
 
613
623
  ie: ta.Any = None
614
624
 
@@ -627,8 +637,7 @@ class _MgMaysyncGenerator(
627
637
 
628
638
  elif t == 'o':
629
639
  try:
630
- y = yield x
631
- ie = (y, None)
640
+ ie = ((yield x), None)
632
641
  except BaseException as ex: # noqa
633
642
  ie = (None, ex)
634
643
 
@@ -694,16 +703,12 @@ class _MaysyncFutureNotAwaitedError(RuntimeError):
694
703
  class _MaysyncFuture(ta.Generic[T]):
695
704
  def __init__(
696
705
  self,
697
- x: ta.Any,
698
- args: ta.Tuple[ta.Any, ...],
699
- kwargs: ta.Mapping[str, ta.Any],
706
+ x: ta.Callable[[], Maywaitable[T]],
700
707
  ) -> None:
701
- self.x = x
702
- self.args = args
703
- self.kwargs = kwargs
708
+ self._x = x
704
709
 
705
710
  def __repr__(self) -> str:
706
- return f'{self.__class__.__name__}@{id(self):x}({self.x!r}, done={self.done!r})'
711
+ return f'{self.__class__.__name__}@{id(self):x}({self._x!r}, done={self.done!r})'
707
712
 
708
713
  done: bool = False
709
714
  result: T
@@ -724,7 +729,7 @@ class _MaysyncFuture(ta.Generic[T]):
724
729
  return
725
730
 
726
731
  try:
727
- self.result = self.x(*self.args, **self.kwargs).s()
732
+ self.result = self._x().s()
728
733
  except BaseException as ex: # noqa
729
734
  self.error = ex
730
735
  self.done = True
@@ -734,7 +739,7 @@ class _MaysyncFuture(ta.Generic[T]):
734
739
  return
735
740
 
736
741
  try:
737
- self.result = await self.x(*self.args, **self.kwargs).a()
742
+ self.result = await self._x().a()
738
743
  except BaseException as ex: # noqa
739
744
  self.error = ex
740
745
  self.done = True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: omlish
3
- Version: 0.0.0.dev410
3
+ Version: 0.0.0.dev411
4
4
  Summary: omlish
5
5
  Author: wrmsr
6
6
  License-Expression: BSD-3-Clause
@@ -1,5 +1,5 @@
1
1
  omlish/.manifests.json,sha256=FLw7xkPiSXuImZgqSP8BwrEib2R1doSzUPLUkc-QUIA,8410
2
- omlish/__about__.py,sha256=4TpD9f2NLOxe8wPIx2jkab-TAgd8pW7229kNrBPdko4,3601
2
+ omlish/__about__.py,sha256=qRafTkrQl5EPJTSgaE1ncsq5b84uBIoagA3OZKLm0p4,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
@@ -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=XZ-zbK3bES8Al6--lYcvGGhYKqfVg1zoHJ6GCwLbiv4,19663
493
+ omlish/lite/maysyncs.py,sha256=QqXV0thRHwevPFlkyELv4tHmXBM9cJ6uMjx0pZ0LbHc,20046
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.dev410.dist-info/licenses/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
913
- omlish-0.0.0.dev410.dist-info/METADATA,sha256=tqNKk_ovprTJtbpkIsi_jZRS44y4hTlBreu5pTxzTRs,18881
914
- omlish-0.0.0.dev410.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
915
- omlish-0.0.0.dev410.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
916
- omlish-0.0.0.dev410.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
917
- omlish-0.0.0.dev410.dist-info/RECORD,,
912
+ omlish-0.0.0.dev411.dist-info/licenses/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
913
+ omlish-0.0.0.dev411.dist-info/METADATA,sha256=3MPeR2SuWXjWtGnh7xZs-LvdohVQarQEXwYO7Oh01ao,18881
914
+ omlish-0.0.0.dev411.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
915
+ omlish-0.0.0.dev411.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
916
+ omlish-0.0.0.dev411.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
917
+ omlish-0.0.0.dev411.dist-info/RECORD,,