Skip to content

Commit

Permalink
Bump PyO3 0.20.0 -> 0.21.0 and use new API
Browse files Browse the repository at this point in the history
Closes #537
  • Loading branch information
wtfsck committed Mar 27, 2024
1 parent c581ec5 commit df26469
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ sealed class GeneratedMethodInfo {
ctx.Writer.Write("u64");
break;
case MethodArgType.ByteSlice:
ctx.Writer.Write("&PyAny");
ctx.Writer.Write("&Bound<'_, PyAny>");
break;
case MethodArgType.ByteArray:
case MethodArgType.WordArray:
Expand Down
2 changes: 1 addition & 1 deletion src/csharp/Intel/Generator/Misc/Python/PyClassParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ sealed class Lines {
};
var newArgs = new List<PyMethodArg> {
new PyMethodArg(selfArgName, "&self", isSelf: true),
new PyMethodArg("other", "&PyAny", isSelf: false),
new PyMethodArg("other", "&Bound<'_, PyAny>", isSelf: false),
};
yield return new PyMethod(opName, method.DocComments, method.Attributes, newArgs, "bool");
}
Expand Down
2 changes: 1 addition & 1 deletion src/csharp/Intel/Generator/Misc/Python/PyiGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ sealed class PyiGen {
return "str";
case "PyRef<'_, Self>" or "PyRefMut<'_, Self>" or "Self":
return pyClass.Name;
case "&PyAny":
case "&Bound<'_, PyAny>":
return "Any";
default:
if (ParseUtils.TryRemovePrefixSuffix(rustType, "PyRef<'_, ", ">", out extractedType))
Expand Down
2 changes: 1 addition & 1 deletion src/rust/iced-x86-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["cdylib"]
bincode = "1.3.3"

[dependencies.pyo3]
version = "0.20.0"
version = "0.21.0"
default-features = false
features = ["macros", "extension-module", "abi3-py38"]

Expand Down
4 changes: 2 additions & 2 deletions src/rust/iced-x86-py/src/block_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ impl BlockEncoder {
/// Raises:
/// ValueError: If one or more instructions couldn't be encoded
#[pyo3(text_signature = "($self, rip)")]
fn encode<'py>(&mut self, py: Python<'py>, rip: u64) -> PyResult<&'py PyBytes> {
fn encode<'py>(&mut self, py: Python<'py>, rip: u64) -> PyResult<Bound<'py, PyBytes>> {
let block = iced_x86::InstructionBlock::new(&self.instructions, rip);
iced_x86::BlockEncoder::encode(self.bitness, block, self.options)
.map_or_else(|e| Err(to_value_error(e)), |result| Ok(PyBytes::new(py, &result.code_buffer)))
.map_or_else(|e| Err(to_value_error(e)), |result| Ok(PyBytes::new_bound(py, &result.code_buffer)))
}
}
2 changes: 1 addition & 1 deletion src/rust/iced-x86-py/src/constant_offsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl ConstantOffsets {
///
/// This is identical to :class:`ConstantOffsets.copy`
#[pyo3(text_signature = "($self, memo)")]
fn __deepcopy__(&self, _memo: &PyAny) -> Self {
fn __deepcopy__(&self, _memo: &Bound<'_, PyAny>) -> Self {
*self
}

Expand Down
23 changes: 12 additions & 11 deletions src/rust/iced-x86-py/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::constant_offsets::ConstantOffsets;
use crate::instruction::Instruction;
use crate::utils::to_value_error;
use core::slice;
use pyo3::class::iter::IterNextOutput;
use pyo3::exceptions::PyTypeError;
use pyo3::gc::PyVisit;
use pyo3::prelude::*;
Expand All @@ -14,7 +13,8 @@ use pyo3::PyTraverseError;

enum DecoderDataRef {
None,
Vec(Vec<u8>),
Vec(#[allow(dead_code)] Vec<u8>),
#[allow(dead_code)]
PyObj(PyObject),
}

Expand Down Expand Up @@ -110,15 +110,16 @@ impl Decoder {
#[new]
#[pyo3(text_signature = "(bitness, data, options = 0, ip = 0)")]
#[pyo3(signature = (bitness, data, options = 0, ip = 0))]
fn new(bitness: u32, data: &PyAny, options: u32, ip: u64) -> PyResult<Self> {
fn new(bitness: u32, data: &Bound<'_, PyAny>, options: u32, ip: u64) -> PyResult<Self> {
// #[pyo3(signature = (...))] line assumption
const _: () = assert!(iced_x86::DecoderOptions::NONE == 0);

let (data_ref, decoder_data): (DecoderDataRef, &'static [u8]) = if let Ok(bytes) = <PyBytes as PyTryFrom>::try_from(data) {
let slice_data = bytes.as_bytes();
let decoder_data = unsafe { slice::from_raw_parts(slice_data.as_ptr(), slice_data.len()) };
(DecoderDataRef::PyObj(bytes.into()), decoder_data)
} else if let Ok(bytearray) = <PyByteArray as PyTryFrom>::try_from(data) {
let (data_ref, decoder_data): (DecoderDataRef, &'static [u8]) = if let Ok(bytes) = data.downcast::<PyBytes>() {
//TODO: try to use a reference to the original data like we did with PyO3 0.20 and earlier, see previous commit
let vec_data: Vec<_> = bytes.as_bytes().into();
let decoder_data = unsafe { slice::from_raw_parts(vec_data.as_ptr(), vec_data.len()) };
(DecoderDataRef::Vec(vec_data), decoder_data)
} else if let Ok(bytearray) = data.downcast::<PyByteArray>() {
//TODO: support bytearray without copying its data by getting a ref to its data every time the Decoder is used (also update the ctor args docs)
let vec_data: Vec<_> = unsafe { bytearray.as_bytes().into() };
let decoder_data = unsafe { slice::from_raw_parts(vec_data.as_ptr(), vec_data.len()) };
Expand Down Expand Up @@ -400,11 +401,11 @@ impl Decoder {
slf
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> IterNextOutput<Instruction, ()> {
fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<Instruction> {
if slf.can_decode() {
IterNextOutput::Yield(slf.decode())
Some(slf.decode())
} else {
IterNextOutput::Return(())
None
}
}
}
4 changes: 2 additions & 2 deletions src/rust/iced-x86-py/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ impl Encoder {
/// Returns:
/// bytes: The encoded instructions
#[pyo3(text_signature = "($self)")]
fn take_buffer<'py>(&mut self, py: Python<'py>) -> &'py PyBytes {
fn take_buffer<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyBytes> {
let buffer = self.encoder.take_buffer();
PyBytes::new(py, &buffer)
PyBytes::new_bound(py, &buffer)
}

/// Gets the offsets of the constants (memory displacement and immediate) in the encoded instruction.
Expand Down
4 changes: 2 additions & 2 deletions src/rust/iced-x86-py/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl UsedRegister {
///
/// This is identical to :class:`UsedRegister.copy`
#[pyo3(text_signature = "($self, memo)")]
fn __deepcopy__(&self, _memo: &PyAny) -> Self {
fn __deepcopy__(&self, _memo: &Bound<'_, PyAny>) -> Self {
*self
}

Expand Down Expand Up @@ -168,7 +168,7 @@ impl UsedMemory {
///
/// This is identical to :class:`UsedMemory.copy`
#[pyo3(text_signature = "($self, memo)")]
fn __deepcopy__(&self, _memo: &PyAny) -> Self {
fn __deepcopy__(&self, _memo: &Bound<'_, PyAny>) -> Self {
*self
}

Expand Down
6 changes: 3 additions & 3 deletions src/rust/iced-x86-py/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Instruction {
/// bytes: The unpickled state
#[pyo3(text_signature = "($self)")]
fn __getstate__(&self, py: Python<'_>) -> PyResult<PyObject> {
let state = PyBytes::new(py, &serialize(&self.instr).map_err(to_value_error)?).to_object(py);
let state = PyBytes::new_bound(py, &serialize(&self.instr).map_err(to_value_error)?).to_object(py);
Ok(state)
}

Expand All @@ -214,7 +214,7 @@ impl Instruction {
///
/// This is identical to :class:`Instruction.copy`
#[pyo3(text_signature = "($self, memo)")]
fn __deepcopy__(&self, _memo: &PyAny) -> Self {
fn __deepcopy__(&self, _memo: &Bound<'_, PyAny>) -> Self {
*self
}

Expand Down Expand Up @@ -4735,7 +4735,7 @@ impl Instruction {
#[rustfmt::skip]
#[staticmethod]
#[pyo3(text_signature = "(data)")]
fn create_declare_byte(data: &PyAny) -> PyResult<Self> {
fn create_declare_byte(data: &Bound<'_, PyAny>) -> PyResult<Self> {
let data = unsafe { get_temporary_byte_array_ref(data)? };
Ok(Instruction { instr: iced_x86::Instruction::with_declare_byte(data).map_err(to_value_error)? })
}
Expand Down
2 changes: 1 addition & 1 deletion src/rust/iced-x86-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ struct TupleType {}
// GENERATOR-END: EnumClassDefs

#[pymodule]
fn _iced_x86_py(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn _iced_x86_py(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
// GENERATOR-BEGIN: ClassExport
// ⚠️This was generated by GENERATOR!🦹‍♂️
m.add_class::<BlockEncoder>()?;
Expand Down
2 changes: 1 addition & 1 deletion src/rust/iced-x86-py/src/memory_operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl MemoryOperand {
///
/// This is identical to :class:`MemoryOperand.copy`
#[pyo3(text_signature = "($self, memo)")]
fn __deepcopy__(&self, _memo: &PyAny) -> Self {
fn __deepcopy__(&self, _memo: &Bound<'_, PyAny>) -> Self {
*self
}

Expand Down
6 changes: 3 additions & 3 deletions src/rust/iced-x86-py/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::fmt::Display;

/// Gets a ref to the bytes or an error. It assumes the input data is not modified
/// if it's mutable (eg. if it's a `bytearray`)
pub(crate) unsafe fn get_temporary_byte_array_ref(data: &PyAny) -> PyResult<&[u8]> {
if let Ok(bytes) = <PyBytes as PyTryFrom>::try_from(data) {
pub(crate) unsafe fn get_temporary_byte_array_ref<'a>(data: &'a Bound<'_, PyAny>) -> PyResult<&'a [u8]> {
if let Ok(bytes) = data.downcast::<PyBytes>() {
Ok(bytes.as_bytes())
} else if let Ok(bytearray) = <PyByteArray as PyTryFrom>::try_from(data) {
} else if let Ok(bytearray) = data.downcast::<PyByteArray>() {
Ok(bytearray.as_bytes())
} else {
//TODO: support memoryview (also update docs and Decoder ctor and the message below)
Expand Down

0 comments on commit df26469

Please sign in to comment.