fix(python): Review fixes
This commit is contained in:
parent
bbd0153de8
commit
e40a35a983
|
@ -74,6 +74,23 @@ fn get_object_type_from_object(object: *mut pyo3::ffi::PyObject) -> ObjectType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_type_name(object_type: *mut pyo3::ffi::PyTypeObject) -> String {
|
||||||
|
let type_name = unsafe { CStr::from_ptr((*object_type).tp_name).to_string_lossy() };
|
||||||
|
type_name.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn check_type_is_str<E: ser::Error>(object: *mut pyo3::ffi::PyObject) -> Result<(), E> {
|
||||||
|
let object_type = unsafe { Py_TYPE(object) };
|
||||||
|
if object_type != unsafe { types::STR_TYPE } {
|
||||||
|
return Err(ser::Error::custom(format!(
|
||||||
|
"Dict key must be str. Got '{}'",
|
||||||
|
get_type_name(object_type)
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_object_type(object_type: *mut pyo3::ffi::PyTypeObject) -> ObjectType {
|
pub fn get_object_type(object_type: *mut pyo3::ffi::PyTypeObject) -> ObjectType {
|
||||||
if object_type == unsafe { types::STR_TYPE } {
|
if object_type == unsafe { types::STR_TYPE } {
|
||||||
|
@ -95,8 +112,7 @@ pub fn get_object_type(object_type: *mut pyo3::ffi::PyTypeObject) -> ObjectType
|
||||||
} else if is_enum_subclass(object_type) {
|
} else if is_enum_subclass(object_type) {
|
||||||
ObjectType::Enum
|
ObjectType::Enum
|
||||||
} else {
|
} else {
|
||||||
let type_name = unsafe { CStr::from_ptr((*object_type).tp_name).to_string_lossy() };
|
ObjectType::Unknown(get_type_name(object_type))
|
||||||
ObjectType::Unknown(type_name.to_string())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,15 +157,7 @@ impl Serialize for SerializePyObject {
|
||||||
unsafe {
|
unsafe {
|
||||||
pyo3::ffi::PyDict_Next(self.object, &mut pos, &mut key, &mut value);
|
pyo3::ffi::PyDict_Next(self.object, &mut pos, &mut key, &mut value);
|
||||||
}
|
}
|
||||||
match get_object_type_from_object(key) {
|
check_type_is_str(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 uni = unsafe { string::read_utf8_from_str(key, &mut str_size) };
|
||||||
let slice = unsafe {
|
let slice = unsafe {
|
||||||
std::str::from_utf8_unchecked(std::slice::from_raw_parts(
|
std::str::from_utf8_unchecked(std::slice::from_raw_parts(
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import uuid
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
@ -236,7 +237,7 @@ def test_enums(type_, value, expected):
|
||||||
|
|
||||||
def test_dict_with_non_str_keys():
|
def test_dict_with_non_str_keys():
|
||||||
schema = {"type": "object"}
|
schema = {"type": "object"}
|
||||||
instance = {1234567: "foo"}
|
instance = {uuid.uuid4(): "foo"}
|
||||||
with pytest.raises(ValueError) as exec_info:
|
with pytest.raises(ValueError) as exec_info:
|
||||||
validate(schema, instance)
|
validate(schema, instance)
|
||||||
assert exec_info.value.args[0] == "Supported only str key type. Provided type 'Int'"
|
assert exec_info.value.args[0] == "Dict key must be str. Got 'UUID'"
|
||||||
|
|
Loading…
Reference in New Issue