Skip to content
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 ast_serialize.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class _ASTData(TypedDict):

def parse(
fnam: str,
source: str | None = None,
source: str | bytes | None = None,
skip_function_bodies: bool = False,
python_version: tuple[int, int] | None = None,
platform: str | None = None,
Expand Down
36 changes: 17 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub mod type_comment;
fn parse(
py: Python,
fnam: String,
source: Option<String>,
source: Option<serialize_ast::Source>,
skip_function_bodies: bool,
python_version: Option<(u32, u32)>,
platform: Option<String>,
Expand All @@ -66,14 +66,14 @@ fn parse(

let always_true = always_true.unwrap_or_default();
let always_false = always_false.unwrap_or_default();

if source.is_some() {
return Err(PyErr::new::<PyValueError, _>(
"Source parsing is not supported yet",
));
}

let path = Path::new(&fnam);
let options = options::Options::new(
python_version,
platform,
always_true,
always_false,
cache_version,
);
let (
ast_bytes,
syntax_errors,
Expand All @@ -85,17 +85,15 @@ fn parse(
source_hash,
) = py
.detach(|| {
serialize_ast::serialize_python_file(
path,
skip_function_bodies,
options::Options::new(
python_version,
platform,
always_true,
always_false,
cache_version,
),
)
if let Some(src) = source {
let s = match src {
serialize_ast::Source::Text(s) => s,
serialize_ast::Source::Bytes(s) => String::from_utf8(s)?,
};
serialize_ast::serialize_python_source(s, skip_function_bodies, options)
} else {
serialize_ast::serialize_python_file(path, skip_function_bodies, options)
}
})
.map_err(|e| PyErr::new::<PyValueError, _>(e.to_string()))?;

Expand Down
Loading
Loading