Skip to content

Cause syntax error when assigning to 'var #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion macros/src/embed_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl EmbedPython {
unreachable!()
};
let name_str = format!("_RUST_{}", name);
self.python.push_str(&name_str);
self.python.push_str(&format!("((lambda:{})())", name_str));
self.loc.column += name_str.chars().count() - 6 + 1;
self.variables.entry(name_str).or_insert(name);
} else if x.as_char() == '#' && x.spacing() == Spacing::Joint {
Expand Down
5 changes: 4 additions & 1 deletion macros/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> Token
let msg: Option<String> = value.getattr(py, "msg").ok().and_then(|x| x.extract(py).ok());
if let (Some(line), Some(msg)) = (line, msg) {
if let Some(span) = span_for_line(tokens.clone(), line) {
let error = format!("python: {}", msg);
let mut error = format!("python: {}", msg);
if msg == "cannot assign to function call" {
error += ". LIKELY CAUSE: you cannot assign to RUST-Variables, see Context::get_global() instead";
}
return quote_spanned!(span.into() => compile_error!{#error});
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//!
//! To reference Rust variables, use `'var`, as shown in the example above.
//! `var` needs to implement [`pyo3::ToPyObject`].
//! Do not assign to a `'var`, it will cause a Syntax Error `cannot assign to function call`.
//!
//! ## Re-using a Python context
//!
Expand Down Expand Up @@ -71,6 +72,17 @@
//! assert_eq!(c.get::<i32>("foo"), 5);
//! ```
//!
//! ## Compile Errors
//! The python code is compiled when the rust code is compiled, so syntax errors are emitted by
//! `rustc` and not at runtime. They should show up in your IDE.
//!
//! Changing rust variables will cause a syntax error because they are implemented as
//! `((lambda:value)())`
//! ```compile_fail
//! # use inline_python::python;
//! python!{ 'x = 42 }
//! ```
//!
//! ## Syntax issues
//!
//! Since the Rust tokenizer will tokenize the Python code, some valid Python
Expand Down