1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use proc_macro2::TokenStream;
use quote::quote;
use crate::python_ast::{CodeGen, CodeGenContext, ExprType, PythonOptions, SymbolTableScopes};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
//#[pyo3(transparent)]
pub struct Attribute {
pub value: Box<ExprType>,
pub attr: String,
ctx: String,
}
impl<'a> CodeGen for Attribute {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn to_rust(self, ctx: Self::Context, options: Self::Options, symbols: Self::SymbolTable) -> Result<TokenStream, Box<dyn std::error::Error>> {
let value_tokens = self.value.to_rust(ctx, options, symbols)?;
let value_str = value_tokens.to_string();
let attr = crate::python_ast::safe_ident(&self.attr);
// Determine if this is a module access or a field/method access
// Module names are typically lowercase and match Python stdlib modules
let is_module_access = matches!(
value_str.as_str(),
"sys" | "os" | "subprocess" | "json" | "urllib" | "xml" | "asyncio" |
"time" | "math" | "random" | "heapq" | "functools" | "textwrap" | "itertools" | "re" | "hashlib" | "csv" | "io" |
// `datetime` covers both the runtime module and the datetime
// TYPE from `from datetime import datetime` — either way the
// attribute is a path item (datetime::strptime, datetime::now),
// never a field on a value.
"datetime" |
"os :: path" | "os::path" // for nested modules
);
if is_module_access {
// Use :: for module access (Python's sys.executable becomes sys::executable)
// Special handling for LazyLock static variables that need
// dereferencing. os::environ is NOT here: it is a live-view
// unit struct whose methods auto-ref.
let needs_deref = matches!((value_str.as_str(), self.attr.as_str()), ("sys", "executable") | ("sys", "argv"));
if needs_deref {
// Wrap dereferenced values in parentheses to ensure correct precedence
// This prevents *sys::executable.to_string() and ensures (*sys::executable).to_string()
Ok(quote!((*#value_tokens::#attr)))
} else {
Ok(quote!(#value_tokens::#attr))
}
} else {
// Use . for field/method access (Python's obj.field becomes obj.field)
Ok(quote!(#value_tokens.#attr))
}
}
}
impl Attribute {
/// Builds an attribute node; `ctx` is the CPython context name
/// ("Load", "Store" or "Del").
pub fn new(value: ExprType, attr: impl Into<String>, ctx: impl Into<String>) -> Self {
Self { value: Box::new(value), attr: attr.into(), ctx: ctx.into() }
}
}