Skip to content

Commit

Permalink
Upgrade to pyo3 0.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Nov 8, 2024
1 parent f21c44e commit 6d13d47
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 42 deletions.
88 changes: 56 additions & 32 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 @@ -22,7 +22,7 @@ name = "starlark"
starlark = "^0.10.0"
starlark_derive = "^0.10.0"
anyhow = "^1.0.65"
pyo3 = { version = "0.17.1", features = ["extension-module"] }
pyo3 = { version = "0.21.2", features = ["extension-module"] }

# needed to resolve contradictory constraints in dependencies
syn = "^1.0.96"
Expand Down
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ fn serde_to_starlark<'v>(x: serde_json::Value, heap: &'v Heap) -> anyhow::Result
fn value_to_pyobject(value: Value) -> PyResult<PyObject> {
let json_val = convert_err(value.to_json())?;
Python::with_gil(|py| {
let json = py.import("json")?;
let json = py.import_bound("json")?;
json.getattr("loads")?.call1((json_val,))?.extract()
})
}

fn pyobject_to_value<'v>(obj: PyObject, heap: &'v Heap) -> PyResult<Value<'v>> {
Python::with_gil(|py| -> PyResult<Value<'v>> {
let json = py.import("json")?;
let json = py.import_bound("json")?;
let json_str: String = json.getattr("dumps")?.call1((obj,))?.extract()?;
convert_err(serde_to_starlark(
convert_serde_err(serde_json::from_str(&json_str))?,
Expand Down Expand Up @@ -633,7 +633,7 @@ impl<'v> StarlarkValue<'v> for PythonCallableValue {
.map(|v| -> PyResult<PyObject> { value_to_pyobject(v) }))
.collect::<PyResult<Vec<PyObject>>>()?;
convert_to_anyhow(pyobject_to_value(
self.callable.call1(py, PyTuple::new(py, py_args))?,
self.callable.call1(py, PyTuple::new_bound(py, py_args))?,
eval.heap(),
))
})
Expand All @@ -649,12 +649,12 @@ impl<'v> StarlarkValue<'v> for PythonCallableValue {
/// .. automethod:: add_callable
/// .. automethod:: freeze
#[pyclass]
#[pyo3(text_signature = "() -> None")]
struct Module(starlark::environment::Module);

#[pymethods]
impl Module {
#[new]
#[pyo3(text_signature = "() -> None")]
fn py_new() -> PyResult<Module> {
Ok(Module(starlark::environment::Module::new()))
}
Expand All @@ -680,7 +680,6 @@ impl Module {
self.0.set(name, b);
}

#[pyo3(text_signature = "() -> FrozenModule")]
fn freeze(mod_cell: &PyCell<Module>) -> PyResult<FrozenModule> {
let module = mod_cell
.replace(Module(starlark::environment::Module::new()))
Expand All @@ -701,14 +700,14 @@ struct FrozenModule(starlark::environment::FrozenModule);
// {{{ FileLoader

#[pyclass]
#[pyo3(text_signature = "(load_func: Callable[[str], FrozenModule]) -> None")]
struct FileLoader {
callable: PyObject,
}

#[pymethods]
impl FileLoader {
#[new]
#[pyo3(text_signature = "(load_func: Callable[[str], FrozenModule]) -> None")]
fn py_new(callable: PyObject) -> FileLoader {
FileLoader { callable: callable }
}
Expand Down Expand Up @@ -755,7 +754,7 @@ fn eval(
module: &mut Module,
ast: &PyCell<AstModule>,
globals: &Globals,
file_loader: Option<&PyCell<FileLoader>>,
file_loader: Option<&Bound<FileLoader>>,
) -> PyResult<PyObject> {
let tail = |evaluator: &mut starlark::eval::Evaluator| {
// Stupid: eval_module consumes the AST.
Expand Down Expand Up @@ -783,7 +782,7 @@ fn eval(

#[pymodule]
#[pyo3(name = "starlark")]
fn starlark_py(_py: Python, m: &PyModule) -> PyResult<()> {
fn starlark_py(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<ResolvedPos>()?;
m.add_class::<ResolvedSpan>()?;
m.add_class::<ResolvedFileSpan>()?;
Expand All @@ -799,7 +798,7 @@ fn starlark_py(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<FileLoader>()?;
m.add_wrapped(wrap_pyfunction!(parse))?;
m.add_wrapped(wrap_pyfunction!(eval))?;
m.add("StarlarkError", _py.get_type::<StarlarkError>())?;
m.add("StarlarkError", m.py().get_type_bound::<StarlarkError>())?;

Ok(())
}
Expand Down

0 comments on commit 6d13d47

Please sign in to comment.