py_yyjson icon indicating copy to clipboard operation
py_yyjson copied to clipboard

SystemError when trying to serialize a dictionary with non-string keys

Open MichaelSquires opened this issue 8 months ago • 2 comments

Hi, I ran across this error today which seems like a bug:

$ python -c "import yyjson; yyjson.dumps({'a':'a', 2:2})"
TypeError: bad argument type for built-in operation

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "~/.pyenv/versions/3.11.7/lib/python3.11/site-packages/yyjson/__init__.py", line 71, in dumps
    return Document(obj, default=default).dumps()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SystemError: <class 'cyyjson.Document'> returned a result with an exception se

The preferred behavior here would probably be to raise a ValueError with a descriptive message about the dict key not being a string.

This was tested with py_yyjson v4.0.6 from pypi.

Thanks!

MichaelSquires avatar Apr 17 '25 19:04 MichaelSquires

The repro case can be further minimized to just python -c "import yyjson; yyjson.dumps({2:2})".

MichaelSquires avatar Apr 17 '25 19:04 MichaelSquires

Here's another edge case (surrogates in the dict key): python -c "import yyjson; yyjson.dumps({'\ud83d\ude47': 'foo'})"

MichaelSquires avatar Apr 18 '25 12:04 MichaelSquires