diff --git a/src/csharp/Intel/Generator/Encoder/Python/PythonInstrCreateGen.cs b/src/csharp/Intel/Generator/Encoder/Python/PythonInstrCreateGen.cs index 0ac2dbb05..cccc8703e 100644 --- a/src/csharp/Intel/Generator/Encoder/Python/PythonInstrCreateGen.cs +++ b/src/csharp/Intel/Generator/Encoder/Python/PythonInstrCreateGen.cs @@ -136,7 +136,7 @@ static void WriteMethodDeclArgs(in GenerateMethodContext ctx) { ctx.Writer.Write("u64"); break; case MethodArgType.ByteSlice: - ctx.Writer.Write("&PyAny"); + ctx.Writer.Write("&Bound<'_, PyAny>"); break; case MethodArgType.ByteArray: case MethodArgType.WordArray: diff --git a/src/csharp/Intel/Generator/Misc/Python/PyClassParser.cs b/src/csharp/Intel/Generator/Misc/Python/PyClassParser.cs index a4f0ea2a4..506ae6a03 100644 --- a/src/csharp/Intel/Generator/Misc/Python/PyClassParser.cs +++ b/src/csharp/Intel/Generator/Misc/Python/PyClassParser.cs @@ -582,7 +582,7 @@ IEnumerable ReadMethod(PyClass pyClass, string fullLine) { }; var newArgs = new List { 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"); } diff --git a/src/csharp/Intel/Generator/Misc/Python/PyiGen.cs b/src/csharp/Intel/Generator/Misc/Python/PyiGen.cs index cd2aef12d..18607d9a7 100644 --- a/src/csharp/Intel/Generator/Misc/Python/PyiGen.cs +++ b/src/csharp/Intel/Generator/Misc/Python/PyiGen.cs @@ -359,7 +359,7 @@ static string GetType(PyClass pyClass, string methodName, string rustType, strin 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)) diff --git a/src/rust/iced-x86-py/Cargo.toml b/src/rust/iced-x86-py/Cargo.toml index d9bf24095..393118a93 100644 --- a/src/rust/iced-x86-py/Cargo.toml +++ b/src/rust/iced-x86-py/Cargo.toml @@ -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"] diff --git a/src/rust/iced-x86-py/src/block_encoder.rs b/src/rust/iced-x86-py/src/block_encoder.rs index 1bf67491b..34f5ce33d 100644 --- a/src/rust/iced-x86-py/src/block_encoder.rs +++ b/src/rust/iced-x86-py/src/block_encoder.rs @@ -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> { 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))) } } diff --git a/src/rust/iced-x86-py/src/constant_offsets.rs b/src/rust/iced-x86-py/src/constant_offsets.rs index 5e7e46dca..5ca7b7ab3 100644 --- a/src/rust/iced-x86-py/src/constant_offsets.rs +++ b/src/rust/iced-x86-py/src/constant_offsets.rs @@ -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 } diff --git a/src/rust/iced-x86-py/src/decoder.rs b/src/rust/iced-x86-py/src/decoder.rs index 207f73c62..c6db425e8 100644 --- a/src/rust/iced-x86-py/src/decoder.rs +++ b/src/rust/iced-x86-py/src/decoder.rs @@ -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::*; @@ -14,7 +13,8 @@ use pyo3::PyTraverseError; enum DecoderDataRef { None, - Vec(Vec), + Vec(#[allow(dead_code)] Vec), + #[allow(dead_code)] PyObj(PyObject), } @@ -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 { + fn new(bitness: u32, data: &Bound<'_, PyAny>, options: u32, ip: u64) -> PyResult { // #[pyo3(signature = (...))] line assumption const _: () = assert!(iced_x86::DecoderOptions::NONE == 0); - let (data_ref, decoder_data): (DecoderDataRef, &'static [u8]) = if let Ok(bytes) = ::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) = ::try_from(data) { + let (data_ref, decoder_data): (DecoderDataRef, &'static [u8]) = if let Ok(bytes) = data.downcast::() { + //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::() { //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()) }; @@ -400,11 +401,11 @@ impl Decoder { slf } - fn __next__(mut slf: PyRefMut<'_, Self>) -> IterNextOutput { + fn __next__(mut slf: PyRefMut<'_, Self>) -> Option { if slf.can_decode() { - IterNextOutput::Yield(slf.decode()) + Some(slf.decode()) } else { - IterNextOutput::Return(()) + None } } } diff --git a/src/rust/iced-x86-py/src/encoder.rs b/src/rust/iced-x86-py/src/encoder.rs index 635d667ee..96245b519 100644 --- a/src/rust/iced-x86-py/src/encoder.rs +++ b/src/rust/iced-x86-py/src/encoder.rs @@ -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. diff --git a/src/rust/iced-x86-py/src/info.rs b/src/rust/iced-x86-py/src/info.rs index fcb20fff5..4a62ed143 100644 --- a/src/rust/iced-x86-py/src/info.rs +++ b/src/rust/iced-x86-py/src/info.rs @@ -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 } @@ -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 } diff --git a/src/rust/iced-x86-py/src/instruction.rs b/src/rust/iced-x86-py/src/instruction.rs index 9481825cf..5a7d99c3a 100644 --- a/src/rust/iced-x86-py/src/instruction.rs +++ b/src/rust/iced-x86-py/src/instruction.rs @@ -189,7 +189,7 @@ impl Instruction { /// bytes: The unpickled state #[pyo3(text_signature = "($self)")] fn __getstate__(&self, py: Python<'_>) -> PyResult { - 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) } @@ -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 } @@ -4735,7 +4735,7 @@ impl Instruction { #[rustfmt::skip] #[staticmethod] #[pyo3(text_signature = "(data)")] - fn create_declare_byte(data: &PyAny) -> PyResult { + fn create_declare_byte(data: &Bound<'_, PyAny>) -> PyResult { let data = unsafe { get_temporary_byte_array_ref(data)? }; Ok(Instruction { instr: iced_x86::Instruction::with_declare_byte(data).map_err(to_value_error)? }) } diff --git a/src/rust/iced-x86-py/src/lib.rs b/src/rust/iced-x86-py/src/lib.rs index 8357a1ca9..0c0a08cb9 100644 --- a/src/rust/iced-x86-py/src/lib.rs +++ b/src/rust/iced-x86-py/src/lib.rs @@ -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::()?; diff --git a/src/rust/iced-x86-py/src/memory_operand.rs b/src/rust/iced-x86-py/src/memory_operand.rs index af83d5f5a..db6212c62 100644 --- a/src/rust/iced-x86-py/src/memory_operand.rs +++ b/src/rust/iced-x86-py/src/memory_operand.rs @@ -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 } diff --git a/src/rust/iced-x86-py/src/utils.rs b/src/rust/iced-x86-py/src/utils.rs index e6bc8d01f..fbf5e3a8b 100644 --- a/src/rust/iced-x86-py/src/utils.rs +++ b/src/rust/iced-x86-py/src/utils.rs @@ -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) = ::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::() { Ok(bytes.as_bytes()) - } else if let Ok(bytearray) = ::try_from(data) { + } else if let Ok(bytearray) = data.downcast::() { Ok(bytearray.as_bytes()) } else { //TODO: support memoryview (also update docs and Decoder ctor and the message below)