From bbd0153de883fc0bb0a78df1152fbf87905636f8 Mon Sep 17 00:00:00 2001 From: "o.ermakov" Date: Tue, 18 Oct 2022 17:21:41 +0400 Subject: [PATCH] fix(python): Fix SystemError when non-str dict key provided #386 --- bindings/python/src/ser.rs | 11 ++++++++++- bindings/python/tests-py/test_jsonschema.py | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/bindings/python/src/ser.rs b/bindings/python/src/ser.rs index 207d53e..e257505 100644 --- a/bindings/python/src/ser.rs +++ b/bindings/python/src/ser.rs @@ -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( diff --git a/bindings/python/tests-py/test_jsonschema.py b/bindings/python/tests-py/test_jsonschema.py index 758d6be..9734463 100644 --- a/bindings/python/tests-py/test_jsonschema.py +++ b/bindings/python/tests-py/test_jsonschema.py @@ -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'"