Skip to content

Commit 7cb88e6

Browse files
authored
update to PyO3 0.22 (#119)
1 parent d2d0cd0 commit 7cb88e6

File tree

10 files changed

+152
-155
lines changed

10 files changed

+152
-155
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ jobs:
213213
cache-target: release
214214
bins: cargo-fuzz
215215

216-
- run: cargo fuzz run --fuzz-dir crates/fuzz compare_to_serde --release -- -max_total_time=300s
216+
- run: |
217+
# cargo fuzz defaults to musl targets, which is seeming incomatible with sanitizers according to CI failures
218+
RUST_TARGET=$(rustc -Vv | grep host | cut -d ' ' -f 2)
219+
cargo fuzz run --target=$RUST_TARGET --fuzz-dir crates/fuzz compare_to_serde --release -- -max_total_time=300s
217220
218221
fuzz-skip:
219222
name: fuzz skip
@@ -230,7 +233,10 @@ jobs:
230233
cache-target: release
231234
bins: cargo-fuzz
232235

233-
- run: cargo fuzz run --fuzz-dir crates/fuzz compare_skip --release -- -max_total_time=300s
236+
- run: |
237+
# cargo fuzz defaults to musl targets, which is seeming incomatible with sanitizers according to CI failures
238+
RUST_TARGET=$(rustc -Vv | grep host | cut -d ' ' -f 2)
239+
cargo fuzz run --target=$RUST_TARGET --fuzz-dir crates/fuzz compare_skip --release -- -max_total_time=300s
234240
235241
lint:
236242
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ resolver = "2"
88

99
[workspace.package]
1010
authors = ["Samuel Colvin <[email protected]>"]
11-
version = "0.4.2"
11+
version = "0.5.0"
1212
edition = "2021"
1313
license = "MIT"
1414
keywords = ["JSON", "parsing", "deserialization", "iter"]
@@ -28,4 +28,5 @@ inherits = "release"
2828
debug = true
2929

3030
[workspace.dependencies]
31-
pyo3 = { version = "0.21.0", default-features = false}
31+
pyo3 = { version = "0.22.0" }
32+
pyo3-build-config = { version = "0.22.0" }

README.md

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@ See [the `JsonValue` docs](https://docs.rs/jiter/latest/jiter/enum.JsonValue.htm
2020
```rust
2121
use jiter::JsonValue;
2222

23-
fn main() {
24-
let json_data = r#"
25-
{
26-
"name": "John Doe",
27-
"age": 43,
28-
"phones": [
29-
"+44 1234567",
30-
"+44 2345678"
31-
]
32-
}"#;
33-
let json_value = JsonValue::parse(json_data.as_bytes(), true).unwrap();
34-
println!("{:#?}", json_value);
35-
}
23+
let json_data = r#"
24+
{
25+
"name": "John Doe",
26+
"age": 43,
27+
"phones": [
28+
"+44 1234567",
29+
"+44 2345678"
30+
]
31+
}"#;
32+
let json_value = JsonValue::parse(json_data.as_bytes(), true).unwrap();
33+
println!("{:#?}", json_value);
3634
```
3735

3836
returns:
@@ -59,35 +57,33 @@ To use [Jiter](https://docs.rs/jiter/latest/jiter/struct.Jiter.html), you need t
5957
```rust
6058
use jiter::{Jiter, NumberInt, Peek};
6159

62-
fn main() {
63-
let json_data = r#"
64-
{
65-
"name": "John Doe",
66-
"age": 43,
67-
"phones": [
68-
"+44 1234567",
69-
"+44 2345678"
70-
]
71-
}"#;
72-
let mut jiter = Jiter::new(json_data.as_bytes());
73-
assert_eq!(jiter.next_object().unwrap(), Some("name"));
74-
assert_eq!(jiter.next_str().unwrap(), "John Doe");
75-
assert_eq!(jiter.next_key().unwrap(), Some("age"));
76-
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(43));
77-
assert_eq!(jiter.next_key().unwrap(), Some("phones"));
78-
assert_eq!(jiter.next_array().unwrap(), Some(Peek::String));
79-
// we know the next value is a string as we just asserted so
80-
assert_eq!(jiter.known_str().unwrap(), "+44 1234567");
81-
assert_eq!(jiter.array_step().unwrap(), Some(Peek::String));
82-
// same again
83-
assert_eq!(jiter.known_str().unwrap(), "+44 2345678");
84-
// next we'll get `None` from `array_step` as the array is finished
85-
assert_eq!(jiter.array_step().unwrap(), None);
86-
// and `None` from `next_key` as the object is finished
87-
assert_eq!(jiter.next_key().unwrap(), None);
88-
// and we check there's nothing else in the input
89-
jiter.finish().unwrap();
90-
}
60+
let json_data = r#"
61+
{
62+
"name": "John Doe",
63+
"age": 43,
64+
"phones": [
65+
"+44 1234567",
66+
"+44 2345678"
67+
]
68+
}"#;
69+
let mut jiter = Jiter::new(json_data.as_bytes());
70+
assert_eq!(jiter.next_object().unwrap(), Some("name"));
71+
assert_eq!(jiter.next_str().unwrap(), "John Doe");
72+
assert_eq!(jiter.next_key().unwrap(), Some("age"));
73+
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(43));
74+
assert_eq!(jiter.next_key().unwrap(), Some("phones"));
75+
assert_eq!(jiter.next_array().unwrap(), Some(Peek::String));
76+
// we know the next value is a string as we just asserted so
77+
assert_eq!(jiter.known_str().unwrap(), "+44 1234567");
78+
assert_eq!(jiter.array_step().unwrap(), Some(Peek::String));
79+
// same again
80+
assert_eq!(jiter.known_str().unwrap(), "+44 2345678");
81+
// next we'll get `None` from `array_step` as the array is finished
82+
assert_eq!(jiter.array_step().unwrap(), None);
83+
// and `None` from `next_key` as the object is finished
84+
assert_eq!(jiter.next_key().unwrap(), None);
85+
// and we check there's nothing else in the input
86+
jiter.finish().unwrap();
9187
```
9288

9389
## Benchmarks

crates/jiter-python/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ homepage = {workspace = true}
1010
repository = {workspace = true}
1111

1212
[dependencies]
13-
pyo3 = { workspace = true, default-features = true, features = ["num-bigint", "auto-initialize"] }
13+
pyo3 = { workspace = true, features = ["num-bigint"] }
1414
jiter = { path = "../jiter", features = ["python"] }
1515

1616
[features]

crates/jiter-python/src/lib.rs

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,5 @@
11
use std::sync::OnceLock;
22

3-
use pyo3::prelude::*;
4-
5-
use jiter::{map_json_error, LosslessFloat, PartialMode, PythonParse, StringCacheMode};
6-
7-
#[allow(clippy::fn_params_excessive_bools)]
8-
#[pyfunction(
9-
signature = (
10-
json_data,
11-
/,
12-
*,
13-
allow_inf_nan=true,
14-
cache_mode=StringCacheMode::All,
15-
partial_mode=PartialMode::Off,
16-
catch_duplicate_keys=false,
17-
lossless_floats=false,
18-
)
19-
)]
20-
pub fn from_json<'py>(
21-
py: Python<'py>,
22-
json_data: &[u8],
23-
allow_inf_nan: bool,
24-
cache_mode: StringCacheMode,
25-
partial_mode: PartialMode,
26-
catch_duplicate_keys: bool,
27-
lossless_floats: bool,
28-
) -> PyResult<Bound<'py, PyAny>> {
29-
let parse_builder = PythonParse {
30-
allow_inf_nan,
31-
cache_mode,
32-
partial_mode,
33-
catch_duplicate_keys,
34-
lossless_floats,
35-
};
36-
parse_builder
37-
.python_parse(py, json_data)
38-
.map_err(|e| map_json_error(json_data, &e))
39-
}
40-
413
pub fn get_jiter_version() -> &'static str {
424
static JITER_VERSION: OnceLock<String> = OnceLock::new();
435

