Skip to content

Commit de413f9

Browse files
committed
Fix remaining ast tests
1 parent b90d02e commit de413f9

File tree

8 files changed

+173
-101
lines changed

8 files changed

+173
-101
lines changed

Lib/test/test_ast.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ def test_invalid_position_information(self):
388388
with self.assertRaises(ValueError):
389389
compile(tree, '<string>', 'exec')
390390

391+
# XXX RUSTPYTHON: we always require that end ranges be present
392+
@unittest.expectedFailure
391393
def test_compilation_of_ast_nodes_with_default_end_position_values(self):
392394
tree = ast.Module(body=[
393395
ast.Import(names=[ast.alias(name='builtins', lineno=1, col_offset=0)], lineno=1, col_offset=0),
@@ -1531,6 +1533,8 @@ def test_literal_eval_malformed_dict_nodes(self):
15311533
malformed = ast.Dict(keys=[ast.Constant(1)], values=[ast.Constant(2), ast.Constant(3)])
15321534
self.assertRaises(ValueError, ast.literal_eval, malformed)
15331535

1536+
# TODO: RUSTPYTHON
1537+
@unittest.expectedFailure
15341538
def test_literal_eval_trailing_ws(self):
15351539
self.assertEqual(ast.literal_eval(" -1"), -1)
15361540
self.assertEqual(ast.literal_eval("\t\t-1"), -1)
@@ -1549,6 +1553,8 @@ def test_literal_eval_malformed_lineno(self):
15491553
with self.assertRaisesRegex(ValueError, msg):
15501554
ast.literal_eval(node)
15511555

1556+
# TODO: RUSTPYTHON
1557+
@unittest.expectedFailure
15521558
def test_literal_eval_syntax_errors(self):
15531559
with self.assertRaisesRegex(SyntaxError, "unexpected indent"):
15541560
ast.literal_eval(r'''
@@ -1569,6 +1575,8 @@ def test_bad_integer(self):
15691575
compile(mod, 'test', 'exec')
15701576
self.assertIn("invalid integer value: None", str(cm.exception))
15711577

1578+
# XXX RUSTPYTHON: we always require that end ranges be present
1579+
@unittest.expectedFailure
15721580
def test_level_as_none(self):
15731581
body = [ast.ImportFrom(module='time',
15741582
names=[ast.alias(name='sleep',
@@ -2034,8 +2042,6 @@ def test_call(self):
20342042
call = ast.Call(func, args, bad_keywords)
20352043
self.expr(call, "must have Load context")
20362044

2037-
# TODO: RUSTPYTHON
2038-
@unittest.expectedFailure
20392045
def test_num(self):
20402046
with warnings.catch_warnings(record=True) as wlog:
20412047
warnings.filterwarnings('ignore', '', DeprecationWarning)
@@ -2733,8 +2739,6 @@ def test_source_segment_multi(self):
27332739
binop = self._parse_value(s_orig)
27342740
self.assertEqual(ast.get_source_segment(s_orig, binop.left), s_tuple)
27352741

2736-
# TODO: RUSTPYTHON
2737-
@unittest.expectedFailure
27382742
def test_source_segment_padded(self):
27392743
s_orig = dedent('''
27402744
class C:
@@ -2756,8 +2760,6 @@ def test_source_segment_endings(self):
27562760
self._check_content(s, y, 'y = 1')
27572761
self._check_content(s, z, 'z = 1')
27582762

2759-
# TODO: RUSTPYTHON
2760-
@unittest.expectedFailure
27612763
def test_source_segment_tabs(self):
27622764
s = dedent('''
27632765
class C:

compiler/codegen/src/compile.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,11 +2680,7 @@ impl Compiler<'_> {
26802680
}
26812681
Expr::NumberLiteral(number) => match &number.value {
26822682
Number::Int(int) => {
2683-
let value = if let Some(small) = int.as_u64() {
2684-
BigInt::from(small)
2685-
} else {
2686-
parse_big_integer(int).map_err(|e| self.error(e))?
2687-
};
2683+
let value = ruff_int_to_bigint(int).map_err(|e| self.error(e))?;
26882684
self.emit_load_const(ConstantData::Integer { value });
26892685
}
26902686
Number::Float(float) => {
@@ -3534,6 +3530,14 @@ fn split_doc<'a>(body: &'a [Stmt], opts: &CompileOpts) -> (Option<String>, &'a [
35343530
(None, body)
35353531
}
35363532

3533+
pub fn ruff_int_to_bigint(int: &Int) -> Result<BigInt, CodegenErrorType> {
3534+
if let Some(small) = int.as_u64() {
3535+
Ok(BigInt::from(small))
3536+
} else {
3537+
parse_big_integer(int)
3538+
}
3539+
}
3540+
35373541
/// Converts a `ruff` ast integer into a `BigInt`.
35383542
/// Unlike small integers, big integers may be stored in one of four possible radix representations.
35393543
fn parse_big_integer(int: &Int) -> Result<BigInt, CodegenErrorType> {

vm/src/stdlib/ast/basic.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rustpython_codegen::compile::ruff_int_to_bigint;
2+
13
use super::*;
24

35
impl Node for ruff::Identifier {
@@ -18,20 +20,7 @@ impl Node for ruff::Identifier {
1820

1921
impl Node for ruff::Int {
2022
fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef {
21-
if let Some(int) = self.as_i32() {
22-
vm.ctx.new_int(int)
23-
} else if let Some(int) = self.as_u32() {
24-
vm.ctx.new_int(int)
25-
} else if let Some(int) = self.as_i64() {
26-
vm.ctx.new_int(int)
27-
} else if let Some(int) = self.as_u64() {
28-
vm.ctx.new_int(int)
29-
} else {
30-
// FIXME: performance
31-
let int = self.to_string().parse().unwrap();
32-
vm.ctx.new_bigint(&int)
33-
}
34-
.into()
23+
vm.ctx.new_int(ruff_int_to_bigint(&self).unwrap()).into()
3524
}
3625

3726
fn ast_from_object(

0 commit comments

Comments
 (0)