mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-28 22:35:02 +00:00
to_pyo3_ast to return &'py
This commit is contained in:
parent
e1f02fced7
commit
b81273e9bc
3 changed files with 1289 additions and 1288 deletions
File diff suppressed because it is too large
Load diff
129
ast/src/pyo3.rs
129
ast/src/pyo3.rs
|
@ -3,7 +3,8 @@ use num_complex::Complex64;
|
|||
use once_cell::sync::OnceCell;
|
||||
use pyo3::{
|
||||
prelude::*,
|
||||
types::{PyBytes, PyList, PyString, PyTuple},
|
||||
types::{PyBool, PyBytes, PyList, PyString, PyTuple},
|
||||
ToPyObject,
|
||||
};
|
||||
|
||||
pub trait Pyo3Node {
|
||||
|
@ -16,28 +17,28 @@ pub trait Pyo3Node {
|
|||
}
|
||||
|
||||
pub trait ToPyo3Ast {
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>>;
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny>;
|
||||
}
|
||||
|
||||
impl<T: ToPyo3Ast> ToPyo3Ast for Box<T> {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||
(**self).to_pyo3_ast(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToPyo3Ast> ToPyo3Ast for Option<T> {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||
match self {
|
||||
Some(ast) => ast.to_pyo3_ast(py),
|
||||
None => Ok(py.None()),
|
||||
None => Ok(ast_cache().none_ref(py)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToPyo3Ast> ToPyo3Ast for Vec<T> {
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||
let elts = self
|
||||
.iter()
|
||||
.map(|item| item.to_pyo3_ast(py))
|
||||
|
@ -49,60 +50,74 @@ impl<T: ToPyo3Ast> ToPyo3Ast for Vec<T> {
|
|||
|
||||
impl ToPyo3Ast for crate::Identifier {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
Ok(self.as_str().to_object(py))
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||
Ok(PyString::new(py, self.as_str()).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyo3Ast for crate::String {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
Ok(self.as_str().to_object(py))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyo3Ast for crate::Int {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
Ok((self.to_u32()).to_object(py))
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||
Ok(PyString::new(py, self.as_str()).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyo3Ast for bool {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
Ok((*self as u32).to_object(py))
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||
Ok(ast_cache().bool_int(py, *self))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyo3Ast for ConversionFlag {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
Ok((*self as i8).to_object(py))
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||
Ok(ast_cache().conversion_flag(py, *self))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyo3Ast for crate::Constant {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
let value = match self {
|
||||
crate::Constant::None => py.None(),
|
||||
crate::Constant::Bool(bool) => bool.to_object(py),
|
||||
impl ToPyObject for crate::Constant {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
let cache = ast_cache();
|
||||
match self {
|
||||
crate::Constant::None => cache.none.clone_ref(py),
|
||||
crate::Constant::Bool(bool) => cache.bool(py, *bool).into(),
|
||||
crate::Constant::Str(string) => string.to_object(py),
|
||||
crate::Constant::Bytes(bytes) => PyBytes::new(py, bytes).into(),
|
||||
crate::Constant::Int(int) => int.to_object(py),
|
||||
crate::Constant::Tuple(elts) => {
|
||||
let elts: PyResult<Vec<_>> = elts.iter().map(|c| c.to_pyo3_ast(py)).collect();
|
||||
PyTuple::new(py, elts?).into()
|
||||
let elts: Vec<_> = elts.iter().map(|c| c.to_object(py)).collect();
|
||||
PyTuple::new(py, elts).into()
|
||||
}
|
||||
crate::Constant::Float(f64) => f64.to_object(py),
|
||||
crate::Constant::Complex { real, imag } => Complex64::new(*real, *imag).to_object(py),
|
||||
crate::Constant::Ellipsis => py.Ellipsis(),
|
||||
};
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// impl ToPyo3Ast for crate::Constant {
|
||||
// #[inline]
|
||||
// fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||
// let cache = ast_cache();
|
||||
// let value = match self {
|
||||
// crate::Constant::None => cache.none_ref(py),
|
||||
// crate::Constant::Bool(bool) => cache.bool_ref(py),
|
||||
// crate::Constant::Str(string) => string.to_object(py),
|
||||
// crate::Constant::Bytes(bytes) => PyBytes::new(py, bytes).into(),
|
||||
// crate::Constant::Int(int) => int.to_object(py),
|
||||
// crate::Constant::Tuple(elts) => {
|
||||
// let elts: PyResult<Vec<_>> = elts.iter().map(|c| c.to_pyo3_ast(py)).collect();
|
||||
// PyTuple::new(py, elts?).into()
|
||||
// }
|
||||
// crate::Constant::Float(f64) => f64.to_object(py),
|
||||
// crate::Constant::Complex { real, imag } => Complex64::new(*real, *imag).to_object(py),
|
||||
// crate::Constant::Ellipsis => py.Ellipsis(),
|
||||
// };
|
||||
// Ok(value)
|
||||
// }
|
||||
// }
|
||||
|
||||
#[pyclass(module = "rustpython_ast", subclass)]
|
||||
pub struct AST;
|
||||
|
||||
|
@ -125,26 +140,70 @@ fn cache_py_type<N: Pyo3Node + Node>(ast_module: &PyAny) -> PyResult<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
struct AstKeyCache {
|
||||
// TODO: This cache must be bound to 'py
|
||||
struct AstCache {
|
||||
lineno: Py<PyString>,
|
||||
col_offset: Py<PyString>,
|
||||
end_lineno: Py<PyString>,
|
||||
end_col_offset: Py<PyString>,
|
||||
none: Py<PyAny>,
|
||||
bool_values: (Py<PyBool>, Py<PyBool>),
|
||||
bool_int_values: (Py<PyAny>, Py<PyAny>),
|
||||
conversion_flags: (Py<PyAny>, Py<PyAny>, Py<PyAny>, Py<PyAny>),
|
||||
}
|
||||
|
||||
fn ast_key_cache() -> &'static OnceCell<AstKeyCache> {
|
||||
impl AstCache {
|
||||
#[inline]
|
||||
fn none_ref<'py>(&'static self, py: Python<'py>) -> &'py PyAny {
|
||||
Py::<PyAny>::as_ref(&self.none, py)
|
||||
}
|
||||
#[inline]
|
||||
fn bool_int<'py>(&'static self, py: Python<'py>, value: bool) -> &'py PyAny {
|
||||
let v = &self.bool_int_values;
|
||||
Py::<PyAny>::as_ref(if value { &v.1 } else { &v.0 }, py)
|
||||
}
|
||||
#[inline]
|
||||
fn bool(&'static self, py: Python, value: bool) -> Py<PyBool> {
|
||||
let v = &self.bool_values;
|
||||
(if value { &v.1 } else { &v.0 }).clone_ref(py)
|
||||
}
|
||||
fn conversion_flag<'py>(&'static self, py: Python<'py>, value: ConversionFlag) -> &'py PyAny {
|
||||
let v = &self.conversion_flags;
|
||||
match value {
|
||||
ConversionFlag::None => v.0.as_ref(py),
|
||||
ConversionFlag::Str => v.1.as_ref(py),
|
||||
ConversionFlag::Ascii => v.2.as_ref(py),
|
||||
ConversionFlag::Repr => v.3.as_ref(py),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ast_cache_cell() -> &'static OnceCell<AstCache> {
|
||||
{
|
||||
static PY_TYPE: OnceCell<AstKeyCache> = OnceCell::new();
|
||||
static PY_TYPE: OnceCell<AstCache> = OnceCell::new();
|
||||
&PY_TYPE
|
||||
}
|
||||
}
|
||||
|
||||
fn ast_cache() -> &'static AstCache {
|
||||
ast_cache_cell().get().unwrap()
|
||||
}
|
||||
|
||||
pub fn init(py: Python) -> PyResult<()> {
|
||||
ast_key_cache().get_or_init(|| AstKeyCache {
|
||||
ast_cache_cell().get_or_init(|| AstCache {
|
||||
lineno: pyo3::intern!(py, "lineno").into_py(py),
|
||||
col_offset: pyo3::intern!(py, "col_offset").into_py(py),
|
||||
end_lineno: pyo3::intern!(py, "end_lineno").into_py(py),
|
||||
end_col_offset: pyo3::intern!(py, "end_col_offset").into_py(py),
|
||||
none: py.None(),
|
||||
bool_values: (PyBool::new(py, false).into(), PyBool::new(py, true).into()),
|
||||
bool_int_values: ((0).to_object(py), (1).to_object(py)),
|
||||
conversion_flags: (
|
||||
(-1).to_object(py),
|
||||
(b's').to_object(py),
|
||||
(b'a').to_object(py),
|
||||
(b'r').to_object(py),
|
||||
),
|
||||
});
|
||||
|
||||
init_types(py)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue