Skip to content
Open
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
25 changes: 12 additions & 13 deletions native/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 native/libcst/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trace = ["peg/trace"]

[dependencies]
paste = "1.0.15"
pyo3 = { version = "0.25.1", optional = true }
pyo3 = { version = "0.26", optional = true }
thiserror = "2.0.12"
peg = "0.8.5"
annotate-snippets = "0.11.5"
Expand Down
8 changes: 4 additions & 4 deletions native/libcst/src/nodes/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2670,8 +2670,8 @@ mod py {
use crate::nodes::traits::py::TryIntoPy;

// TODO: this could be a derive helper attribute to override the python class name
impl<'a> TryIntoPy<pyo3::PyObject> for Element<'a> {
fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult<pyo3::PyObject> {
impl<'a> TryIntoPy<pyo3::Py<pyo3::PyAny>> for Element<'a> {
fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<pyo3::PyAny>> {
match self {
Self::Starred(s) => s.try_into_py(py),
Self::Simple { value, comma } => {
Expand Down Expand Up @@ -2699,8 +2699,8 @@ mod py {
}

// TODO: this could be a derive helper attribute to override the python class name
impl<'a> TryIntoPy<pyo3::PyObject> for DictElement<'a> {
fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult<pyo3::PyObject> {
impl<'a> TryIntoPy<pyo3::Py<pyo3::PyAny>> for DictElement<'a> {
fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<pyo3::PyAny>> {
match self {
Self::Starred(s) => s.try_into_py(py),
Self::Simple {
Expand Down
4 changes: 2 additions & 2 deletions native/libcst/src/nodes/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ macro_rules! py_import {
( $module_name:expr, $member_name:expr, $getter_fn:ident ) => {
paste::paste! {
static [<IMPORT_CELL_ $getter_fn:snake:upper>]
: pyo3::once_cell::GILOnceCell<pyo3::PyResult<pyo3::PyObject>>
= pyo3::once_cell::GILOnceCell::new();
: pyo3::once_cell::PyOnceLock<pyo3::PyResult<pyo3::Py<pyo3::PyAny>>>
= pyo3::once_cell::PyOnceLock::new();

fn $getter_fn<'py>(py: pyo3::Python<'py>) -> pyo3::PyResult<&'py pyo3::PyAny> {
Ok([<IMPORT_CELL_ $getter_fn:snake:upper>].get_or_init(py, || {
Expand Down
26 changes: 13 additions & 13 deletions native/libcst/src/nodes/parser_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ impl BaseWhitespaceParserConfig {
}

#[getter]
fn get_lines(&self, py: Python) -> PyObject {
fn get_lines(&self, py: Python) -> Py<PyAny> {
self.lines.to_object(py)
}

#[getter]
fn get_default_newline(&self, py: Python) -> PyObject {
fn get_default_newline(&self, py: Python) -> Py<PyAny> {
self.default_newline.to_object(py)
}
}
Expand Down Expand Up @@ -62,36 +62,36 @@ impl BaseWhitespaceParserConfig {
}
}

// These fields are private and PyObject, since we don't currently care about using them from
// These fields are private and Py<PyAny>, since we don't currently care about using them from
// within rust.
#[pyclass(extends=BaseWhitespaceParserConfig, module="libcst_native.parser_config")]
#[text_signature = "(*, lines, encoding, default_indent, default_newline, has_trailing_newline, version, future_imports)"]
pub struct ParserConfig {
// lines is inherited
#[pyo3(get)]
encoding: PyObject,
encoding: Py<PyAny>,
#[pyo3(get)]
default_indent: PyObject,
default_indent: Py<PyAny>,
// default_newline is inherited
#[pyo3(get)]
has_trailing_newline: PyObject,
has_trailing_newline: Py<PyAny>,
#[pyo3(get)]
version: PyObject,
version: Py<PyAny>,
#[pyo3(get)]
future_imports: PyObject,
future_imports: Py<PyAny>,
}

#[pymethods]
impl ParserConfig {
#[new]
fn new(
lines: &PySequence,
encoding: PyObject,
default_indent: PyObject,
encoding: Py<PyAny>,
default_indent: Py<PyAny>,
default_newline: &PyString,
has_trailing_newline: PyObject,
version: PyObject,
future_imports: PyObject,
has_trailing_newline: Py<PyAny>,
version: Py<PyAny>,
future_imports: Py<PyAny>,
) -> PyResult<(Self, BaseWhitespaceParserConfig)> {
Ok((
Self {
Expand Down
16 changes: 8 additions & 8 deletions native/libcst/src/nodes/py_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use pyo3::prelude::*;
use std::convert::AsRef;
use std::ops::Deref;

/// An immutable wrapper around a rust type T and it's PyObject equivalent. Caches the conversion
/// to and from the PyObject.
/// An immutable wrapper around a rust type T and its Py<PyAny> equivalent. Caches the conversion
/// to and from the Py<PyAny>.
pub struct PyCached<T> {
native: T,
py_object: PyObject,
py_object: Py<PyAny>,
}

impl<T> PyCached<T>
Expand All @@ -31,7 +31,7 @@ where
T: FromPyObject<'source>,
{
fn extract(ob: &'source PyAny) -> PyResult<Self> {
Python::with_gil(|py| {
Python::attach(|py| {
Ok(PyCached {
native: ob.extract()?,
py_object: ob.to_object(py),
Expand All @@ -40,14 +40,14 @@ where
}
}

impl<T> IntoPy<PyObject> for PyCached<T> {
fn into_py(self, _py: Python) -> PyObject {
impl<T> IntoPy<Py<PyAny>> for PyCached<T> {
fn into_py(self, _py: Python) -> Py<PyAny> {
self.py_object
}
}

impl<T> ToPyObject for PyCached<T> {
fn to_object(&self, py: Python) -> PyObject {
fn to_object(&self, py: Python) -> Py<PyAny> {
self.py_object.clone_ref(py)
}
}
Expand All @@ -71,6 +71,6 @@ where
T: ToPyObject,
{
fn from(val: T) -> Self {
Python::with_gil(|py| Self::new(py, val))
Python::attach(|py| Self::new(py, val))
}
}
28 changes: 14 additions & 14 deletions native/libcst/src/nodes/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a, T: Inflate<'a>> Inflate<'a> for Vec<T> {
}
#[cfg(feature = "py")]
pub mod py {
use pyo3::{types::PyTuple, IntoPyObjectExt, PyObject, PyResult, Python};
use pyo3::{types::PyTuple, IntoPyObjectExt, Py, PyAny, PyResult, Python};

// TODO: replace with upstream implementation once
// https://github.com/PyO3/pyo3/issues/1813 is resolved
Expand All @@ -133,38 +133,38 @@ pub mod py {
// }
// }

impl TryIntoPy<PyObject> for bool {
fn try_into_py(self, py: Python) -> PyResult<PyObject> {
impl TryIntoPy<Py<PyAny>> for bool {
fn try_into_py(self, py: Python) -> PyResult<Py<PyAny>> {
self.into_py_any(py)
}
}

impl<T: TryIntoPy<PyObject>> TryIntoPy<PyObject> for Box<T>
impl<T: TryIntoPy<Py<PyAny>>> TryIntoPy<Py<PyAny>> for Box<T>
where
T: TryIntoPy<PyObject>,
T: TryIntoPy<Py<PyAny>>,
{
fn try_into_py(self, py: Python) -> PyResult<PyObject> {
fn try_into_py(self, py: Python) -> PyResult<Py<PyAny>> {
(*self).try_into_py(py)
}
}

impl<T> TryIntoPy<PyObject> for Option<T>
impl<T> TryIntoPy<Py<PyAny>> for Option<T>
where
T: TryIntoPy<PyObject>,
T: TryIntoPy<Py<PyAny>>,
{
fn try_into_py(self, py: Python) -> PyResult<PyObject> {
fn try_into_py(self, py: Python) -> PyResult<Py<PyAny>> {
Ok(match self {
None => py.None(),
Some(x) => x.try_into_py(py)?,
})
}
}

impl<T> TryIntoPy<PyObject> for Vec<T>
impl<T> TryIntoPy<Py<PyAny>> for Vec<T>
where
T: TryIntoPy<PyObject>,
T: TryIntoPy<Py<PyAny>>,
{
fn try_into_py(self, py: Python) -> PyResult<PyObject> {
fn try_into_py(self, py: Python) -> PyResult<Py<PyAny>> {
let converted = self
.into_iter()
.map(|x| x.try_into_py(py))
Expand All @@ -174,8 +174,8 @@ pub mod py {
}
}

impl<'a> TryIntoPy<PyObject> for &'a str {
fn try_into_py(self, py: Python) -> PyResult<PyObject> {
impl<'a> TryIntoPy<Py<PyAny>> for &'a str {
fn try_into_py(self, py: Python) -> PyResult<Py<PyAny>> {
self.into_py_any(py)
}
}
Expand Down
2 changes: 1 addition & 1 deletion native/libcst/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod py_error {

impl<'a> From<ParserError<'a>> for PyErr {
fn from(e: ParserError) -> Self {
Python::with_gil(|py| {
Python::attach(|py| {
let lines = match &e {
ParserError::TokenizerError(_, text) | ParserError::ParserError(_, text) => {
text.lines().collect::<Vec<_>>()
Expand Down
12 changes: 6 additions & 6 deletions native/libcst/src/py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ use pyo3::prelude::*;
pub fn libcst_native(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
#[pyfn(m)]
#[pyo3(signature = (source, encoding=None))]
fn parse_module(source: String, encoding: Option<&str>) -> PyResult<PyObject> {
fn parse_module(source: String, encoding: Option<&str>) -> PyResult<Py<PyAny>> {
let m = crate::parse_module(source.as_str(), encoding)?;
Python::with_gil(|py| m.try_into_py(py))
Python::attach(|py| m.try_into_py(py))
}

#[pyfn(m)]
fn parse_expression(source: String) -> PyResult<PyObject> {
fn parse_expression(source: String) -> PyResult<Py<PyAny>> {
let expr = crate::parse_expression(source.as_str())?;
Python::with_gil(|py| expr.try_into_py(py))
Python::attach(|py| expr.try_into_py(py))
}

#[pyfn(m)]
fn parse_statement(source: String) -> PyResult<PyObject> {
fn parse_statement(source: String) -> PyResult<Py<PyAny>> {
let stm = crate::parse_statement(source.as_str())?;
Python::with_gil(|py| stm.try_into_py(py))
Python::attach(|py| stm.try_into_py(py))
}

Ok(())
Expand Down
Loading