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
58 changes: 19 additions & 39 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chialisp"
version = "0.4.5"
version = "0.4.6"
edition = "2021"
authors = ["Art Yerkes <art.yerkes@gmail.com>"]
description = "tools for working with chialisp language; compiler, repl, python and wasm bindings"
Expand Down Expand Up @@ -28,7 +28,7 @@ lfsr = { version = "0.3.0", optional = true }
num = "0.4.3"
num-bigint = { version = "0.4.4", features = ["serde"] }
num-traits = "0.2.15"
rand = { version = "0.9.4", optional = true }
rand = { version = "0.9.4", optional = true, features = ["std", "std_rng", "os_rng"] }
rand_chacha = { version = "0.9.0", optional = true }
regex = "1.12.3"
serde = { version = "1.0", features = ["derive", "rc"] }
Expand All @@ -41,7 +41,7 @@ yaml-rust2 = "0.11.0"
subprocess = { version = "1.1.0", optional = true }

[dependencies.pyo3]
version = "0.25.1"
version = "0.29.0"
features = ["abi3-py310", "extension-module"]
optional = true

Expand Down
29 changes: 15 additions & 14 deletions src/py/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// re: https://github.com/rust-lang/rust-clippy/issues/8971
use pyo3::exceptions::PyException;
use pyo3::types::{PyBool, PyDict, PyString, PyTuple};
use pyo3::CastError;
use pyo3::{create_exception, prelude::*, IntoPyObjectExt};

use std::cmp::Ordering;
Expand Down Expand Up @@ -79,7 +80,7 @@ fn get_source_from_input(input_code: CompileClvmSource) -> PyResult<(String, Str
.and_then(|x| x.get_item(0))
.and_then(|x| x.str())
} else {
input_path.extract()
input_path.extract().map_err(|e: CastError| e.into())
}?;

