Skip to content

Commit 82e8811

Browse files
Merge pull request RustPython#1499 from HyeockJinKim/issue1498
Take only numbers after `.`
2 parents 8462e7b + 2cd4c95 commit 82e8811

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

parser/src/lexer.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ where
357357
if self.chr0 == Some('.') || self.at_exponent() {
358358
// Take '.':
359359
if self.chr0 == Some('.') {
360+
if self.chr1 == Some('_') {
361+
return Err(LexicalError {
362+
error: LexicalErrorType::OtherError("Invalid Syntax".to_string()),
363+
location: self.get_pos(),
364+
});
365+
}
360366
value_text.push(self.next_char().unwrap());
361367
value_text.push_str(&self.radix_run(10));
362368
}
@@ -416,6 +422,7 @@ where
416422
/// like this: '1_2_3_4' == '1234'
417423
fn radix_run(&mut self, radix: u32) -> String {
418424
let mut value_text = String::new();
425+
419426
loop {
420427
if let Some(c) = self.take_number(radix) {
421428
value_text.push(c);

tests/snippets/floats.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@
206206
# Test special case for lexer, float starts with a dot:
207207
a = .5
208208
assert a == 0.5
209+
assert 3.14 == float('3.14')
210+
src = """
211+
a = 3._14
212+
"""
213+
214+
with assert_raises(SyntaxError):
215+
exec(src)
216+
src = """
217+
a = 3.__14
218+
"""
219+
220+
with assert_raises(SyntaxError):
221+
exec(src)
222+
223+
src = """
224+
a = 3.1__4
225+
"""
226+
227+
with assert_raises(SyntaxError):
228+
exec(src)
209229

210230
assert float.fromhex('0x0.0p+0') == 0.0
211231
assert float.fromhex('-0x0.0p+0') == -0.0

0 commit comments

Comments
 (0)