Skip to content

Commit

Permalink
Update to newest PyO3
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed Nov 17, 2024
1 parent baf21b7 commit 7853bc3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 50 deletions.
52 changes: 26 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rlbot-flatbuffers-py"
version = "0.11.0"
version = "0.11.1"
edition = "2021"
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
repository = "https://github.com/VirxEC/rlbot_flatbuffers_py"
Expand All @@ -19,7 +19,7 @@ name = "rlbot_flatbuffers"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.22.0", features = ["py-clone"] }
pyo3 = { version = "0.23.0", features = ["py-clone"] }
serde = "1.0.197"
flatbuffers = "24.3.25"
# get-size appears to be unmaintained but it's too useful here
Expand Down
17 changes: 6 additions & 11 deletions codegen/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,13 @@ impl StructBindGenerator {
self,
" fn __match_args__(py: Python) -> Bound<pyo3::types::PyTuple> {"
);
write_str!(self, " pyo3::types::PyTuple::new_bound(py, [");
write_str!(self, " pyo3::types::PyTuple::new(py, [");

for variable_info in &self.types {
write_fmt!(self, " \"{}\",", variable_info.name);
}

write_str!(self, " ])");
write_str!(self, " ]).unwrap()");
write_str!(self, " }");
}

Expand Down Expand Up @@ -613,14 +613,11 @@ impl StructBindGenerator {
write_str!(self, " let offset = flat_t.pack(&mut builder);");
write_str!(self, " builder.finish(offset, None);");
write_str!(self, "");
write_str!(
self,
" PyBytes::new_bound(py, builder.finished_data())"
);
write_str!(self, " PyBytes::new(py, builder.finished_data())");
} else {
write_str!(self, " let item = flat_t.pack();");
write_str!(self, "");
write_str!(self, " PyBytes::new_bound(py, &item.0)");
write_str!(self, " PyBytes::new(py, &item.0)");
}

write_str!(self, " }");
Expand Down Expand Up @@ -797,9 +794,7 @@ impl Generator for StructBindGenerator {
let variable_name = variable_info.name.as_str();

let end = match &variable_info.rust_type {
RustType::Vec(InnerVecType::U8) => {
Cow::Borrowed("PyBytes::new_bound(py, &[]).unbind()")
}
RustType::Vec(InnerVecType::U8) => Cow::Borrowed("PyBytes::new(py, &[]).unbind()"),
RustType::Vec(_) => Cow::Borrowed("Vec::new()"),
RustType::Option(_, _) => Cow::Borrowed("None"),
RustType::Union(inner_type)
Expand Down Expand Up @@ -869,7 +864,7 @@ impl Generator for StructBindGenerator {
RustType::Vec(InnerVecType::U8) => {
write_fmt!(
self,
" {variable_name}: PyBytes::new_bound(py, &flat_t.{variable_name}).unbind(),"
" {variable_name}: PyBytes::new(py, &flat_t.{variable_name}).unbind(),"
)
}
RustType::Vec(InnerVecType::String | InnerVecType::Base(_)) => {
Expand Down
9 changes: 3 additions & 6 deletions codegen/unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl UnionBindGenerator {

let file_contents = vec![
Cow::Borrowed("use crate::{generated::rlbot::flat, FromGil};"),
Cow::Borrowed("use pyo3::{pyclass, pymethods, Py, PyObject, Python, ToPyObject};"),
Cow::Borrowed("use pyo3::{pyclass, pymethods, Bound, Py, PyObject, Python};"),
Cow::Borrowed(""),
];

Expand Down Expand Up @@ -61,10 +61,7 @@ impl UnionBindGenerator {
write_str!(self, " }");
write_str!(self, "");
write_str!(self, " #[getter(item)]");
write_str!(
self,
" pub fn get(&self, py: Python) -> Option<PyObject> {"
);
write_str!(self, " pub fn get(&self) -> Option<&PyObject> {");
write_str!(self, " match self.item.as_ref() {");

for variable_info in &self.types {
Expand All @@ -75,7 +72,7 @@ impl UnionBindGenerator {
} else {
write_fmt!(
self,
" Some({}Union::{variable_name}(item)) => Some(item.to_object(py)),",
" Some({}Union::{variable_name}(item)) => Some(item.as_any()),",
self.struct_name
);
}
Expand Down
2 changes: 1 addition & 1 deletion flatbuffers-schema
Submodule flatbuffers-schema updated 0 files
4 changes: 2 additions & 2 deletions pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def random_player_config():
return PlayerConfiguration(
variety=Psyonix(PsyonixSkill.AllStar),
name=random_string(),
location=random_string(),
root_dir=random_string(),
run_command=random_string(),
loadout=PlayerLoadout(
loadout_paint=LoadoutPaint(),
Expand Down Expand Up @@ -135,7 +135,7 @@ def random_script_config():
renderPolyLine = RenderMessage(
PolyLine3D(
[Vector3() for _ in range(2048)],
Color(255),
Color(a=255),
),
)

Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
clippy::derivable_impls,
clippy::unnecessary_cast,
clippy::size_of_in_element_count,
clippy::needless_lifetimes,
clippy::too_long_first_doc_paragraph,
non_snake_case,
unused_imports
Expand Down Expand Up @@ -88,20 +89,23 @@ impl<T: Default + PyClass + Into<PyClassInitializer<T>>> PyDefault for T {
}
}

#[inline(never)]
pub fn get_empty_pybytes() -> Py<PyBytes> {
Python::with_gil(|py| PyBytes::new_bound(py, &[]).unbind())
Python::with_gil(|py| PyBytes::new(py, &[]).unbind())
}

pub fn get_py_default<T: PyDefault>() -> Py<T> {
Python::with_gil(|py| T::py_default(py))
}

#[must_use]
#[inline(never)]
pub fn none_str() -> String {
String::from("None")
}

#[must_use]
#[inline(never)]
pub const fn bool_to_str(b: bool) -> &'static str {
if b {
"True"
Expand Down Expand Up @@ -160,7 +164,7 @@ macro_rules! pynamedmodule {
fn $name(py: Python, m: Bound<PyModule>) -> PyResult<()> {
$(m.add_class::<$class_name>()?);*;
$(m.add($var_name, $value)?);*;
$(m.add(stringify!($except), py.get_type_bound::<$except>())?);*;
$(m.add(stringify!($except), py.get_type::<$except>())?);*;
Ok(())
}
};
Expand Down

0 comments on commit 7853bc3

Please sign in to comment.