Skip to content

Commit

Permalink
fix with python parsing seen on pydantic-core (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Jan 17, 2024
1 parent 849d5b8 commit 1ce40b3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
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())
}
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> {
_ => {
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");
})
}

0 comments on commit 1ce40b3

Please sign in to comment.