Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: convert python tuples into lists #312

Merged
merged 6 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bindings/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Fixed

- Set `jsonschema_rs.JSONSchema.__module__` to `jsonschema_rs`.
- Convert tuples into lists for validation to fix `ValueError: Unsupported type: 'tuple'`

### Performance

Expand Down
33 changes: 32 additions & 1 deletion bindings/python/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pyo3::{
exceptions,
ffi::{
PyDictObject, PyFloat_AS_DOUBLE, PyList_GET_ITEM, PyList_GET_SIZE, PyLong_AsLongLong,
Py_TYPE,
Py_TYPE, PyTuple_GET_ITEM, PyTuple_GET_SIZE
},
prelude::*,
types::PyAny,
Expand All @@ -27,6 +27,7 @@ pub enum ObjectType {
Float,
List,
Dict,
Tuple,
Unknown(String),
}

Expand Down Expand Up @@ -81,6 +82,8 @@ pub fn get_object_type(object_type: *mut pyo3::ffi::PyTypeObject) -> ObjectType
ObjectType::None
} else if object_type == unsafe { types::LIST_TYPE } {
ObjectType::List
} else if object_type == unsafe { types::TUPLE_TYPE } {
ObjectType::Tuple
} else if object_type == unsafe { types::DICT_TYPE } {
ObjectType::Dict
} else {
Expand Down Expand Up @@ -174,6 +177,34 @@ impl Serialize for SerializePyObject {
sequence.end()
}
}
ObjectType::Tuple => {
blacha marked this conversation as resolved.
Show resolved Hide resolved
if self.recursion_depth == RECURSION_LIMIT {
return Err(ser::Error::custom("Recursion limit reached"));
}
let length = unsafe { PyTuple_GET_SIZE(self.object) } as usize;
if length == 0 {
serializer.serialize_seq(Some(0))?.end()
} else {
let mut type_ptr = std::ptr::null_mut();
let mut ob_type = ObjectType::Str;
let mut sequence = serializer.serialize_seq(Some(length))?;
for i in 0..length {
let elem = unsafe { PyTuple_GET_ITEM(self.object, i as isize) };
let current_ob_type = unsafe { Py_TYPE(elem) };
if current_ob_type != type_ptr {
type_ptr = current_ob_type;
ob_type = get_object_type(current_ob_type);
}
#[allow(clippy::integer_arithmetic)]
sequence.serialize_element(&SerializePyObject::with_obtype(
elem,
ob_type.clone(),
self.recursion_depth + 1,
))?;
}
sequence.end()
}
}
ObjectType::Unknown(ref type_name) => Err(ser::Error::custom(format!(
"Unsupported type: '{}'",
type_name
Expand Down
4 changes: 3 additions & 1 deletion bindings/python/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pyo3::ffi::{
PyDict_New, PyFloat_FromDouble, PyList_New, PyLong_FromLongLong, PyTypeObject, PyUnicode_New,
Py_None, Py_TYPE, Py_True,
Py_None, Py_TYPE, Py_True, PyTuple_New
};
use std::sync::Once;

Expand All @@ -13,6 +13,7 @@ pub static mut NONE_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
pub static mut FLOAT_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
pub static mut LIST_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
pub static mut DICT_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
pub static mut TUPLE_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;

static INIT: Once = Once::new();

Expand All @@ -24,6 +25,7 @@ pub fn init() {
TRUE = Py_True();
STR_TYPE = Py_TYPE(PyUnicode_New(0, 255));
DICT_TYPE = Py_TYPE(PyDict_New());
TUPLE_TYPE = Py_TYPE(PyTuple_New(1));
blacha marked this conversation as resolved.
Show resolved Hide resolved
LIST_TYPE = Py_TYPE(PyList_New(0_isize));
NONE_TYPE = Py_TYPE(Py_None());
BOOL_TYPE = Py_TYPE(TRUE);
Expand Down
6 changes: 6 additions & 0 deletions bindings/python/tests-py/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def test_from_str_error():
JSONSchema.from_str(42)


def test_tuple():
schema = {"properties": {"foo": {"type": "array"}}}
instance = {"foo": (1, 2, 3)}
assert is_valid(instance, schema) == True
blacha marked this conversation as resolved.
Show resolved Hide resolved


def test_recursive_dict():
instance = {}
instance["foo"] = instance
Expand Down