Skip to content

Commit b81273e

Browse files
committed
to_pyo3_ast to return &'py
1 parent e1f02fc commit b81273e

File tree

3 files changed

+1294
-1293
lines changed

3 files changed

+1294
-1293
lines changed

ast/asdl_rs.py

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,13 @@ def emit_range(self, has_attributes, depth):
300300

301301
def visitModule(self, mod):
302302
self.emit_attrs(0)
303-
self.emit("""
303+
self.emit(
304+
"""
304305
#[derive(is_macro::Is)]
305306
pub enum Ast<R=TextRange> {
306-
""", 0)
307+
""",
308+
0,
309+
)
307310
for dfn in mod.dfns:
308311
rust_name = rust_type_name(dfn.name)
309312
generics = "" if self.type_info[dfn.name].is_simple else "<R>"
@@ -315,23 +318,29 @@ def visitModule(self, mod):
315318
# "ast_" prefix to everywhere seems less useful.
316319
self.emit('#[is(name = "module")]', 1)
317320
self.emit(f"{rust_name}({rust_name}{generics}),", 1)
318-
self.emit("""
319-
}
320-
impl<R> Node for Ast<R> {
321-
const NAME: &'static str = "AST";
322-
const FIELD_NAMES: &'static [&'static str] = &[];
323-
}
324-
""", 0)
321+
self.emit(
322+
"""
323+
}
324+
impl<R> Node for Ast<R> {
325+
const NAME: &'static str = "AST";
326+
const FIELD_NAMES: &'static [&'static str] = &[];
327+
}
328+
""",
329+
0,
330+
)
325331
for dfn in mod.dfns:
326332
rust_name = rust_type_name(dfn.name)
327333
generics = "" if self.type_info[dfn.name].is_simple else "<R>"
328-
self.emit(f"""
334+
self.emit(
335+
f"""
329336
impl<R> From<{rust_name}{generics}> for Ast<R> {{
330337
fn from(node: {rust_name}{generics}) -> Self {{
331338
Ast::{rust_name}(node)
332339
}}
333340
}}
334-
""", 0)
341+
""",
342+
0,
343+
)
335344

336345
for dfn in mod.dfns:
337346
self.visit(dfn)
@@ -663,9 +672,7 @@ def visitConstructor(self, cons, type, depth):
663672

664673
cons_type_name = f"{enum_name}{cons.name}"
665674

666-
self.emit(
667-
f"impl<T, U> Foldable<T, U> for {cons_type_name}{apply_t} {{", depth
668-
)
675+
self.emit(f"impl<T, U> Foldable<T, U> for {cons_type_name}{apply_t} {{", depth)
669676
self.emit(f"type Mapped = {cons_type_name}{apply_u};", depth + 1)
670677
self.emit(
671678
"fn fold<F: Fold<T, TargetU = U> + ?Sized>(self, folder: &mut F) -> Result<Self::Mapped, F::Error> {",
@@ -1097,7 +1104,7 @@ def visitSum(self, sum, type):
10971104
f"""
10981105
impl ToPyo3Ast for crate::generic::{rust_name}{self.generics} {{
10991106
#[inline]
1100-
fn to_pyo3_ast(&self, {"_" if simple else ""}py: Python) -> PyResult<Py<PyAny>> {{
1107+
fn to_pyo3_ast<'py>(&self, {"_" if simple else ""}py: Python<'py>) -> PyResult<&'py PyAny> {{
11011108
let instance = match &self {{
11021109
""",
11031110
0,
@@ -1130,7 +1137,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
11301137
f"""
11311138
impl ToPyo3Ast for crate::{name}{self.generics} {{
11321139
#[inline]
1133-
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {{
1140+
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
11341141
let cache = Self::py_type_cache().get().unwrap();
11351142
""",
11361143
0,
@@ -1144,10 +1151,37 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
11441151
1,
11451152
)
11461153
self.emit(
1147-
"let instance = cache.0.call1(py, (",
1154+
"""
1155+
let instance = Py::<PyAny>::as_ref(&cache.0, py).call1((
1156+
""",
11481157
1,
11491158
)
11501159
for field in cons.fields:
1160+
if field.type == "constant":
1161+
self.emit(
1162+
f"{rust_field(field.name)}.to_object(py),",
1163+
3,
1164+
)
1165+
continue
1166+
if field.type == "int":
1167+
if field.name == "level":
1168+
assert field.opt
1169+
self.emit(
1170+
f"{rust_field(field.name)}.map_or_else(|| py.None(), |level| level.to_u32().to_object(py)),",
1171+
3,
1172+
)
1173+
continue
1174+
if field.name in (
1175+
"lineno",
1176+
"col_offset",
1177+
"end_lineno",
1178+
"end_col_offset",
1179+
):
1180+
self.emit(
1181+
f"{rust_field(field.name)}.to_u32().to_object(py),",
1182+
3,
1183+
)
1184+
continue
11511185
self.emit(
11521186
f"{rust_field(field.name)}.to_pyo3_ast(py)?,",
11531187
3,
@@ -1158,7 +1192,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
11581192
)
11591193
else:
11601194
self.emit(
1161-
"let instance = cache.0.call0(py)?;",
1195+
"let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;",
11621196
1,
11631197
)
11641198
self.emit(
@@ -1168,12 +1202,12 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
11681202
if type.value.attributes and self.namespace == "located":
11691203
self.emit(
11701204
"""
1171-
let cache = ast_key_cache().get().unwrap();
1172-
instance.setattr(py, cache.lineno.as_ref(py), _range.start.row.get())?;
1173-
instance.setattr(py, cache.col_offset.as_ref(py), _range.start.column.get())?;
1205+
let cache = ast_cache();
1206+
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
1207+
instance.setattr(cache.col_offset.as_ref(py), _range.start.column.get())?;
11741208
if let Some(end) = _range.end {
1175-
instance.setattr(py, cache.end_lineno.as_ref(py), end.row.get())?;
1176-
instance.setattr(py, cache.end_col_offset.as_ref(py), end.column.get())?;
1209+
instance.setattr(cache.end_lineno.as_ref(py), end.row.get())?;
1210+
instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
11771211
}
11781212
""",
11791213
1,
@@ -1858,7 +1892,7 @@ def write_to_pyo3_simple(type_info, f):
18581892
f"""
18591893
impl ToPyo3Ast for crate::generic::{rust_name} {{
18601894
#[inline]
1861-
fn to_pyo3_ast(&self, _py: Python) -> PyResult<Py<PyAny>> {{
1895+
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
18621896
let cell = match &self {{
18631897
""",
18641898
)
@@ -1869,7 +1903,7 @@ def write_to_pyo3_simple(type_info, f):
18691903
f.write(
18701904
"""
18711905
};
1872-
Ok(cell.get().unwrap().1.clone())
1906+
Ok(Py::<PyAny>::as_ref(&cell.get().unwrap().1, py))
18731907
}
18741908
}
18751909
""",

0 commit comments

Comments
 (0)