agronholm / cbor2

Python CBOR (de)serializer with extensive tag support
MIT License
226 stars 59 forks source link

Exceptions raised in 'object_hook' callback from C implementation of load result in SystemError #201

Closed kohlerjl closed 11 months ago

kohlerjl commented 11 months ago

Things to check first

cbor2 version

5.5.1

Python version

3.11.6

What happened?

Python Exceptions raised in the object_hook callback in cbor2.load are not propagated correctly in the C module, and result in this exception: "SystemError: returned a result with an exception set".

I would expect the original Python exceptions to be propagated up, matching the python implementation behavior.

How can we reproduce the bug?

import pytest

import cbor2
from cbor2._decoder import loads as loads_py
from _cbor2 import loads as loads_c

def dec_objhook(decoder, d):
    raise TypeError("Unsupported type")

a = {
    'a': 1, 
    'hello': 'world', 
    'test': {'foo': 'bar'}
}
test = cbor2.dumps(a)

with pytest.raises(TypeError):
    loads_py(test, object_hook=dec_objhook)

with pytest.raises(TypeError):
    loads_c(test, object_hook=dec_objhook)
agronholm commented 11 months ago

This too resulted from sloppy C code. Maybe I should rewrite the C extension myself, on top of HPy.

agronholm commented 11 months ago

It would be even better if I could do it with Rust. But HPy doesn't support Rust just yet.