Skip to content

Commit 900dd6a

Browse files
committed
gh-120317: Lock around tokenizer calls in the tokenizer module
1 parent c3b6dbf commit 900dd6a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Python/Python-tokenize.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Python.h"
22
#include "errcode.h"
3+
#include "internal/pycore_lock.h" // PyMutex
34
#include "../Parser/lexer/state.h"
45
#include "../Parser/lexer/lexer.h"
56
#include "../Parser/tokenizer/tokenizer.h"
@@ -37,6 +38,10 @@ typedef struct
3738
PyObject *last_line;
3839
Py_ssize_t last_lineno;
3940
Py_ssize_t byte_col_offset_diff;
41+
42+
#ifdef Py_GIL_DISABLED
43+
PyMutex mutex;
44+
#endif
4045
} tokenizeriterobject;
4146

4247
/*[clinic input]
@@ -74,6 +79,10 @@ tokenizeriter_new_impl(PyTypeObject *type, PyObject *readline,
7479
}
7580
self->done = 0;
7681

82+
#ifdef Py_GIL_DISABLED
83+
self->mutex = (PyMutex) {_Py_UNLOCKED};
84+
#endif
85+
7786
self->last_line = NULL;
7887
self->byte_col_offset_diff = 0;
7988
self->last_lineno = 0;
@@ -182,7 +191,14 @@ tokenizeriter_next(tokenizeriterobject *it)
182191
struct token token;
183192
_PyToken_Init(&token);
184193

194+
#ifdef Py_GIL_DISABLED
195+
PyMutex_Lock(&it->mutex);
196+
#endif
185197
int type = _PyTokenizer_Get(it->tok, &token);
198+
#ifdef Py_GIL_DISABLED
199+
PyMutex_Unlock(&it->mutex);
200+
#endif
201+
186202
if (type == ERRORTOKEN) {
187203
if(!PyErr_Occurred()) {
188204
_tokenizer_error(it->tok);

0 commit comments

Comments
 (0)