let mut path_string = real_input_path.to_string();
Expand All @@ -101,7 +102,7 @@ fn run_clvm_compilation(
action: CompileClvmAction,
search_paths: Vec<String>,
export_symbols: Option<bool>,
) -> PyResult<PyObject> {
) -> PyResult<Py<PyAny>> {
// Resolve the input, get the indicated path and content.
let (path_string, file_content) = get_source_from_input(input_code)?;

Expand Down Expand Up @@ -148,7 +149,7 @@ fn run_clvm_compilation(

// Produce compiled output according to whether output with symbols
// or just the standard result is required.
Python::with_gil(|py| {
Python::attach(|py| {
if export_symbols == Some(true) {
let mut result_dict = HashMap::new();
result_dict.insert("output".to_string(), compiled.into_py_any(py)?);
Expand All @@ -167,7 +168,7 @@ fn run_clvm_compilation(
.map(|rlist| rlist.iter().map(|i| decode_string(&i.name)).collect())?;

// Return all visited files.
Python::with_gil(|py| result_deps.into_py_any(py))
Python::attach(|py| result_deps.into_py_any(py))
}
}
}
Expand All @@ -179,7 +180,7 @@ fn compile_clvm(
output_path: String,
search_paths: Vec<String>,
export_symbols: Option<bool>,
) -> PyResult<PyObject> {
) -> PyResult<Py<PyAny>> {
run_clvm_compilation(
CompileClvmSource::SourcePath(input_path),
CompileClvmAction::CompileCode(Some(output_path)),
Expand All @@ -194,7 +195,7 @@ fn compile(
source: String,
search_paths: Vec<String>,
export_symbols: Option<bool>,
) -> PyResult<PyObject> {
) -> PyResult<Py<PyAny>> {
run_clvm_compilation(
CompileClvmSource::SourceCode("*inline*".to_string(), source),
CompileClvmAction::CompileCode(None),
Expand All @@ -208,7 +209,7 @@ fn compile(
fn check_dependencies(
input_path: Bound<'_, PyAny>,
search_paths: Vec<String>,
) -> PyResult<PyObject> {
) -> PyResult<Py<PyAny>> {
run_clvm_compilation(
CompileClvmSource::SourcePath(input_path),
CompileClvmAction::CheckDependencies,
Expand All @@ -225,7 +226,7 @@ struct PythonRunStep {
rx: Receiver<(bool, Option<BTreeMap<String, String>>)>,
}

fn runstep(myself: &mut PythonRunStep) -> PyResult<Option<PyObject>> {
fn runstep(myself: &mut PythonRunStep) -> PyResult<Option<Py<PyAny>>> {
if myself.ended {
return Ok(None);
}
Expand All @@ -249,7 +250,7 @@ fn runstep(myself: &mut PythonRunStep) -> PyResult<Option<PyObject>> {
let dict_result = res
.1
.map(|m| {
Python::with_gil(|py| {
Python::attach(|py| {
let dict = PyDict::new(py);
for (k, v) in m.iter() {
let _ = dict.set_item(PyString::new(py, k), PyString::new(py, v));
Expand All @@ -272,8 +273,8 @@ impl PythonRunStep {
let _ = self.tx.send(true);
}

fn step(&mut self, py: Python) -> PyResult<Option<PyObject>> {
py.allow_threads(|| runstep(self))
fn step(&mut self, py: Python) -> PyResult<Option<Py<PyAny>>> {
py.detach(|| runstep(self))
}
}

Expand All @@ -289,7 +290,7 @@ impl CldbSinglePythonOverride {

impl CldbSingleBespokeOverride for CldbSinglePythonOverride {
fn get_override(&self, env: Rc<SExp>) -> Result<Rc<SExp>, RunFailure> {
Python::with_gil(|py| {
Python::attach(|py| {
let arg_value = clvm_value_to_python(py, env.clone())
.map_err(|e| RunFailure::RunErr(env.loc(), format!("{}", e)))?;
let res = self
Expand Down Expand Up @@ -317,7 +318,7 @@ fn start_clvm_program(
let (command_tx, command_rx) = mpsc::channel();
let (result_tx, result_rx) = mpsc::channel();

let print_only_value = Python::with_gil(|py| {
let print_only_value = Python::attach(|py| {
let print_only_option = run_options
.and_then(|h| h.get("print").map(|p| Ok(p.clone_ref(py))))
.or_else(|| Some(PyBool::new(py, false).into_py_any(py)))
Expand Down Expand Up @@ -361,7 +362,7 @@ fn start_clvm_program(
HashMap::new();
if let Some(t) = overrides {
for (k, v) in t.iter() {
let v_clone = Python::with_gil(|py| v.clone_ref(py));
let v_clone = Python::attach(|py| v.clone_ref(py));
let override_fun_callable = CldbSinglePythonOverride::new(v_clone);
overrides_table.insert(k.clone(), Box::new(override_fun_callable));
}
Expand Down
16 changes: 7 additions & 9 deletions src/py/binutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ fn convert_to_external<'a>(
cons: Bound<'a, PyAny>,
from_bytes: Bound<'a, PyAny>,
root_node: NodePtr,
) -> PyResult<PyObject> {
) -> PyResult<Py<PyAny>> {
let mut stack: Vec<NodePtr> = vec![root_node];
let mut finished = HashMap::<NodePtr, PyObject>::new();
let mut finished = HashMap::<NodePtr, Py<PyAny>>::new();

while let Some(node) = stack.last() {
let node = *node; // To avoid borrowing issues with the stack
Expand All @@ -31,11 +31,10 @@ fn convert_to_external<'a>(
if left_finished && right_finished {
stack.pop();

let result: PyObject = Python::with_gil(|py| {
let result: Py<PyAny> = Python::attach(|py| {
let a = finished.get(&left).unwrap();
let b = finished.get(&right).unwrap();
let args = PyTuple::new(py, &[a, b])?;
cons.call1(args).and_then(|value| value.into_py_any(py))
cons.call1((a, b)).and_then(|value| value.into_py_any(py))
})?
.into();

Expand All @@ -53,15 +52,14 @@ fn convert_to_external<'a>(
stack.pop();

if !finished.contains_key(&node) {
let converted: PyObject = Python::with_gil(|py| {
let converted: Py<PyAny> = Python::attach(|py| {
let atom = allocator.atom(node);
let bytes = PyBytes::new(py, atom.as_ref());
let args = PyTuple::new(py, &[bytes])?;
from_bytes
.call1(args)
.and_then(|value| value.into_py_any(py))
})?
.into();
})?;
finished.insert(node, converted);
}
}
Expand All @@ -80,7 +78,7 @@ pub fn assemble_generic(
cons: Bound<'_, PyAny>,
from_bytes: Bound<'_, PyAny>,
args: String,
) -> PyResult<PyObject> {
) -> PyResult<Py<PyAny>> {
let mut allocator = Allocator::new();
let assembled =
binutils::assemble(&mut allocator, &args).map_err(|e| ConvError::new_err(e.to_string()))?;
Expand Down
6 changes: 3 additions & 3 deletions src/py/pyval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn map_err_to_pyerr<PA>(srcloc: Srcloc, r: PyResult<PA>) -> Result<PA, RunFa

pub fn python_value_to_clvm(val: Bound<'_, PyAny>) -> Result<Rc<SExp>, RunFailure> {
let srcloc = Srcloc::start("*python*");
val.downcast::<PyList>()
val.cast::<PyList>()
.ok()
.map(|l| {
if l.is_empty() {
Expand All @@ -39,7 +39,7 @@ pub fn python_value_to_clvm(val: Bound<'_, PyAny>) -> Result<Rc<SExp>, RunFailur
})
.map(Some)
.unwrap_or_else(|| {
val.downcast::<PyTuple>()
val.cast::<PyTuple>()
.map(|t| {
if t.len() != 2 {
Err(RunFailure::RunErr(
Expand All @@ -60,7 +60,7 @@ pub fn python_value_to_clvm(val: Bound<'_, PyAny>) -> Result<Rc<SExp>, RunFailur
})
.map(Some)
.unwrap_or_else(|| {
val.downcast::<PyBytes>()
val.cast::<PyBytes>()
.map(|b| Ok(Rc::new(SExp::Atom(srcloc.clone(), b.as_bytes().to_vec()))))
.ok()
})
Expand Down
Loading
Loading