Skip to content

Commit

Permalink
Changed the tokenizer so it is bug-for-bug compatible with the CPytho…
Browse files Browse the repository at this point in the history
…n tokenizer in versions 3.10 and newer in the case where a backslash (continuation character) is located by itself on a line. This addresses #7799.
  • Loading branch information
erictraut committed Apr 29, 2024
1 parent 1aed0e3 commit fcf5b37
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/pyright-internal/src/parser/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,25 @@ export class Tokenizer {
} else {
this._cs.advance(2);
}

this._addLineRange();

if (this._tokens.length > 0 && this._tokens[this._tokens.length - 1].type === TokenType.NewLine) {
this._readIndentationAfterNewLine();
}
return true;
} else if (this._cs.nextChar === Char.LineFeed) {
}

if (this._cs.nextChar === Char.LineFeed) {
this._cs.advance(2);
this._addLineRange();

if (this._tokens.length > 0 && this._tokens[this._tokens.length - 1].type === TokenType.NewLine) {
this._readIndentationAfterNewLine();
}
return true;
}

return this._handleInvalid();
}

Expand Down
10 changes: 8 additions & 2 deletions packages/pyright-internal/src/tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ test('Empty', () => {
assert.equal(parserOutput.parseTree.statements.length, 0);
});

test('Sample1', () => {
test('Parser1', () => {
const diagSink = new DiagnosticSink();
const parserOutput = TestUtils.parseSampleFile('sample1.py', diagSink).parserOutput;
const parserOutput = TestUtils.parseSampleFile('parser1.py', diagSink).parserOutput;

assert.equal(diagSink.fetchAndClear().length, 0);
assert.equal(parserOutput.parseTree.statements.length, 4);
});

test('Parser2', () => {
const diagSink = new DiagnosticSink();
TestUtils.parseSampleFile('parser2.py', diagSink);
assert.strictEqual(diagSink.getErrors().length, 0);
});

test('FStringEmptyTuple', () => {
assert.doesNotThrow(() => {
const diagSink = new DiagnosticSink();
Expand Down
11 changes: 11 additions & 0 deletions packages/pyright-internal/src/tests/samples/parser2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class A:
\
pass

class B:
\
pass

class C:
\
pass

0 comments on commit fcf5b37

Please sign in to comment.