rustpython_ast python package (#79)

This commit is contained in:
Jeong, YunWon 2023-06-17 01:32:27 +09:00 committed by GitHub
parent edcfcb4a74
commit 5270020423
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 4 deletions

View file

@ -33,7 +33,7 @@ log = "0.4.16"
num-complex = "0.4.0"
num-bigint = "0.4.3"
num-traits = "0.2"
pyo3 = { version = "0.18.3" }
pyo3 = { version = "0.19.0" }
malachite-bigint = { version = "0.1.0" }
memchr = "2.5.0"
rand = "0.8.5"

View file

@ -1,9 +1,10 @@
[package]
name = "rustpython-ast-pyo3"
version = "0.1.0"
version = "0.0.1"
edition = "2021"
[features]
# abi3 = ["pyo3/abi3-py37"] # will be supported from next pyo3 version
# This feature is experimental
# It reimplements AST types, but currently both slower than python AST types and limited to use in other API
wrapper = []
@ -16,6 +17,7 @@ crate-type = ["cdylib"]
rustpython-ast = { workspace = true, features = ["location"] }
rustpython-parser = { workspace = true, features = ["num-bigint"] }
num-complex = { workspace = true }
num-traits = { workspace = true }
once_cell = { workspace = true }
pyo3 = { workspace = true, features = ["num-bigint", "num-complex"] }

View file

@ -10,6 +10,6 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
]
[tool.maturin]
# module-name = "_rustpython_ast"
features = ["pyo3/extension-module"]

View file

@ -0,0 +1,3 @@
from rustpython_ast.rustpython_ast import parse
__all__ = ["parse"]

View file

@ -1,4 +1,5 @@
use num_complex::Complex64;
use num_traits::cast::ToPrimitive;
use once_cell::sync::OnceCell;
use pyo3::{
prelude::*,
@ -97,7 +98,10 @@ fn constant_to_object(constant: &ast::Constant, py: Python) -> PyObject {
ast::Constant::Bool(bool) => cache.bool(py, *bool).into(),
ast::Constant::Str(string) => string.to_object(py),
ast::Constant::Bytes(bytes) => PyBytes::new(py, bytes).into(),
ast::Constant::Int(int) => int.to_object(py),
ast::Constant::Int(int) => match int.to_i64() {
Some(small_int) => small_int.to_object(py),
None => int.to_object(py),
},
ast::Constant::Tuple(elts) => {
let elts: Vec<_> = elts.iter().map(|c| constant_to_object(c, py)).collect();
PyTuple::new(py, elts).into()