Skip to content

Commit e2b6961

Browse files
committed
Merge branch 'master' of github.com:RustPython/RustPython into stack-size-calculation
2 parents 7239229 + 188ff0e commit e2b6961

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

parser/src/lexer.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub use super::token::Tok;
88
use crate::error::{LexicalError, LexicalErrorType};
99
use crate::location::Location;
1010
use num_bigint::BigInt;
11+
use num_traits::identities::Zero;
1112
use num_traits::Num;
1213
use std::cmp::Ordering;
1314
use std::collections::HashMap;
@@ -352,7 +353,7 @@ where
352353
/// Lex a normal number, that is, no octal, hex or binary number.
353354
fn lex_normal_number(&mut self) -> LexResult {
354355
let start_pos = self.get_pos();
355-
356+
let start_is_zero = self.chr0 == Some('0');
356357
// Normal number:
357358
let mut value_text = self.radix_run(10);
358359

@@ -403,6 +404,12 @@ where
403404
} else {
404405
let end_pos = self.get_pos();
405406
let value = value_text.parse::<BigInt>().unwrap();
407+
if start_is_zero && !value.is_zero() {
408+
return Err(LexicalError {
409+
error: LexicalErrorType::OtherError("Invalid Token".to_string()),
410+
location: self.get_pos(),
411+
});
412+
}
406413
Ok((start_pos, Tok::Int { value }, end_pos))
407414
}
408415
}

tests/snippets/ints.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,24 @@ def __int__(self):
232232
assert_raises(TypeError, lambda: (1).__round__(None))
233233
assert_raises(TypeError, lambda: (0).__round__(0.0))
234234
assert_raises(TypeError, lambda: (1).__round__(0.0))
235+
236+
assert 00 == 0
237+
assert 0_0 == 0
238+
assert 03.2 == 3.2
239+
assert 3+02j == 3+2j
240+
241+
# Invalid syntax:
242+
src = """
243+
b = 02
244+
"""
245+
246+
with assert_raises(SyntaxError):
247+
exec(src)
248+
249+
# Invalid syntax:
250+
src = """
251+
b = 03 + 2j
252+
"""
253+
254+
with assert_raises(SyntaxError):
255+
exec(src)

0 commit comments

Comments
 (0)