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 with python parsing seen on pydantic-core #54

Merged
merged 4 commits into from
Jan 17, 2024
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
26 changes: 13 additions & 13 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ struct PythonParser<'j> {
impl<'j> PythonParser<'j> {
fn py_take_value<StringCache: StringMaybeCache>(&mut self, py: Python, peek: Peek) -> JsonResult<PyObject> {
match peek {
Peek::Null => {
self.parser.consume_null()?;
Ok(py.None())
}
Comment on lines +62 to +65
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth the compiler will probably reorder branches in a match for what it thinks makes most sense anyway (as long as they're not gated by conditionals, only one of these branches will be traversed), so moving this likely does nothing.

Peek::True => {
self.parser.consume_true()?;
Ok(true.to_object(py))
Expand All @@ -67,9 +71,15 @@ impl<'j> PythonParser<'j> {
self.parser.consume_false()?;
Ok(false.to_object(py))
}
Peek::Null => {
self.parser.consume_null()?;
Ok(py.None())
Peek::Minus | Peek::Infinity | Peek::NaN => {
let n = self
.parser
.consume_number::<NumberAny>(peek.into_inner(), self.allow_inf_nan)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe one solution is to remove peek.into_inner() and replace it with peek.into_number() which errors when it's not a number? This would probably keep most of the same performance properties, but force you to handle all invalid cases earlier as part of the wildcard / numerical branch.

match n {
NumberAny::Int(NumberInt::Int(int)) => Ok(int.to_object(py)),
NumberAny::Int(NumberInt::BigInt(big_int)) => Ok(big_int.to_object(py)),
NumberAny::Float(float) => Ok(float.to_object(py)),
}
}
Peek::String => {
let s = self.parser.consume_string::<StringDecoder>(&mut self.tape)?;
Expand Down Expand Up @@ -117,16 +127,6 @@ impl<'j> PythonParser<'j> {
}
Ok(dict.to_object(py))
}
_ => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, removing this clause did cause errors to fire here about non-exhaustive matching 😉. But I take your point about it not enforcing all datatypes.

I think part of the performance win in #48 was that it effectively removed a large set of branches (from Peak::new) because we have these same set of branches anyway when we decide what to do with the peek's u8. Put another way, we assume that the peek is always valid and defer erroring until after we fail to parse an integer.

It's definitely possible that there's another way to express this which keeps the same performance property, might require a play and a think.

let n = self
.parser
.consume_number::<NumberAny>(peek.into_inner(), self.allow_inf_nan)?;
match n {
NumberAny::Int(NumberInt::Int(int)) => Ok(int.to_object(py)),
NumberAny::Int(NumberInt::BigInt(big_int)) => Ok(big_int.to_object(py)),
NumberAny::Float(float) => Ok(float.to_object(py)),
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ single_tests! {
second_line: err => "[1\nx]", "ExpectedListCommaOrEnd @ 2:1";
floats_error: err => "06", "InvalidNumber @ 1:2";
unexpect_value: err => "[\u{16}\u{8}", "ExpectedSomeValue @ 1:2";
unexpect_value_xx: err => "xx", "ExpectedSomeValue @ 1:1";
}

#[test]
Expand Down
12 changes: 12 additions & 0 deletions tests/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ fn test_recursion_limit_incr() {
assert_eq!(v.as_ref(py).len().unwrap(), 2000);
});
}

#[test]
fn test_exected_value_error() {
let json = "xx";
let bytes = json.as_bytes();

Python::with_gil(|py| {
let r = python_parse(py, bytes, false, true);
let e = r.map_err(|e| map_json_error(bytes, &e)).unwrap_err();
assert_eq!(e.to_string(), "ValueError: expected value at line 1 column 1");
})
}
Loading