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 all commits
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
pass_filenames: false
- id: clippy
name: Clippy
entry: cargo clippy -- -D warnings -A incomplete_features -W clippy::dbg_macro -W clippy::print_stdout
entry: cargo clippy -F python -- -D warnings -A incomplete_features -W clippy::dbg_macro -W clippy::print_stdout
types: [rust]
language: system
pass_filenames: false
Expand Down
25 changes: 16 additions & 9 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pyo3::{ffi, AsPyPointer};
use hashbrown::hash_map::{HashMap, RawEntryMut};
use smallvec::SmallVec;

use crate::errors::{json_err, JsonError, JsonResult, DEFAULT_RECURSION_LIMIT};
use crate::errors::{json_err, json_error, JsonError, JsonResult, DEFAULT_RECURSION_LIMIT};
use crate::number_decoder::{NumberAny, NumberInt};
use crate::parse::{Parser, Peek};
use crate::string_decoder::{StringDecoder, Tape};
Expand Down 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,10 +71,6 @@ impl<'j> PythonParser<'j> {
self.parser.consume_false()?;
Ok(false.to_object(py))
}
Peek::Null => {
self.parser.consume_null()?;
Ok(py.None())
}
Peek::String => {
let s = self.parser.consume_string::<StringDecoder>(&mut self.tape)?;
Ok(StringCache::get(py, s.as_str()))
Expand Down Expand Up @@ -120,11 +120,18 @@ impl<'j> PythonParser<'j> {
_ => {
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)?;
.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)),
Ok(NumberAny::Int(NumberInt::Int(int))) => Ok(int.to_object(py)),
Ok(NumberAny::Int(NumberInt::BigInt(big_int))) => Ok(big_int.to_object(py)),
Ok(NumberAny::Float(float)) => Ok(float.to_object(py)),
Err(e) => {
if !peek.is_num() {
Err(json_error!(ExpectedSomeValue, self.parser.index))
} else {
Err(e)
}
}
}
}
}
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