Skip to content

Commit e5b5f8d

Browse files
committed
more support for bytearray
1 parent 21a6ee2 commit e5b5f8d

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/input/input_python.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::str::from_utf8;
33
use pyo3::exceptions::{PyAttributeError, PyTypeError};
44
use pyo3::prelude::*;
55
use pyo3::types::{
6-
PyBool, PyBytes, PyDate, PyDateTime, PyDict, PyFrozenSet, PyInt, PyList, PyMapping, PySequence, PySet, PyString,
7-
PyTime, PyTuple, PyType,
6+
PyBool, PyByteArray, PyBytes, PyDate, PyDateTime, PyDict, PyFrozenSet, PyInt, PyList, PyMapping, PySequence, PySet,
7+
PyString, PyTime, PyTuple, PyType,
88
};
99
use pyo3::{intern, AsPyPointer};
1010

@@ -60,6 +60,12 @@ impl<'a> Input<'a> for PyAny {
6060
Err(_) => return Err(ValError::new(ErrorKind::StrUnicode, self)),
6161
};
6262
Ok(str.into())
63+
} else if let Ok(py_byte_array) = self.cast_as::<PyByteArray>() {
64+
let str = match from_utf8(unsafe { py_byte_array.as_bytes() }) {
65+
Ok(s) => s,
66+
Err(_) => return Err(ValError::new(ErrorKind::StrUnicode, self)),
67+
};
68+
Ok(str.into())
6369
} else if self.cast_as::<PyBool>().is_ok() {
6470
// do this before int and float parsing as `False` is cast to `0` and we don't want False to
6571
// be returned as a string
@@ -270,6 +276,8 @@ impl<'a> Input<'a> for PyAny {
270276
} else if let Ok(py_str) = self.cast_as::<PyString>() {
271277
let string = py_str.to_string_lossy().to_string();
272278
Ok(string.into_bytes().into())
279+
} else if let Ok(py_byte_array) = self.cast_as::<PyByteArray>() {
280+
Ok(py_byte_array.to_vec().into())
273281
} else {
274282
Err(ValError::new(ErrorKind::BytesType, self))
275283
}

tests/validators/test_bytes.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ def test_strict_bytes_validator():
1414
assert v.validate_json('"foo"') == b'foo'
1515

1616
with pytest.raises(ValidationError, match='Value must be a valid bytes'):
17-
assert v.validate_python('foo') == b'foo'
17+
v.validate_python('foo')
18+
with pytest.raises(ValidationError, match='Value must be a valid bytes'):
19+
v.validate_python(bytearray(b'foo'))
1820

1921

2022
def test_lax_bytes_validator():
2123
v = SchemaValidator({'type': 'bytes'})
2224

2325
assert v.validate_python(b'foo') == b'foo'
2426
assert v.validate_python('foo') == b'foo'
27+
assert v.validate_python(bytearray(b'foo')) == b'foo'
2528

2629
assert v.validate_json('"foo"') == b'foo'
2730

tests/validators/test_string.py

+5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ def test_str(py_or_json, input_value, expected):
4141
[
4242
('foobar', 'foobar'),
4343
(b'foobar', 'foobar'),
44+
(bytearray(b'foobar'), 'foobar'),
4445
(b'\x81', Err('Value must be a valid string, unable to parse raw data as a unicode string [kind=str_unicode')),
46+
(
47+
bytearray(b'\x81'),
48+
Err('Value must be a valid string, unable to parse raw data as a unicode string [kind=str_unicode'),
49+
),
4550
# null bytes are very annoying, but we can't really block them here
4651
(b'\x00', '\x00'),
4752
(123, '123'),

0 commit comments

Comments
 (0)