Skip to content

Commit

Permalink
Allow NUL character in f-strings (#7378)
Browse files Browse the repository at this point in the history
## Summary

This PR fixes the bug to allow `NUL` (`\0`) character inside f-strings.

## Test Plan

Add test case with `NUL` character inside f-string.
  • Loading branch information
dhruvmanila committed Sep 28, 2023
1 parent dac59a2 commit e6f36b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
11 changes: 10 additions & 1 deletion crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,10 @@ impl<'source> Lexer<'source> {

loop {
match self.cursor.first() {
EOF_CHAR => {
// The condition is to differentiate between the `NUL` (`\0`) character
// in the source code and the one returned by `self.cursor.first()` when
// we reach the end of the source code.
EOF_CHAR if self.cursor.is_eof() => {
let error = if fstring.is_triple_quoted() {
FStringErrorType::UnterminatedTripleQuotedString
} else {
Expand Down Expand Up @@ -2079,6 +2082,12 @@ f"{(lambda x:{x})}"
assert_debug_snapshot!(lex_source(source));
}

#[test]
fn test_fstring_with_nul_char() {
let source = r"f'\0'";
assert_debug_snapshot!(lex_source(source));
}

fn lex_fstring_error(source: &str) -> FStringErrorType {
match lex(source, Mode::Module).find_map(std::result::Result::err) {
Some(err) => match err.error {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: crates/ruff_python_parser/src/lexer.rs
expression: lex_source(source)
---
[
(
FStringStart,
0..2,
),
(
FStringMiddle {
value: "\\0",
is_raw: false,
},
2..4,
),
(
FStringEnd,
4..5,
),
(
Newline,
5..5,
),
]

0 comments on commit e6f36b9

Please sign in to comment.