@@ -52,23 +14,63 @@ pub fn get_jiter_version() -> &'static str {
5214
})
5315
}
5416

55-
#[pyfunction]
56-
pub fn cache_clear(py: Python<'_>) {
57-
jiter::cache_clear(py);
58-
}
17+
#[pyo3::pymodule]
18+
#[pyo3(name = "jiter")]
19+
mod jiter_python {
20+
use pyo3::prelude::*;
5921

60-
#[pyfunction]
61-
pub fn cache_usage(py: Python<'_>) -> usize {
62-
jiter::cache_usage(py)
63-
}
22+
use jiter::{map_json_error, LosslessFloat, PartialMode, PythonParse, StringCacheMode};
6423

65-
#[pymodule]
66-
#[pyo3(name = "jiter")]
67-
fn jiter_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
68-
m.add("__version__", get_jiter_version())?;
69-
m.add_function(wrap_pyfunction!(from_json, m)?)?;
70-
m.add_function(wrap_pyfunction!(cache_clear, m)?)?;
71-
m.add_function(wrap_pyfunction!(cache_usage, m)?)?;
72-
m.add_class::<LosslessFloat>()?;
73-
Ok(())
24+
use super::get_jiter_version;
25+
26+
#[allow(clippy::fn_params_excessive_bools)]
27+
#[pyfunction(
28+
signature = (
29+
json_data,
30+
/,
31+
*,
32+
allow_inf_nan=true,
33+
cache_mode=StringCacheMode::All,
34+
partial_mode=PartialMode::Off,
35+
catch_duplicate_keys=false,
36+
lossless_floats=false,
37+
)
38+
)]
39+
pub fn from_json<'py>(
40+
py: Python<'py>,
41+
json_data: &[u8],
42+
allow_inf_nan: bool,
43+
cache_mode: StringCacheMode,
44+
partial_mode: PartialMode,
45+
catch_duplicate_keys: bool,
46+
lossless_floats: bool,
47+
) -> PyResult<Bound<'py, PyAny>> {
48+
let parse_builder = PythonParse {
49+
allow_inf_nan,
50+
cache_mode,
51+
partial_mode,
52+
catch_duplicate_keys,
53+
lossless_floats,
54+
};
55+
parse_builder
56+
.python_parse(py, json_data)
57+
.map_err(|e| map_json_error(json_data, &e))
58+
}
59+
60+
#[pyfunction]
61+
pub fn cache_clear(py: Python<'_>) {
62+
jiter::cache_clear(py);
63+
}
64+
65+
#[pyfunction]
66+
pub fn cache_usage(py: Python<'_>) -> usize {
67+
jiter::cache_usage(py)
68+
}
69+
70+
#[pymodule_init]
71+
fn init_jiter_python(m: &Bound<'_, PyModule>) -> PyResult<()> {
72+
m.add("__version__", get_jiter_version())?;
73+
m.add_class::<LosslessFloat>()?;
74+
Ok(())
75+
}
7476
}

