Skip to content

Commit

Permalink
Upgrade pyo3 to 0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Nov 6, 2024
1 parent 816f4c4 commit b08f4c0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 30 deletions.
26 changes: 13 additions & 13 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 minijinja-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ crate-type = ["cdylib"]
[dependencies]
minijinja = { version = "2.4.0", path = "../minijinja", features = ["loader", "json", "urlencode", "fuel", "preserve_order", "speedups", "custom_syntax"] }
once_cell = "1.17.0"
pyo3 = { version = "0.21.1", features = ["extension-module", "serde", "abi3-py38"] }
pyo3 = { version = "0.22.6", features = ["extension-module", "serde", "abi3-py38"] }
59 changes: 44 additions & 15 deletions minijinja-py/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl Environment {
}
let callback: Py<PyAny> = callback.clone().unbind();
let mut inner = self.inner.lock().unwrap();
inner.auto_escape_callback = Some(callback.clone());
inner.auto_escape_callback = Python::with_gil(|py| Some(callback.clone_ref(py)));
inner
.env
.set_auto_escape_callback(move |name: &str| -> AutoEscape {
Expand Down Expand Up @@ -341,8 +341,14 @@ impl Environment {
}

#[getter]
pub fn get_auto_escape_callback(&self) -> PyResult<Option<Py<PyAny>>> {
Ok(self.inner.lock().unwrap().auto_escape_callback.clone())
pub fn get_auto_escape_callback(&self, py: Python<'_>) -> PyResult<Option<Py<PyAny>>> {
Ok(self
.inner
.lock()
.unwrap()
.auto_escape_callback
.as_ref()
.map(|x| x.clone_ref(py)))
}

/// Sets a finalizer.
Expand All @@ -355,7 +361,9 @@ impl Environment {
}
let callback: Py<PyAny> = callback.clone().unbind();
let mut inner = self.inner.lock().unwrap();
inner.finalizer_callback = Some(callback.clone());
Python::with_gil(|py| {
inner.finalizer_callback = Some(callback.clone_ref(py));
});
inner.env.set_formatter(move |output, state, value| {
Python::with_gil(|py| -> Result<(), Error> {
let maybe_new_value = bind_state(state, || -> Result<_, Error> {
Expand All @@ -382,8 +390,14 @@ impl Environment {
}

#[getter]
pub fn get_finalizer(&self) -> PyResult<Option<Py<PyAny>>> {
Ok(self.inner.lock().unwrap().finalizer_callback.clone())
pub fn get_finalizer(&self, py: Python<'_>) -> PyResult<Option<Py<PyAny>>> {
Ok(self
.inner
.lock()
.unwrap()
.finalizer_callback
.as_ref()
.map(|x| x.clone_ref(py)))
}

/// Sets a loader function for the environment.
Expand All @@ -403,7 +417,9 @@ impl Environment {
}
};
let mut inner = self.inner.lock().unwrap();
inner.loader.clone_from(&callback);
Python::with_gil(|py| {
inner.loader = callback.as_ref().map(|x| x.clone_ref(py));
});

if let Some(callback) = callback {
inner.env.set_loader(move |name| {
Expand All @@ -426,8 +442,13 @@ impl Environment {

/// Returns the current loader.
#[getter]
pub fn get_loader(&self) -> Option<Py<PyAny>> {
self.inner.lock().unwrap().loader.clone()
pub fn get_loader(&self, py: Python<'_>) -> Option<Py<PyAny>> {
self.inner
.lock()
.unwrap()
.loader
.as_ref()
.map(|x| x.clone_ref(py))
}

/// Sets a new path join callback.
Expand All @@ -438,7 +459,9 @@ impl Environment {
}
let callback: Py<PyAny> = callback.clone().unbind();
let mut inner = self.inner.lock().unwrap();
inner.path_join_callback = Some(callback.clone());
Python::with_gil(|py| {
inner.path_join_callback = Some(callback.clone_ref(py));
});
inner.env.set_path_join_callback(move |name, parent| {
Python::with_gil(|py| {
let callback = callback.bind(py);
Expand All @@ -456,14 +479,19 @@ impl Environment {

/// Returns the current path join callback.
#[getter]
pub fn get_path_join_callback(&self) -> Option<Py<PyAny>> {
self.inner.lock().unwrap().path_join_callback.clone()
pub fn get_path_join_callback(&self, py: Python<'_>) -> Option<Py<PyAny>> {
self.inner
.lock()
.unwrap()
.path_join_callback
.as_ref()
.map(|x| x.clone_ref(py))
}

/// Triggers a reload of the templates.
pub fn reload(&self) -> PyResult<()> {
pub fn reload(&self, py: Python<'_>) -> PyResult<()> {
let mut inner = self.inner.lock().unwrap();
let loader = inner.loader.as_ref().cloned();
let loader = inner.loader.as_ref().map(|x| x.clone_ref(py));
if loader.is_some() {
inner.env.clear_templates();
}
Expand Down Expand Up @@ -633,11 +661,12 @@ impl Environment {
#[pyo3(signature = (template_name, /, **ctx))]
pub fn render_template(
slf: PyRef<'_, Self>,
py: Python<'_>,
template_name: &str,
ctx: Option<&Bound<'_, PyDict>>,
) -> PyResult<String> {
if slf.reload_before_render.load(Ordering::Relaxed) {
slf.reload()?;
slf.reload(py)?;
}
bind_environment(slf.as_ptr(), || {
let inner = slf.inner.lock().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion minijinja-py/src/typeconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn to_python_value_impl(py: Python<'_>, value: Value) -> PyResult<Py<PyAny>> {
// conversion. That means that when passing the object back to Python we
// extract the retained raw Python reference.
if let Some(pyobj) = value.downcast_object_ref::<DynamicObject>() {
return Ok(pyobj.inner.clone());
return Ok(pyobj.inner.clone_ref(py));
}

if let Some(obj) = value.as_object() {
Expand Down

0 comments on commit b08f4c0

Please sign in to comment.