-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add symtable.py from 3.13.5
#6048
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
Changes from 1 commit
c87884a
32d63cc
09a7fcf
cabfd73
796b335
570fb54
16f7737
433081b
75981e3
e66d264
8066fe5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,9 +5,7 @@ mod symtable { | |||||||||||
| use crate::{ | ||||||||||||
| PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, builtins::PyStrRef, compiler, | ||||||||||||
| }; | ||||||||||||
| use rustpython_codegen::symboltable::{ | ||||||||||||
| CompilerScope, Symbol, SymbolFlags, SymbolScope, SymbolTable, | ||||||||||||
| }; | ||||||||||||
| use rustpython_codegen::symboltable::{Symbol, SymbolFlags, SymbolScope, SymbolTable}; | ||||||||||||
| use std::fmt; | ||||||||||||
|
|
||||||||||||
| // Consts as defined at | ||||||||||||
|
|
@@ -79,6 +77,30 @@ mod symtable { | |||||||||||
| #[pyattr] | ||||||||||||
| pub const GENERATOR_EXPRESSION: i32 = 2; | ||||||||||||
|
|
||||||||||||
| #[pyattr] | ||||||||||||
| pub const SCOPE_OFF: i32 = SCOPE_OFFSET; | ||||||||||||
|
|
||||||||||||
| #[pyattr] | ||||||||||||
| pub const TYPE_FUNCTION: i32 = 0; | ||||||||||||
|
|
||||||||||||
| #[pyattr] | ||||||||||||
| pub const TYPE_CLASS: i32 = 1; | ||||||||||||
|
|
||||||||||||
| #[pyattr] | ||||||||||||
| pub const TYPE_MODULE: i32 = 2; | ||||||||||||
|
|
||||||||||||
| #[pyattr] | ||||||||||||
| pub const TYPE_ANNOTATION: i32 = 3; | ||||||||||||
|
|
||||||||||||
| #[pyattr] | ||||||||||||
| pub const TYPE_TYPE_VAR_BOUND: i32 = 4; | ||||||||||||
|
|
||||||||||||
| #[pyattr] | ||||||||||||
| pub const TYPE_TYPE_ALIAS: i32 = 5; | ||||||||||||
|
|
||||||||||||
| #[pyattr] | ||||||||||||
| pub const TYPE_TYPE_PARAM: i32 = 6; | ||||||||||||
|
|
||||||||||||
| #[pyfunction] | ||||||||||||
| fn symtable( | ||||||||||||
| source: PyStrRef, | ||||||||||||
|
|
@@ -117,57 +139,39 @@ mod symtable { | |||||||||||
|
|
||||||||||||
| #[pyclass] | ||||||||||||
| impl PySymbolTable { | ||||||||||||
| #[pymethod] | ||||||||||||
| fn get_name(&self) -> String { | ||||||||||||
| #[pygetset] | ||||||||||||
| fn name(&self) -> String { | ||||||||||||
| self.symtable.name.clone() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[pymethod] | ||||||||||||
| fn get_type(&self) -> String { | ||||||||||||
| #[pygetset(name = "type")] | ||||||||||||
| fn typ(&self) -> String { | ||||||||||||
| self.symtable.typ.to_string() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[pymethod] | ||||||||||||
| const fn get_lineno(&self) -> u32 { | ||||||||||||
| #[pygetset] | ||||||||||||
| const fn lineno(&self) -> u32 { | ||||||||||||
| self.symtable.line_number | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[pymethod] | ||||||||||||
| const fn is_nested(&self) -> bool { | ||||||||||||
| self.symtable.is_nested | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[pymethod] | ||||||||||||
| fn is_optimized(&self) -> bool { | ||||||||||||
| matches!( | ||||||||||||
| self.symtable.typ, | ||||||||||||
| CompilerScope::Function | CompilerScope::AsyncFunction | ||||||||||||
| ) | ||||||||||||
| #[pygetset] | ||||||||||||
| fn children(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> { | ||||||||||||
| let children = self | ||||||||||||
| .symtable | ||||||||||||
| .sub_tables | ||||||||||||
| .iter() | ||||||||||||
| .map(|t| to_py_symbol_table(t.clone()).into_pyobject(vm)) | ||||||||||||
| .collect(); | ||||||||||||
| Ok(children) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[pymethod] | ||||||||||||
| fn lookup(&self, name: PyStrRef, vm: &VirtualMachine) -> PyResult<PyRef<PySymbol>> { | ||||||||||||
| let name = name.as_str(); | ||||||||||||
| if let Some(symbol) = self.symtable.symbols.get(name) { | ||||||||||||
| Ok(PySymbol { | ||||||||||||
| symbol: symbol.clone(), | ||||||||||||
| namespaces: self | ||||||||||||
| .symtable | ||||||||||||
| .sub_tables | ||||||||||||
| .iter() | ||||||||||||
| .filter(|table| table.name == name) | ||||||||||||
| .cloned() | ||||||||||||
| .collect(), | ||||||||||||
| is_top_scope: self.symtable.name == "top", | ||||||||||||
| } | ||||||||||||
| .into_ref(&vm.ctx)) | ||||||||||||
| } else { | ||||||||||||
| Err(vm.new_key_error(vm.ctx.new_str(format!("lookup {name} failed")).into())) | ||||||||||||
| } | ||||||||||||
| #[pygetset] | ||||||||||||
| fn id(&self) -> usize { | ||||||||||||
| self as *const Self as *const std::ffi::c_void as usize | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+181
to
184
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainConsider security implications of exposing memory addresses. The Consider whether this aligns with CPython's behavior and if there are security implications for your use case: 🌐 Web query: 💡 Result: The Details:
Summary Table of Relevant SymbolTable Methods:
If you require a value that is stable and reveals no internal information, prefer using References: Citations:
Align SymbolTable.id() with CPython’s get_id(): avoid exposing raw pointers The current implementation in Suggested changes:
Locations to update:
Example diff sketch: - #[pygetset]
- fn id(&self) -> usize {
- self as *const Self as *const std::ffi::c_void as usize
- }
+ // Use an internal counter instead of raw pointer to avoid leaking memory addresses
+ #[pygetset]
+ fn id(&self) -> usize {
+ self.inner_id // populated from a global AtomicUsize at creation
+ }
🤖 Prompt for AI Agents |
||||||||||||
|
|
||||||||||||
| #[pymethod] | ||||||||||||
| fn get_identifiers(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> { | ||||||||||||
| #[pygetset] | ||||||||||||
| fn identifiers(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> { | ||||||||||||
| let symbols = self | ||||||||||||
| .symtable | ||||||||||||
| .symbols | ||||||||||||
|
|
@@ -177,8 +181,8 @@ mod symtable { | |||||||||||
| Ok(symbols) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[pymethod] | ||||||||||||
| fn get_symbols(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> { | ||||||||||||
| #[pygetset] | ||||||||||||
| fn symbols(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> { | ||||||||||||
| let symbols = self | ||||||||||||
| .symtable | ||||||||||||
| .symbols | ||||||||||||
|
|
@@ -201,22 +205,6 @@ mod symtable { | |||||||||||
| .collect(); | ||||||||||||
| Ok(symbols) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[pymethod] | ||||||||||||
| const fn has_children(&self) -> bool { | ||||||||||||
| !self.symtable.sub_tables.is_empty() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[pymethod] | ||||||||||||
| fn get_children(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> { | ||||||||||||
| let children = self | ||||||||||||
| .symtable | ||||||||||||
| .sub_tables | ||||||||||||
| .iter() | ||||||||||||
| .map(|t| to_py_symbol_table(t.clone()).into_pyobject(vm)) | ||||||||||||
| .collect(); | ||||||||||||
| Ok(children) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[pyattr] | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify integer constant mapping for type property.
The
typ()method now returns integer constants instead of strings, which is a significant interface change. The mapping looks reasonable, but the fallback to-1for unhandled cases may not align with CPython's behavior.🏁 Script executed:
Length of output: 297
Ensure
typeproperty matches CPython’s string-based APICPython’s SymbolTable.get_type() returns strings like
"module"and"function", not integer codes. Our Rusttyp()currently returns integer constants and falls back to-1for any unhandled cases, which:Please update the implementation to either:
-1fallback with a dedicatedTYPE_UNKNOWNconstant—and add mappings for all types in CPython’s C implementation.Points to address:
// TODOand implement mappings for missing CPython types (generator, lambda, comprehension, etc.)-1fallback or replace it with a well-defined default constant🤖 Prompt for AI Agents