crates/jiter/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ num-bigint = "0.4.4"
1616
num-traits = "0.2.16"
1717
ahash = "0.8.0"
1818
smallvec = "1.11.0"
19-
pyo3 = { version = "0.21.0", optional = true }
19+
pyo3 = { workspace = true, optional = true }
2020
lexical-parse-float = { version = "0.8.5", features = ["format"] }
2121
bitvec = "1.0.1"
2222

@@ -28,11 +28,11 @@ bencher = "0.1.5"
2828
paste = "1.0.7"
2929
serde_json = {version = "1.0.87", features = ["preserve_order", "arbitrary_precision", "float_roundtrip"]}
3030
serde = "1.0.147"
31-
pyo3 = { workspace = true, default-features=false, features = ["num-bigint", "auto-initialize"] }
31+
pyo3 = { workspace = true, features = ["num-bigint", "auto-initialize"] }
3232
codspeed-bencher-compat = "2.3.1"
3333

3434
[build-dependencies]
35-
pyo3-build-config = { version = "0.21.0", optional = true }
35+
pyo3-build-config = { workspace = true, optional = true }
3636

3737
[[test]]
3838
name = "python"

crates/jiter/README.md

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ See [the `JsonValue` docs][JsonValue] for more details.
2020
```rust
2121
use jiter::JsonValue;
2222

23-
fn main() {
24-
let json_data = r#"
25-
{
26-
"name": "John Doe",
27-
"age": 43,
28-
"phones": [
29-
"+44 1234567",
30-
"+44 2345678"
31-
]
32-
}"#;
33-
let json_value = JsonValue::parse(json_data.as_bytes(), true).unwrap();
34-
println!("{:#?}", json_value);
35-
}
23+
let json_data = r#"
24+
{
25+
"name": "John Doe",
26+
"age": 43,
27+
"phones": [
28+
"+44 1234567",
29+
"+44 2345678"
30+
]
31+
}"#;
32+
let json_value = JsonValue::parse(json_data.as_bytes(), true).unwrap();
33+
println!("{:#?}", json_value);
34+
3635
```
3736

