Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 15 additions & 53 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ crate-type = ["cdylib"]
parsepatch = "0.3"

[dependencies.pyo3]
version = "0.22"
version = "0.28"
features = ["extension-module"]
32 changes: 16 additions & 16 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use parsepatch::{BinaryHunk, FileMode, FileOp};
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::types::{PyByteArray, PyBytes, PyString, PyTuple};
use pyo3::types::{PyByteArray, PyBytes, PyString};
use pyo3::Python;

pub(crate) enum Bytes<'a> {
Expand All @@ -10,34 +10,34 @@ pub(crate) enum Bytes<'a> {
}

#[inline(always)]
pub fn create_mode(old: Option<u32>, new: Option<u32>, py: &Python) -> PyObject {
let dict = PyDict::new_bound(*py);
pub fn create_mode(old: Option<u32>, new: Option<u32>, py: &Python) -> Py<PyAny> {
let dict = PyDict::new(*py);
if let Some(old) = old {
dict.set_item("old", old).unwrap();
}
if let Some(new) = new {
dict.set_item("new", new).unwrap();
}
dict.to_object(*py)
dict.into_any().unbind()
}

#[inline(always)]
pub fn create_file_mode(modes: Option<FileMode>, py: &Python) -> PyObject {
let dict = PyDict::new_bound(*py);
pub fn create_file_mode(modes: Option<FileMode>, py: &Python) -> Py<PyAny> {
let dict = PyDict::new(*py);
if let Some(modes) = modes {
dict.set_item("old", modes.old).unwrap();
dict.set_item("new", modes.new).unwrap();
}
dict.to_object(*py)
dict.into_any().unbind()
}

#[inline(always)]
pub fn create_bin_size(h: BinaryHunk, py: &Python) -> PyObject {
pub fn create_bin_size(h: BinaryHunk, py: &Python) -> Py<PyAny> {
let x = match h {
BinaryHunk::Literal(s) => ("literal", s),
BinaryHunk::Delta(s) => ("delta", s),
};
PyTuple::new_bound(*py, &[x.0.to_object(*py), x.1.to_object(*py)]).to_object(*py)
(x.0, x.1).into_pyobject(*py).unwrap().into_any().unbind()
}

#[inline(always)]
Expand Down Expand Up @@ -100,24 +100,24 @@ pub fn set_info(

if let Some(mut binary_sizes) = binary_sizes {
diff.set_item("binary", true).unwrap();
let sizes: Vec<PyObject> = binary_sizes
let sizes: Vec<Py<PyAny>> = binary_sizes
.drain(..)
.map(move |x| create_bin_size(x, py))
.collect();
diff.set_item("binary_hunk_size", sizes.to_object(*py))
.unwrap();
diff.set_item("binary_hunk_size", sizes).unwrap();
} else {
diff.set_item("binary", false).unwrap();
}
}

#[inline(always)]
pub(crate) fn get_bytes<'a>(py: Python<'a>, bytes: &'a PyObject) -> Option<Bytes<'a>> {
if let Ok(bytes) = bytes.bind(py).downcast::<PyBytes>() {
pub(crate) fn get_bytes<'a>(py: Python<'a>, bytes: &'a Py<PyAny>) -> Option<Bytes<'a>> {
let bytes = bytes.bind(py);
if let Ok(bytes) = bytes.cast::<PyBytes>() {
Some(Bytes::Slice(bytes.as_bytes()))
} else if let Ok(bytes) = bytes.bind(py).downcast::<PyString>() {
} else if let Ok(bytes) = bytes.cast::<PyString>() {
Some(Bytes::Slice(bytes.to_str().unwrap().as_bytes()))
} else if let Ok(bytes) = bytes.bind(py).downcast::<PyByteArray>() {
} else if let Ok(bytes) = bytes.cast::<PyByteArray>() {
Some(Bytes::Vec(bytes.to_vec()))
} else {
None
Expand Down
12 changes: 6 additions & 6 deletions src/counts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use parsepatch::{BinaryHunk, Diff, FileMode, FileOp, Patch};
use pyo3::prelude::PyDictMethods;
use pyo3::types::PyDict;
use pyo3::{Bound, PyObject, PyResult, Python, ToPyObject};
use pyo3::{Bound, IntoPyObject, Py, PyAny, PyResult, Python};

pub struct PyDiff<'a> {
py: Python<'a>,
Expand All @@ -14,7 +14,7 @@ impl<'a> PyDiff<'a> {
fn new(py: Python<'a>) -> Self {
PyDiff {
py,
diff: PyDict::new_bound(py),
diff: PyDict::new(py),
add: 0,
del: 0,
}
Expand Down Expand Up @@ -45,19 +45,19 @@ impl<'a> Patch<PyDiff<'a>> for PyPatch<'a> {
}

impl<'a> PyPatch<'a> {
pub fn get_result(mut self) -> PyResult<PyObject> {
pub fn get_result(mut self) -> PyResult<Py<PyAny>> {
let py = self.py;
let diffs: Vec<PyObject> = self
let diffs: Vec<Py<PyAny>> = self
.diffs
.drain(..)
.map(move |x| {
x.diff.set_item("added_lines", x.add).unwrap();
x.diff.set_item("deleted_lines", x.del).unwrap();
x.diff.to_object(py)
x.diff.into_any().unbind()
})
.collect();

Ok(diffs.to_object(self.py))
Ok(diffs.into_pyobject(py)?.into_any().unbind())
}
}

Expand Down
38 changes: 20 additions & 18 deletions src/diffs.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
use parsepatch::{BinaryHunk, Diff, FileMode, FileOp, Patch};
use pyo3::prelude::PyDictMethods;
use pyo3::types::{PyBytes, PyDict, PyTuple};
use pyo3::{Bound, PyObject, PyResult, Python, ToPyObject};
use pyo3::{Bound, IntoPyObject, Py, PyAny, PyResult, Python};

pub struct PyDiff<'a> {
py: Python<'a>,
diff: Bound<'a, PyDict>,
lines: Vec<PyObject>,
hunks: Vec<Vec<PyObject>>,
lines: Vec<Py<PyAny>>,
hunks: Vec<Vec<Py<PyAny>>>,
has_hunks: bool,
}

impl<'a> PyDiff<'a> {
fn new(py: Python<'a>, has_hunks: bool) -> Self {
PyDiff {
py,
diff: PyDict::new_bound(py),
diff: PyDict::new(py),
lines: Vec::new(),
hunks: Vec::new(),
has_hunks,
}
}

fn get_line(&self, line: u32) -> PyObject {
fn get_line(&self, line: u32) -> Py<PyAny> {
if line == 0 {
self.py.None()
} else {
line.to_object(self.py)
line.into_pyobject(self.py).unwrap().into_any().unbind()
}
}
}
Expand Down Expand Up @@ -57,28 +57,28 @@ impl<'a> Patch<PyDiff<'a>> for PyPatch<'a> {
}

impl<'a> PyPatch<'a> {
pub fn get_result(mut self) -> PyResult<PyObject> {
pub fn get_result(mut self) -> PyResult<Py<PyAny>> {
let py = self.py;
if self.hunks {
let diffs: Vec<PyObject> = self
let diffs: Vec<Py<PyAny>> = self
.diffs
.drain(..)
.map(move |x| {
x.diff.set_item("hunks", x.hunks.to_object(py)).unwrap();
x.diff.to_object(py)
x.diff.set_item("hunks", x.hunks).unwrap();
x.diff.into_any().unbind()
})
.collect();
Ok(diffs.to_object(self.py))
Ok(diffs.into_pyobject(py)?.into_any().unbind())
} else {
let diffs: Vec<PyObject> = self
let diffs: Vec<Py<PyAny>> = self
.diffs
.drain(..)
.map(move |x| {
x.diff.set_item("lines", x.lines.to_object(py)).unwrap();
x.diff.to_object(py)
x.diff.set_item("lines", x.lines).unwrap();
x.diff.into_any().unbind()
})
.collect();
Ok(diffs.to_object(self.py))
Ok(diffs.into_pyobject(py)?.into_any().unbind())
}
}
}
Expand All @@ -104,15 +104,17 @@ impl<'a> Diff for PyDiff<'a> {
}

fn add_line(&mut self, old_line: u32, new_line: u32, line: &[u8]) {
let line = PyTuple::new_bound(
let line = PyTuple::new(
self.py,
&[
self.get_line(old_line),
self.get_line(new_line),
PyBytes::new_bound(self.py, line).to_object(self.py),
PyBytes::new(self.py, line).into_any().unbind(),
],
)
.to_object(self.py);
.unwrap()
.into_any()
.unbind();

if self.has_hunks {
self.hunks.last_mut().unwrap().push(line);
Expand Down
Loading