fix(python): Fix SystemError when non-str dict key provided #386
This commit is contained in:
parent
e174bd1332
commit
bbd0153de8
|
@ -18,7 +18,7 @@ use std::ffi::CStr;
|
|||
|
||||
pub const RECURSION_LIMIT: u8 = 255;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ObjectType {
|
||||
Str,
|
||||
Int,
|
||||
|
@ -141,6 +141,15 @@ impl Serialize for SerializePyObject {
|
|||
unsafe {
|
||||
pyo3::ffi::PyDict_Next(self.object, &mut pos, &mut key, &mut value);
|
||||
}
|
||||
match get_object_type_from_object(key) {
|
||||
ObjectType::Str => {}
|
||||
object_type => {
|
||||
return Err(ser::Error::custom(format!(
|
||||
"Supported only str key type. Provided type '{:?}'",
|
||||
object_type
|
||||
)))
|
||||
}
|
||||
}
|
||||
let uni = unsafe { string::read_utf8_from_str(key, &mut str_size) };
|
||||
let slice = unsafe {
|
||||
std::str::from_utf8_unchecked(std::slice::from_raw_parts(
|
||||
|
|
|
@ -232,3 +232,11 @@ def test_enums(type_, value, expected):
|
|||
schema = {"properties": {"foo": {"type": type_}}}
|
||||
instance = {"foo": value}
|
||||
assert is_valid(schema, instance) is expected
|
||||
|
||||
|
||||
def test_dict_with_non_str_keys():
|
||||
schema = {"type": "object"}
|
||||
instance = {1234567: "foo"}
|
||||
with pytest.raises(ValueError) as exec_info:
|
||||
validate(schema, instance)
|
||||
assert exec_info.value.args[0] == "Supported only str key type. Provided type 'Int'"
|
||||
|
|
Loading…
Reference in New Issue