Skip to content
Closed
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
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
Changelog
=========

latest
------

* Use Rust instead of Python's built-in ast module for import parsing.
Note that `SourceSyntaxError` will no longer be raised if the parser encounters
invalid Python.

3.8.2 (2025-04-24)
------------------

Expand Down
76 changes: 76 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ rustc-hash = "2.1.0"
indexmap = "2.7.1"
regex = "1.11.1"
const_format = "0.2.34"
pyimportparse = "0.2.1"
nom = "8.0.0"
nom_locate = "5.0.0"
nom-regex = "0.2.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nom-regex is not being used I think, so we could remove the dep?

nom-unicode = "0.4.0"

[dependencies.pyo3]
version = "0.24.1"
Expand Down
31 changes: 30 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod errors;
pub mod exceptions;
pub mod graph;
pub mod module_expressions;
mod parsing;

use crate::errors::{GrimpError, GrimpResult};
use crate::exceptions::{InvalidModuleExpression, ModuleNotPresent, NoSuchContainer};
Expand All @@ -13,13 +14,15 @@ use itertools::Itertools;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::{IntoPyDict, PyDict, PyFrozenSet, PyList, PySet, PyString, PyTuple};
use pyo3::IntoPyObjectExt;
use pyo3::{IntoPyObjectExt};
use rayon::prelude::*;
use rustc_hash::FxHashSet;
use std::collections::HashSet;


#[pymodule]
fn _rustgrimp(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(parse_to_imported_objects))?;
m.add_class::<GraphWrapper>()?;
m.add("ModuleNotPresent", py.get_type::<ModuleNotPresent>())?;
m.add("NoSuchContainer", py.get_type::<NoSuchContainer>())?;
Expand All @@ -30,6 +33,32 @@ fn _rustgrimp(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
Ok(())
}




#[pyfunction]
fn parse_to_imported_objects<'py>(py: Python<'py>, python_code: &str) -> PyResult<Vec<Bound<'py, PyDict>>> {
let imports = match parsing::parse_imports(python_code) {
Ok(imports) => imports,
Err(message) => return Err(PyValueError::new_err(message)),
};

let imports_as_dicts = imports.into_iter().map(
|import| {
let dict = PyDict::new(py);
dict.set_item("name", import.imported_object).unwrap();
dict.set_item("line_number", import.line_number).unwrap();
dict.set_item("line_contents", import.line_contents).unwrap();
dict.set_item("typechecking_only", import.typechecking_only).unwrap();
dict
}
).collect();

Ok(imports_as_dicts)
}



#[pyclass(name = "Graph")]
struct GraphWrapper {
_graph: Graph,
Expand Down
Loading
Loading