From 527002042365163dbdd7c133417c21814d7cac90 Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" <69878+youknowone@users.noreply.github.com> Date: Sat, 17 Jun 2023 01:32:27 +0900 Subject: [PATCH] rustpython_ast python package (#79) --- Cargo.toml | 2 +- ast-pyo3/Cargo.toml | 4 +++- ast-pyo3/pyproject.toml | 2 +- ast-pyo3/rustpython_ast/__init__.py | 3 +++ ast-pyo3/src/py_ast.rs | 6 +++++- 5 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 ast-pyo3/rustpython_ast/__init__.py diff --git a/Cargo.toml b/Cargo.toml index 88e8a56..2342f7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/ast-pyo3/Cargo.toml b/ast-pyo3/Cargo.toml index bddf453..bf145ab 100644 --- a/ast-pyo3/Cargo.toml +++ b/ast-pyo3/Cargo.toml @@ -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"] } diff --git a/ast-pyo3/pyproject.toml b/ast-pyo3/pyproject.toml index f5e1aac..f6e6dbc 100644 --- a/ast-pyo3/pyproject.toml +++ b/ast-pyo3/pyproject.toml @@ -10,6 +10,6 @@ classifiers = [ "Programming Language :: Python :: Implementation :: CPython", ] - [tool.maturin] +# module-name = "_rustpython_ast" features = ["pyo3/extension-module"] diff --git a/ast-pyo3/rustpython_ast/__init__.py b/ast-pyo3/rustpython_ast/__init__.py new file mode 100644 index 0000000..a013bb1 --- /dev/null +++ b/ast-pyo3/rustpython_ast/__init__.py @@ -0,0 +1,3 @@ +from rustpython_ast.rustpython_ast import parse + +__all__ = ["parse"] diff --git a/ast-pyo3/src/py_ast.rs b/ast-pyo3/src/py_ast.rs index 8a9ed7e..6d2bb41 100644 --- a/ast-pyo3/src/py_ast.rs +++ b/ast-pyo3/src/py_ast.rs @@ -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()