diff --git a/native/libcst/src/py.rs b/native/libcst/src/py.rs index 57da11e78..5b3eaa03f 100644 --- a/native/libcst/src/py.rs +++ b/native/libcst/src/py.rs @@ -8,24 +8,30 @@ use pyo3::prelude::*; #[pymodule(gil_used = false)] #[pyo3(name = "native")] -pub fn libcst_native(_py: Python, m: &Bound) -> PyResult<()> { +pub fn libcst_native(m: &Bound) -> PyResult<()> { #[pyfn(m)] #[pyo3(signature = (source, encoding=None))] fn parse_module(source: String, encoding: Option<&str>) -> PyResult { - let m = crate::parse_module(source.as_str(), encoding)?; - Python::with_gil(|py| m.try_into_py(py)) + Python::with_gil(|py| { + let m = py.allow_threads(|| crate::parse_module(source.as_str(), encoding))?; + m.try_into_py(py) + }) } #[pyfn(m)] fn parse_expression(source: String) -> PyResult { - let expr = crate::parse_expression(source.as_str())?; - Python::with_gil(|py| expr.try_into_py(py)) + Python::with_gil(|py| { + let expr = py.allow_threads(|| crate::parse_expression(source.as_str()))?; + expr.try_into_py(py) + }) } #[pyfn(m)] fn parse_statement(source: String) -> PyResult { - let stm = crate::parse_statement(source.as_str())?; - Python::with_gil(|py| stm.try_into_py(py)) + Python::with_gil(|py| { + let stm = py.allow_threads(|| crate::parse_statement(source.as_str()))?; + stm.try_into_py(py) + }) } Ok(())