Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade pyo3 and maturin #67

Merged
merged 2 commits into from
Mar 5, 2024
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
118 changes: 55 additions & 63 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 @@ -40,7 +40,7 @@ serde = { version = "1.0", features = ["derive", "rc"] }
regex = "1.8.4"

[dependencies.pyo3]
version = "0.14.2"
version = "0.20.2"
features = ["abi3-py38", "extension-module"]
optional = true

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["maturin>=1.1.0,<1.3.0"]
requires = ["maturin>=1.3.2,<1.4.0"]
build-backend = "maturin"

[tool.maturin]
Expand Down
37 changes: 22 additions & 15 deletions src/py/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ fn run_clvm_compilation(
}
}

#[pyfunction(arg3 = "[]", arg4 = "None")]
#[pyfunction]
#[pyo3(signature = (input_path, output_path, search_paths = Vec::new(), export_symbols = None))]
fn compile_clvm(
input_path: &PyAny,
output_path: String,
Expand All @@ -181,7 +182,8 @@ fn compile_clvm(
)
}

#[pyfunction(arg2 = "[]", arg3 = "None")]
#[pyfunction]
#[pyo3(signature = (source, search_paths = Vec::new(), export_symbols = None))]
fn compile(
source: String,
search_paths: Vec<String>,
Expand All @@ -195,7 +197,8 @@ fn compile(
)
}

#[pyfunction(arg2 = "[]")]
#[pyfunction]
#[pyo3(signature = (input_path, search_paths=Vec::new()))]
fn check_dependencies(input_path: &PyAny, search_paths: Vec<String>) -> PyResult<PyObject> {
run_clvm_compilation(
CompileClvmSource::SourcePath(input_path),
Expand Down Expand Up @@ -282,12 +285,14 @@ impl CldbSingleBespokeOverride for CldbSinglePythonOverride {
.pycode
.call1(py, PyTuple::new(py, &vec![arg_value]))
.map_err(|e| RunFailure::RunErr(env.loc(), format!("{}", e)))?;
python_value_to_clvm(py, res)
let res_ref: &PyAny = res.as_ref(py);
python_value_to_clvm(res_ref)
})
}
}

#[pyfunction(arg4 = "None", arg5 = "None")]
#[pyfunction]
#[pyo3(signature = (hex_prog, hex_args, symbol_table, overrides=None, run_options=None))]
fn start_clvm_program(
hex_prog: String,
hex_args: String,
Expand All @@ -298,17 +303,18 @@ fn start_clvm_program(
let (command_tx, command_rx) = mpsc::channel();
let (result_tx, result_rx) = mpsc::channel();

let gil = Python::acquire_gil();
let py = gil.python();
let print_only_value = Python::with_gil(|py| {
let print_only_option = run_options
.and_then(|h| h.get("print").map(|p| p.clone()))
.unwrap_or_else(|| {
let any: Py<PyAny> = PyBool::new(py, false).into();
any
});

let print_only_option = run_options
.and_then(|h| h.get("print").map(|p| p.clone()))
.unwrap_or_else(|| {
let any: Py<PyAny> = PyBool::new(py, false).into();
any
});
PyBool::new(py, true).compare(print_only_option)
})?;

let print_only = PyBool::new(py, true).compare(print_only_option)? == Ordering::Equal;
let print_only = print_only_value == Ordering::Equal;

thread::spawn(move || {
let mut allocator = Allocator::new();
Expand Down Expand Up @@ -386,7 +392,8 @@ fn start_clvm_program(
})
}

#[pyfunction(arg3 = 2)]
#[pyfunction]
#[pyo3(signature = (tool_name, args, default_stage=2))]
fn launch_tool(tool_name: String, args: Vec<String>, default_stage: u32) -> Vec<u8> {
let mut stdout = Stream::new(None);
cmds::launch_tool(&mut stdout, &args, &tool_name, default_stage);
Expand Down
26 changes: 11 additions & 15 deletions src/py/pyval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ use crate::compiler::runtypes::RunFailure;
use crate::compiler::sexp::SExp;
use crate::compiler::srcloc::Srcloc;

pub fn map_err_to_pyerr(srcloc: Srcloc, r: PyResult<Py<PyAny>>) -> Result<Py<PyAny>, RunFailure> {
pub fn map_err_to_pyerr<PA>(srcloc: Srcloc, r: PyResult<PA>) -> Result<PA, RunFailure> {
r.map_err(|e| RunFailure::RunErr(srcloc, format!("{e}")))
}

pub fn python_value_to_clvm(py: Python, val: Py<PyAny>) -> Result<Rc<SExp>, RunFailure> {
pub fn python_value_to_clvm(val: &PyAny) -> Result<Rc<SExp>, RunFailure> {
let srcloc = Srcloc::start("*python*");
val.as_ref(py)
.downcast::<PyList>()
val.downcast::<PyList>()
.ok()
.map(|l| {
if l.is_empty() {
Expand All @@ -27,11 +26,10 @@ pub fn python_value_to_clvm(py: Python, val: Py<PyAny>) -> Result<Rc<SExp>, RunF
let mut result = SExp::Nil(srcloc.clone());
for i_rev in 0..l.len() {
let i = l.len() - i_rev - 1;
let item = l.get_item(i as isize).extract();
let any_of_elt = map_err_to_pyerr(srcloc.clone(), item)?;
let any_of_elt = map_err_to_pyerr(srcloc.clone(), l.get_item(i))?;
result = SExp::Cons(
srcloc.clone(),
python_value_to_clvm(py, any_of_elt)?,
python_value_to_clvm(any_of_elt)?,
Rc::new(result),
);
}
Expand All @@ -40,30 +38,28 @@ pub fn python_value_to_clvm(py: Python, val: Py<PyAny>) -> Result<Rc<SExp>, RunF
})
.map(Some)
.unwrap_or_else(|| {
val.as_ref(py)
.downcast::<PyTuple>()
val.downcast::<PyTuple>()
.map(|t| {
if t.len() != 2 {
Err(RunFailure::RunErr(
srcloc.clone(),
"tuple must have len 2".to_string(),
))
} else {
let any_of_e0 = map_err_to_pyerr(srcloc.clone(), t.get_item(0).extract())?;
let any_of_e1 = map_err_to_pyerr(srcloc.clone(), t.get_item(1).extract())?;
let any_of_e0 = map_err_to_pyerr(srcloc.clone(), t.get_item(0))?;
let any_of_e1 = map_err_to_pyerr(srcloc.clone(), t.get_item(1))?;
Ok(Rc::new(SExp::Cons(
srcloc.clone(),
python_value_to_clvm(py, any_of_e0)?,
python_value_to_clvm(py, any_of_e1)?,
python_value_to_clvm(any_of_e0)?,
python_value_to_clvm(any_of_e1)?,
)))
}
})
.ok()
})
.map(Some)
.unwrap_or_else(|| {
val.as_ref(py)
.downcast::<PyBytes>()
val.downcast::<PyBytes>()
.map(|b| Ok(Rc::new(SExp::Atom(srcloc.clone(), b.as_bytes().to_vec()))))
.ok()
})
Expand Down
Loading
Loading