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

@ -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()