3837
returns:
@@ -59,35 +58,33 @@ To use [Jiter], you need to know what schema you're expecting:
5958
```rust
6059
use jiter::{Jiter, NumberInt, Peek};
6160

62-
fn main() {
63-
let json_data = r#"
64-
{
65-
"name": "John Doe",
66-
"age": 43,
67-
"phones": [
68-
"+44 1234567",
69-
"+44 2345678"
70-
]
71-
}"#;
72-
let mut jiter = Jiter::new(json_data.as_bytes()).with_allow_inf_nan();
73-
assert_eq!(jiter.next_object().unwrap(), Some("name"));
74-
assert_eq!(jiter.next_str().unwrap(), "John Doe");
75-
assert_eq!(jiter.next_key().unwrap(), Some("age"));
76-
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(43));
77-
assert_eq!(jiter.next_key().unwrap(), Some("phones"));
78-
assert_eq!(jiter.next_array().unwrap(), Some(Peek::String));
79-
// we know the next value is a string as we just asserted so
80-
assert_eq!(jiter.known_str().unwrap(), "+44 1234567");
81-
assert_eq!(jiter.array_step().unwrap(), Some(Peek::String));
82-
// same again
83-
assert_eq!(jiter.known_str().unwrap(), "+44 2345678");
84-
// next we'll get `None` from `array_step` as the array is finished
85-
assert_eq!(jiter.array_step().unwrap(), None);
86-
// and `None` from `next_key` as the object is finished
87-
assert_eq!(jiter.next_key().unwrap(), None);
88-
// and we check there's nothing else in the input
89-
jiter.finish().unwrap();
90-
}
61+
let json_data = r#"
62+
{
63+
"name": "John Doe",
64+
"age": 43,
65+
"phones": [
66+
"+44 1234567",
67+
"+44 2345678"
68+
]
69+
}"#;
70+
let mut jiter = Jiter::new(json_data.as_bytes()).with_allow_inf_nan();
71+
assert_eq!(jiter.next_object().unwrap(), Some("name"));
72+
assert_eq!(jiter.next_str().unwrap(), "John Doe");
73+
assert_eq!(jiter.next_key().unwrap(), Some("age"));
74+
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(43));
75+
assert_eq!(jiter.next_key().unwrap(), Some("phones"));
76+
assert_eq!(jiter.next_array().unwrap(), Some(Peek::String));
77+
// we know the next value is a string as we just asserted so
78+
assert_eq!(jiter.known_str().unwrap(), "+44 1234567");
79+
assert_eq!(jiter.array_step().unwrap(), Some(Peek::String));
80+
// same again
81+
assert_eq!(jiter.known_str().unwrap(), "+44 2345678");
82+
// next we'll get `None` from `array_step` as the array is finished
83+
assert_eq!(jiter.array_step().unwrap(), None);
84+
// and `None` from `next_key` as the object is finished
85+
assert_eq!(jiter.next_key().unwrap(), None);
86+
// and we check there's nothing else in the input
87+
jiter.finish().unwrap();
9188
```
9289

9390
## Benchmarks

crates/jiter/benches/python.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::Read;
55

66
use pyo3::Python;
77

8-
use jiter::{cache_clear, PartialMode, PythonParse, StringCacheMode};
8+
use jiter::{cache_clear, PythonParse, StringCacheMode};
99

1010
fn python_parse_numeric(bench: &mut Bencher) {
1111
Python::with_gil(|py| {

0 commit comments

Comments
 (0)