Compare commits

..

3 Commits

Author SHA1 Message Date
Tyler Goodlet acf6f123a9 Raise RTE from `limit_plds()` on no `curr_ctx`
Since it should only be used from within a `Portal.open_context()`
scope, make sure the caller knows that!

Also don't hide the frame in tb if the immediate function errors..
2025-03-08 15:50:14 -05:00
Tyler Goodlet 27a3fdc473 Offer a `mods: list` to `dec_type_union()`; drop importing this-mod 2025-03-08 15:49:21 -05:00
Tyler Goodlet 62db4f232d Tweak type-error messages for when `ext_types` is missing 2025-03-08 15:48:18 -05:00
3 changed files with 53 additions and 23 deletions

View File

@ -211,24 +211,28 @@ def mk_dec(
and
ext_types is None
):
raise ValueError(
f'You must provide a type-spec for a msg decoder!\n'
f'The only time `spec=None` is permitted is if custom extension types '
f'are expected to be supported, in which case `ext_types` must be non-`None`'
f'and it is presumed that only the `ext_types` (supported by the paired `dec_hook()`) '
f'will be permitted within the type-`spec`!\n'
f'tpec = {spec!r}\n'
raise TypeError(
f'MIssing type-`spec` for msg decoder!\n'
f'\n'
f'`spec=None` is **only** permitted is if custom extension types '
f'are provided via `ext_types`, meaning it must be non-`None`.\n'
f'\n'
f'In this case it is presumed that only the `ext_types`, '
f'which much be handled by a paired `dec_hook()`, '
f'will be permitted within the payload type-`spec`!\n'
f'\n'
f'spec = {spec!r}\n'
f'dec_hook = {dec_hook!r}\n'
f'ext_types = {ext_types!r}\n'
)
if dec_hook:
if ext_types is None:
raise ValueError(
f'If extending the serializable types with a custom decoder hook, '
f'you must also provide the expected type set `dec_hook()` will handle '
f'via the `ext_types: Union[Type]|None = None` argument!\n'
raise TypeError(
f'If extending the serializable types with a custom decode hook (`dec_hook()`), '
f'you must also provide the expected type set that the hook will handle '
f'via a `ext_types: Union[Type]|None = None` argument!\n'
f'\n'
f'dec_hook = {dec_hook!r}\n'
f'ext_types = {ext_types!r}\n'
)
@ -287,7 +291,7 @@ def unpack_spec_types(
When `spec` is not a type-union returns `{spec,}`.
'''
spec_subtypes: set[Union[Type]] = (
spec_subtypes: set[Union[Type]] = set(
getattr(
spec,
'__args__',
@ -449,6 +453,7 @@ class MsgCodec(Struct):
# |_BufferError: Existing exports of data: object cannot be re-sized
as_ext_type: bool = False,
hide_tb: bool = True,
) -> bytes:
'''
@ -459,11 +464,21 @@ class MsgCodec(Struct):
https://jcristharif.com/msgspec/perf-tips.html#reusing-an-output-buffer
'''
__tracebackhide__: bool = hide_tb
if use_buf:
self._enc.encode_into(py_obj, self._buf)
return self._buf
return self._enc.encode(py_obj)
# try:
# return self._enc.encode(py_obj)
# except TypeError as typerr:
# typerr.add_note(
# '|_src error from `msgspec`'
# # f'|_{self._enc.encode!r}'
# )
# raise typerr
# TODO! REMOVE once i'm confident we won't ever need it!
#
# box: Struct = self._ext_types_box
@ -572,10 +587,11 @@ def mk_codec(
pld_spec = ipc_pld_spec
if enc_hook:
if not ext_types:
raise ValueError(
f'If extending the serializable types with a custom decoder hook, '
f'you must also provide the expected type set `enc_hook()` will handle '
f'via the `ext_types: Union[Type]|None = None` argument!\n'
raise TypeError(
f'If extending the serializable types with a custom encode hook (`enc_hook()`), '
f'you must also provide the expected type set that the hook will handle '
f'via a `ext_types: Union[Type]|None = None` argument!\n'
f'\n'
f'enc_hook = {enc_hook!r}\n'
f'ext_types = {ext_types!r}\n'
)

View File

@ -33,6 +33,9 @@ converters,
|_ https://jcristharif.com/msgspec/changelog.html
'''
from types import (
ModuleType,
)
import typing
from typing import (
Type,
@ -41,19 +44,20 @@ from typing import (
def dec_type_union(
type_names: list[str],
) -> Type:
mods: list[ModuleType] = []
) -> Type|Union[Type]:
'''
Look up types by name, compile into a list and then create and
return a `typing.Union` from the full set.
'''
import importlib
# import importlib
types: list[Type] = []
for type_name in type_names:
for mod in [
typing,
importlib.import_module(__name__),
]:
# importlib.import_module(__name__),
] + mods:
if type_ref := getattr(
mod,
type_name,

View File

@ -461,11 +461,16 @@ def limit_plds(
'''
__tracebackhide__: bool = True
curr_ctx: Context|None = current_ipc_ctx()
if curr_ctx is None:
raise RuntimeError(
'No IPC `Context` is active !?\n'
'Did you open `limit_plds()` from outside '
'a `Portal.open_context()` scope-block?'
)
try:
curr_ctx: Context = current_ipc_ctx()
rx: PldRx = curr_ctx._pld_rx
orig_pldec: MsgDec = rx.pld_dec
with rx.limit_plds(
spec=spec,
**dec_kwargs,
@ -475,6 +480,11 @@ def limit_plds(
f'{pldec}\n'
)
yield pldec
except BaseException:
__tracebackhide__: bool = False
raise
finally:
log.runtime(
'Reverted to previous payload-decoder\n\n'