Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-105042: Disable unmatched parens syntax error in python tokenize #105061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reuse flag and allow f-string check
  • Loading branch information
lysnikolaou committed May 30, 2023
commit ed1237aa654cfd12d1fea47eccfb506bf6666998
1 change: 0 additions & 1 deletion 1 Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion 1 Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ struct _Py_global_strings {
STRUCT_FOR_ID(id)
STRUCT_FOR_ID(ident)
STRUCT_FOR_ID(ignore)
STRUCT_FOR_ID(ignore_unmatched_parens)
STRUCT_FOR_ID(imag)
STRUCT_FOR_ID(importlib)
STRUCT_FOR_ID(in_fd)
Expand Down
1 change: 0 additions & 1 deletion 1 Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions 3 Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions 10 Lib/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,7 @@ def tokenize(readline):

def _tokenize(rl_gen, encoding):
source = b"".join(rl_gen).decode(encoding)
for token in _generate_tokens_from_c_tokenizer(source,
extra_tokens=True,
ignore_unmatched_parens=True):
for token in _generate_tokens_from_c_tokenizer(source, extra_tokens=True):
yield token

def generate_tokens(readline):
Expand Down Expand Up @@ -533,12 +531,10 @@ def error(message, filename=None, location=None):
perror("unexpected error: %s" % err)
raise

def _generate_tokens_from_c_tokenizer(source, extra_tokens=False, ignore_unmatched_parens=False):
def _generate_tokens_from_c_tokenizer(source, extra_tokens=False):
"""Tokenize a source reading Python code as unicode strings using the internal C tokenizer"""
import _tokenize as c_tokenizer
for info in c_tokenizer.TokenizerIter(source,
extra_tokens=extra_tokens,
ignore_unmatched_parens=ignore_unmatched_parens):
for info in c_tokenizer.TokenizerIter(source, extra_tokens=extra_tokens):
yield TokenInfo._make(info)


Expand Down
15 changes: 7 additions & 8 deletions 15 Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ tok_new(void)
tok->report_warnings = 1;
tok->tok_extra_tokens = 0;
tok->comment_newline = 0;
tok->ignore_unmatched_parens = 0;
tok->tok_mode_stack[0] = (tokenizer_mode){.kind =TOK_REGULAR_MODE, .f_string_quote='\0', .f_string_quote_size = 0, .f_string_debug=0};
tok->tok_mode_stack_index = 0;
tok->tok_report_warnings = 1;
Expand Down Expand Up @@ -2497,18 +2496,18 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
case ')':
case ']':
case '}':
if (!tok->ignore_unmatched_parens && !tok->level) {
if (INSIDE_FSTRING(tok) && !current_tok->curly_bracket_depth && c == '}') {
return MAKE_TOKEN(syntaxerror(tok, "f-string: single '}' is not allowed"));
}
if (INSIDE_FSTRING(tok) && !current_tok->curly_bracket_depth && c == '}') {
return MAKE_TOKEN(syntaxerror(tok, "f-string: single '}' is not allowed"));
}
if (!tok->tok_extra_tokens && !tok->level) {
return MAKE_TOKEN(syntaxerror(tok, "unmatched '%c'", c));
}
if (tok->level > 0) {
tok->level--;
int opening = tok->parenstack[tok->level];
if (!tok->ignore_unmatched_parens && !((opening == '(' && c == ')') ||
(opening == '[' && c == ']') ||
(opening == '{' && c == '}'))) {
if (!tok->tok_extra_tokens && !((opening == '(' && c == ')') ||
(opening == '[' && c == ']') ||
(opening == '{' && c == '}'))) {
/* If the opening bracket belongs to an f-string's expression
part (e.g. f"{)}") and the closing bracket is an arbitrary
nested expression, then instead of matching a different
Expand Down
1 change: 0 additions & 1 deletion 1 Parser/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ struct tok_state {
int tok_report_warnings;
int tok_extra_tokens;
int comment_newline;
int ignore_unmatched_parens;
#ifdef Py_DEBUG
int debug;
#endif
Expand Down
11 changes: 2 additions & 9 deletions 11 Python/Python-tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ _tokenizer.tokenizeriter.__new__ as tokenizeriter_new
source: str
*
extra_tokens: bool
ignore_unmatched_parens: bool
[clinic start generated code]*/

static PyObject *
tokenizeriter_new_impl(PyTypeObject *type, const char *source,
int extra_tokens, int ignore_unmatched_parens)
/*[clinic end generated code: output=5437e7bbc30de3f4 input=7f6b22d7c235ffd7]*/
int extra_tokens)
/*[clinic end generated code: output=f6f9d8b4beec8106 input=90dc5b6a5df180c2]*/
{
tokenizeriterobject *self = (tokenizeriterobject *)type->tp_alloc(type, 0);
if (self == NULL) {
Expand All @@ -65,12 +64,6 @@ tokenizeriter_new_impl(PyTypeObject *type, const char *source,
if (extra_tokens) {
self->tok->tok_extra_tokens = 1;
}
if (ignore_unmatched_parens) {
self->tok->ignore_unmatched_parens = 1;
}
if (ignore_unmatched_parens) {
self->tok->ignore_unmatched_parens = 1;
}
self->done = 0;
return (PyObject *)self;
}
Expand Down
21 changes: 8 additions & 13 deletions 21 Python/clinic/Python-tokenize.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.