8000 gh-120317: Lock around global state in the tokenize module by lysnikolaou · Pull Request #120318 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-120317: Lock around global state in the tokenize module #120318

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 13 commits into from
Jul 16, 2024
Merged
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
Next Next commit
gh-120317: Lock around tokenizer calls in the tokenizer module
  • Loading branch information
lysnikolaou committed Jun 10, 2024
commit 900dd6a205ec91deadb5b6ab6fc457da6200e29f
16 changes: 16 additions & 0 deletions Python/Python-tokenize.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Python.h"
#include "errcode.h"
#include "internal/pycore_lock.h" // PyMutex
#include "../Parser/lexer/state.h"
#include "../Parser/lexer/lexer.h"
#include "../Parser/tokenizer/tokenizer.h"
Expand Down Expand Up @@ -37,6 +38,10 @@ typedef struct
PyObject *last_line;
Py_ssize_t last_lineno;
Py_ssize_t byte_col_offset_diff;

#ifdef Py_GIL_DISABLED
PyMutex mutex;
#endif
} tokenizeriterobject;

/*[clinic input]
Expand Down Expand Up @@ -74,6 +79,10 @@ tokenizeriter_new_impl(PyTypeObject *type, PyObject *readline,
}
self->done = 0;

#ifdef Py_GIL_DISABLED
self->mutex = (PyMutex) {_Py_UNLOCKED};
#endif

self->last_line = NULL;
self->byte_col_offset_diff = 0;
self->last_lineno = 0;
Expand Down Expand Up @@ -182,7 +191,14 @@ tokenizeriter_next(tokenizeriterobject *it)
struct token token;
_PyToken_Init(&token);

#ifdef Py_GIL_DISABLED
PyMutex_Lock(&it->mutex);
#endif
int type = _PyTokenizer_Get(it->tok, &token);
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&it->mutex);
#endif

if (type == ERRORTOKEN) {
if(!PyErr_Occurred()) {
_tokenizer_error(it->tok);
Expand Down
0