fix(python): Fix SystemError when non-str dict key provided #386

This commit is contained in:
o.ermakov 2022-10-18 17:21:41 +04:00 committed by Dmitry Dygalo
parent e174bd1332
commit bbd0153de8
2 changed files with 18 additions and 1 deletions

View File

@ -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(

View File

@ -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'"