jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.25k stars 65 forks source link

Encoding exceptions - segfault #727

Open regnarg opened 2 weeks ago

regnarg commented 2 weeks ago

Question

I tried to make serializable exceptions using:

import msgspec
class TaggedException(msgspec.Struct, Exception, tag=True): pass

However, this reliably segfaults Python (CPython 3.12.5, msgspec 0.18.6).

I presume there is some kind of incompatibility between the C-level layout of Struct objects and Exception objects? Are there any good alternatives for encoding exception objects? (which seems super usefil when implementing APIs) Maybe this cannot be fixed. But maybe it should at least throw an error instead of crashing the interpreter?

provinzkraut commented 2 weeks ago

I usually do something like this:


class EncodableException(Exception):
  def to_dict(self) -> dict[str, Any]:
   ...

def enc_hook(value: Any) -> Any:
  if isinstance(value, EncodableException):
    return value.to_dict()
  raise TypeError()

and then use this enc_hook whenever I'm encoding stuff that might contain one of those exceptions.