Skip to content

Commit

Permalink
feat: Support Python 3.12
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Mar 3, 2024
1 parent f0828cb commit fa29f2a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-20.04, macos-12, windows-2022]
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']

name: Python ${{ matrix.python-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand Down
4 changes: 4 additions & 0 deletions bindings/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- Support for Python 3.12 [#439](https://github.com/Stranger6667/jsonschema-rs/issues/439)

### Changed

- Expose drafts 2019-09 and 2020-12 to Python
Expand Down
6 changes: 3 additions & 3 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use jsonschema::{paths::JSONPointer, Draft};
use pyo3::{
exceptions::{self, PyValueError},
ffi::PyUnicode_AsUTF8AndSize,
prelude::*,
types::{PyAny, PyList, PyType},
wrap_pyfunction,
Expand All @@ -28,7 +29,6 @@ extern crate pyo3_built;

mod ffi;
mod ser;
mod string;
mod types;

const DRAFT7: u8 = 7;
Expand Down Expand Up @@ -385,8 +385,8 @@ impl JSONSchema {
)))
} else {
let mut str_size: pyo3::ffi::Py_ssize_t = 0;
let uni = unsafe { string::read_utf8_from_str(obj_ptr, &mut str_size) };
let slice = unsafe { std::slice::from_raw_parts(uni, str_size as usize) };
let ptr = unsafe { PyUnicode_AsUTF8AndSize(obj_ptr, &mut str_size) };
let slice = unsafe { std::slice::from_raw_parts(ptr.cast::<u8>(), str_size as usize) };
let raw_schema = serde_json::from_slice(slice)
.map_err(|error| PyValueError::new_err(format!("Invalid string: {}", error)))?;
let options = make_options(draft, with_meta_schemas)?;
Expand Down
12 changes: 6 additions & 6 deletions bindings/python/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pyo3::{
exceptions,
ffi::{
PyDictObject, PyFloat_AS_DOUBLE, PyList_GET_ITEM, PyList_GET_SIZE, PyLong_AsLongLong,
PyObject_GetAttr, PyTuple_GET_ITEM, PyTuple_GET_SIZE, Py_TYPE,
PyObject_GetAttr, PyTuple_GET_ITEM, PyTuple_GET_SIZE, PyUnicode_AsUTF8AndSize, Py_TYPE,
},
prelude::*,
types::PyAny,
Expand All @@ -12,7 +12,7 @@ use serde::{
Serializer,
};

use crate::{ffi, string, types};
use crate::{ffi, types};
use std::ffi::CStr;

pub const RECURSION_LIMIT: u8 = 255;
Expand Down Expand Up @@ -123,10 +123,10 @@ impl Serialize for SerializePyObject {
match self.object_type {
ObjectType::Str => {
let mut str_size: pyo3::ffi::Py_ssize_t = 0;
let uni = unsafe { string::read_utf8_from_str(self.object, &mut str_size) };
let ptr = unsafe { PyUnicode_AsUTF8AndSize(self.object, &mut str_size) };
let slice = unsafe {
std::str::from_utf8_unchecked(std::slice::from_raw_parts(
uni,
ptr.cast::<u8>(),
str_size as usize,
))
};
Expand Down Expand Up @@ -156,10 +156,10 @@ impl Serialize for SerializePyObject {
pyo3::ffi::PyDict_Next(self.object, &mut pos, &mut key, &mut value);
}
check_type_is_str(key)?;
let uni = unsafe { string::read_utf8_from_str(key, &mut str_size) };
let ptr = unsafe { PyUnicode_AsUTF8AndSize(key, &mut str_size) };
let slice = unsafe {
std::str::from_utf8_unchecked(std::slice::from_raw_parts(
uni,
ptr.cast::<u8>(),
str_size as usize,
))
};
Expand Down
48 changes: 0 additions & 48 deletions bindings/python/src/string.rs

This file was deleted.

0 comments on commit fa29f2a

Please sign in to comment.