Skip to content

Commit

Permalink
chore(py): upgrade pyo3 to version 0.23.3
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Dec 16, 2024
1 parent f89ad23 commit c146b6f
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 43 deletions.
106 changes: 94 additions & 12 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ doc = false
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.22.6", features = ["abi3", "abi3-py38", "extension-module"] }
pyo3-file = "0.9.0"
pyo3 = { version = "0.23.3", features = ["abi3", "abi3-py38", "extension-module"] }
pyo3-file = "0.10.0"
serde_json = { workspace = true }

protobuf-json-mapping = { workspace = true }
yara-x = { workspace = true, features = ["parallel-compilation"] }

[build-dependencies]
pyo3-build-config = "0.22.6"
pyo3-build-config = "0.23.3"
54 changes: 26 additions & 28 deletions py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ use std::pin::Pin;
use std::time::Duration;

use protobuf_json_mapping::print_to_string as proto_to_json;
use pyo3::create_exception;
use pyo3::exceptions::{PyException, PyIOError, PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{
PyBool, PyBytes, PyDict, PyFloat, PyInt, PyString, PyTuple,
};
use pyo3::{create_exception, IntoPyObjectExt};
use pyo3_file::PyFileLikeObject;

use ::yara_x as yrx;
Expand Down Expand Up @@ -209,7 +209,7 @@ impl Compiler {
/// This method returns every error encountered during the compilation,
/// across all invocations of [`Compiler::add_source`].
fn errors<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let json = PyModule::import_bound(py, "json")?;
let json = PyModule::import(py, "json")?;
let json_loads = json.getattr("loads")?;
let errors_json = serde_json::to_string_pretty(&self.inner.errors());
let errors_json = errors_json
Expand All @@ -225,7 +225,7 @@ impl Compiler {
&'py self,
py: Python<'py>,
) -> PyResult<Bound<'py, PyAny>> {
let json = PyModule::import_bound(py, "json")?;
let json = PyModule::import(py, "json")?;
let json_loads = json.getattr("loads")?;
let warnings_json =
serde_json::to_string_pretty(&self.inner.warnings());
Expand Down Expand Up @@ -557,13 +557,13 @@ fn scan_results_to_py(
.map(|rule| rule_to_py(py, rule))
.collect::<PyResult<Vec<_>>>()?;

let module_outputs = PyDict::new_bound(py);
let module_outputs = PyDict::new(py);
let outputs = scan_results.module_outputs();

// For better performance we only load the "json" module if there's
// some module output.
if outputs.len() > 0 {
let json = PyModule::import_bound(py, "json")?;
let json = PyModule::import(py, "json")?;
let json_loads = json.getattr("loads")?;
for (module, output) in outputs {
let module_output_json = proto_to_json(output).unwrap();
Expand All @@ -576,7 +576,7 @@ fn scan_results_to_py(
Py::new(
py,
ScanResults {
matching_rules: PyTuple::new_bound(py, matching_rules).unbind(),
matching_rules: PyTuple::new(py, matching_rules)?.unbind(),
module_outputs: module_outputs.into(),
},
)
Expand All @@ -588,23 +588,20 @@ fn rule_to_py(py: Python, rule: yrx::Rule) -> PyResult<Py<Rule>> {
Rule {
identifier: rule.identifier().to_string(),
namespace: rule.namespace().to_string(),
tags: PyTuple::new_bound(
py,
rule.tags().map(|tag| tag.identifier()),
)
.unbind(),
metadata: PyTuple::new_bound(
tags: PyTuple::new(py, rule.tags().map(|tag| tag.identifier()))?
.unbind(),
metadata: PyTuple::new(
py,
rule.metadata()
.map(|(ident, value)| metadata_to_py(py, ident, value)),
)
)?
.unbind(),
patterns: PyTuple::new_bound(
patterns: PyTuple::new(
py,
rule.patterns()
.map(|pattern| pattern_to_py(py, pattern))
.collect::<Result<Vec<_>, _>>()?,
)
)?
.unbind(),
},
)
Expand All @@ -616,28 +613,29 @@ fn metadata_to_py(
metadata: yrx::MetaValue,
) -> Py<PyTuple> {
let value = match metadata {
yrx::MetaValue::Integer(v) => v.to_object(py),
yrx::MetaValue::Float(v) => v.to_object(py),
yrx::MetaValue::Bool(v) => v.to_object(py),
yrx::MetaValue::String(v) => v.to_object(py),
yrx::MetaValue::Bytes(v) => v.to_object(py),
};

PyTuple::new_bound(py, [ident.to_object(py), value]).unbind()
yrx::MetaValue::Integer(v) => v.into_py_any(py),
yrx::MetaValue::Float(v) => v.into_py_any(py),
yrx::MetaValue::Bool(v) => v.into_py_any(py),
yrx::MetaValue::String(v) => v.into_py_any(py),
yrx::MetaValue::Bytes(v) => v.into_py_any(py),
}
.unwrap();

PyTuple::new(py, [ident.into_py_any(py).unwrap(), value]).unwrap().unbind()
}

fn pattern_to_py(py: Python, pattern: yrx::Pattern) -> PyResult<Py<Pattern>> {
Py::new(
py,
Pattern {
identifier: pattern.identifier().to_string(),
matches: PyTuple::new_bound(
matches: PyTuple::new(
py,
pattern
.matches()
.map(|match_| match_to_py(py, match_))
.collect::<Result<Vec<_>, _>>()?,
)
)?
.unbind(),
},
)
Expand Down Expand Up @@ -692,9 +690,9 @@ fn map_scan_err(err: yrx::errors::ScanError) -> PyErr {
/// ```
#[pymodule]
fn yara_x(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("CompileError", m.py().get_type_bound::<CompileError>())?;
m.add("TimeoutError", m.py().get_type_bound::<TimeoutError>())?;
m.add("ScanError", m.py().get_type_bound::<ScanError>())?;
m.add("CompileError", m.py().get_type::<CompileError>())?;
m.add("TimeoutError", m.py().get_type::<TimeoutError>())?;
m.add("ScanError", m.py().get_type::<ScanError>())?;
m.add_function(wrap_pyfunction!(compile, m)?)?;
m.add_class::<Rules>()?;
m.add_class::<Scanner>()?;
Expand Down

0 comments on commit c146b6f

Please sign in to comment.