[ Web Proxy ]
URL:
Viewing:
https://github-redirect.dependabot.com/python/cpython/blob/main/Python/Python-tokenize.c
[Back]
[Original]
cpython/Python/Python-tokenize.c at main python/cpython GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.7k
(2.7k)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
cpython
/
Python
/
Python-tokenize.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
449 lines (399 loc) 12.8 KB
main
Breadcrumbs
cpython
/
Python
/
Python-tokenize.c
Copy path
Top
File metadata and controls
Code
Blame
449 lines (399 loc) 12.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include
"Python.h"
#include
"errcode.h"
#include
"internal/pycore_critical_section.h"
// Py_BEGIN_CRITICAL_SECTION
#include
"../Parser/tokenizer/tokenizer.h"
#include
"../Parser/pegen.h"
// _PyPegen_byte_offset_to_character_offset()
static
struct
PyModuleDef
_tokenizemodule
;
typedef
struct
{
PyTypeObject
*
TokenizerIter
;
}
tokenize_state
;
static
tokenize_state
*
get_tokenize_state
(
PyObject
*
module
) {
return
(
tokenize_state
*
)
PyModule_GetState
(
module
);
}
#define
_tokenize_get_state_by_type
(
type
) \
get_tokenize_state(PyType_GetModuleByDef(type, &_tokenizemodule))
#include
"pycore_runtime.h"
#include
"clinic/Python-tokenize.c.h"
/*[clinic input]
module _tokenizer
class _tokenizer.tokenizeriter "tokenizeriterobject *" "_tokenize_get_state_by_type(type)->TokenizerIter"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=96d98ee2fef7a8bc]*/
typedef
struct
{
PyObject_HEAD
struct
tok_state
*
tok
;
int
done
;
int
extra_tokens
;
/* Needed to cache line for performance */
PyObject
*
last_line
;
Py_ssize_t
last_lineno
;
Py_ssize_t
byte_col_offset_diff
;
}
tokenizeriterobject
;
/*[clinic input]
@classmethod
_tokenizer.tokenizeriter.__new__ as tokenizeriter_new
readline: object
/
*
extra_tokens: bool
encoding: str(c_default="NULL") = 'utf-8'
[clinic start generated code]*/
static
PyObject
*
tokenizeriter_new_impl
(
PyTypeObject
*
type
,
PyObject
*
readline
,
int
extra_tokens
,
const
char
*
encoding
)
/*[clinic end generated code: output=7501a1211683ce16 input=f7dddf8a613ae8bd]*/
{
struct
tok_state
*
tok
=
_PyTokenizer_FromReadline
(
readline
,
encoding
);
if
(
tok
==
NULL
) {
return
NULL
;
}
_Py_DECLARE_STR
(
anon_string
,
"<string>"
);
_PyTokenizer_SetContext
(
tok
,
&
_Py_STR
(
anon_string
),
NULL
);
tokenizeriterobject
*
self
=
(
tokenizeriterobject
*
)
type
->
tp_alloc
(
type
,
0
);
if
(
self
==
NULL
) {
_PyTokenizer_Free
(
tok
);
return
NULL
;
}
self
->
tok
=
tok
;
_PyTokenizer_SetOptions
(
self
->
tok
,
extra_tokens
,
0
);
self
->
extra_tokens
=
extra_tokens
;
self
->
done
=
0
;
self
->
last_line
=
NULL
;
self
->
byte_col_offset_diff
=
0
;
self
->
last_lineno
=
0
;
return
(
PyObject
*
)
self
;
}
static
void
_tokenizer_error
(
tokenizeriterobject
*
it
)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED
(
it
);
assert
(!
PyErr_Occurred
());
const
char
*
msg
=
NULL
;
PyObject
*
errtype
=
PyExc_SyntaxError
;
struct
tok_state
*
tok
=
it
->
tok
;
_PyTokenizer_Info
info
=
_PyTokenizer_GetInfo
(
tok
);
switch
(
info
.
status
) {
case
E_TOKEN
:
msg
=
"invalid token"
;
break
;
case
E_EOF
:
PyErr_SetString
(
PyExc_SyntaxError
,
"unexpected EOF in multi-line statement"
);
PyErr_SyntaxLocationObject
(
info
.
filename
,
info
.
location
.
lineno
,
(
int
)
Py_MAX
(
0
,
info
.
input_span
.
end
-
info
.
input_span
.
start
));
return
;
case
E_DEDENT
:
msg
=
"unindent does not match any outer indentation level"
;
errtype
=
PyExc_IndentationError
;
break
;
case
E_INTR
:
PyErr_SetNone
(
PyExc_KeyboardInterrupt
);
return
;
case
E_NOMEM
:
PyErr_NoMemory
();
return
;
case
E_TABSPACE
:
errtype
=
PyExc_TabError
;
msg
=
"inconsistent use of tabs and spaces in indentation"
;
break
;
case
E_TOODEEP
:
errtype
=
PyExc_IndentationError
;
msg
=
"too many levels of indentation"
;
break
;
case
E_LINECONT
: {
msg
=
"unexpected character after line continuation character"
;
break
;
}
default
:
msg
=
"unknown tokenization error"
;
}
PyObject
*
error_line
=
NULL
;
PyObject
*
value
=
NULL
;
Py_ssize_t
input_size
;
const
char
*
input
=
_PyTokenizer_SpanView
(
tok
,
info
.
input_span
,
&
input_size
);
Py_ssize_t
size
=
input_size
;
assert
(
input
[
size
-
1
]
==
'\n'
);
size
-=
1
;
// Remove the newline character from the end of the line
error_line
=
PyUnicode_DecodeUTF8
(
input
,
size
,
"replace"
);
if
(!
error_line
) {
goto
exit
;
}
Py_ssize_t
offset
=
_PyPegen_byte_offset_to_character_offset
(
error_line
,
input_size
);
if
(
offset
==
-1
) {
goto
exit
;
}
value
=
Py_BuildValue
(
"(s(OinOOO))"
,
msg
,
info
.
filename
,
info
.
location
.
lineno
,
offset
,
error_line
,
Py_None
,
Py_None
);
if
(!
value
) {
goto
exit
;
}
PyErr_SetObject
(
errtype
,
value
);
exit
:
Py_XDECREF
(
error_line
);
Py_XDECREF
(
value
);
}
static
PyObject
*
_get_current_line
(
tokenizeriterobject
*
it
,
int
current_lineno
,
const
char
*
line_start
,
Py_ssize_t
size
,
int
*
line_changed
)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED
(
it
);
if
(
current_lineno
!=
it
->
last_lineno
) {
// Line has changed since last token, so we fetch the new line and cache it
// in the iter object.
Py_XDECREF
(
it
->
last_line
);
it
->
last_line
=
PyUnicode_DecodeUTF8
(
line_start
,
size
,
"replace"
);
it
->
byte_col_offset_diff
=
0
;
}
else
{
*
line_changed
=
0
;
}
return
it
->
last_line
;
}
static
int
_get_col_offsets
(
tokenizeriterobject
*
it
,
const
struct
token
*
token
,
const
_PyToken_View
*
view
,
PyObject
*
line
,
int
line_changed
,
Py_ssize_t
*
col_offset
,
Py_ssize_t
*
end_col_offset
)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED
(
it
);
const
char
*
token_start
=
view
->
text
;
const
char
*
token_end
=
token_start
==
NULL
?
NULL
:
token_start
+
view
->
length
;
Py_ssize_t
lineno
=
token
->
start_loc
.
lineno
;
Py_ssize_t
end_lineno
=
token
->
end_loc
.
lineno
;
Py_ssize_t
byte_offset
=
-1
;
if
(
token_start
!=
NULL
&&
token_start
>=
view
->
line
) {
byte_offset
=
token_start
-
view
->
line
;
if
(
line_changed
) {
*
col_offset
=
_PyPegen_byte_offset_to_character_offset_line
(
line
,
0
,
byte_offset
);
if
(
*
col_offset
<
0
) {
return
-1
;
}
it
->
byte_col_offset_diff
=
byte_offset
-
*
col_offset
;
}
else
{
*
col_offset
=
byte_offset
-
it
->
byte_col_offset_diff
;
}
}
if
(
token_end
!=
NULL
&&
token_end
>=
view
->
end_line
) {
Py_ssize_t
end_byte_offset
=
token_end
-
view
->
end_line
;
if
(
lineno
==
end_lineno
) {
// Avoid rescanning the prefix of a very long line.
Py_ssize_t
token_col_offset
=
_PyPegen_byte_offset_to_character_offset_line
(
line
,
byte_offset
,
end_byte_offset
);
if
(
token_col_offset
<
0
) {
return
-1
;
}
*
end_col_offset
=
*
col_offset
+
token_col_offset
;
it
->
byte_col_offset_diff
+=
token_end
-
token_start
-
token_col_offset
;
}
else
{
*
end_col_offset
=
_PyPegen_byte_offset_to_character_offset_line
(
line
,
view
->
end_line
-
view
->
line
,
token_end
-
view
->
line
);
if
(
*
end_col_offset
<
0
) {
return
-1
;
}
it
->
byte_col_offset_diff
+=
end_byte_offset
-
*
end_col_offset
;
}
}
it
->
last_lineno
=
lineno
;
return
0
;
}
static
PyObject
*
tokenizeriter_next
(
PyObject
*
op
)
{
tokenizeriterobject
*
it
=
(
tokenizeriterobject
*
)
op
;
PyObject
*
result
=
NULL
;
Py_BEGIN_CRITICAL_SECTION
(
it
);
if
(
it
->
done
) {
PyErr_SetString
(
PyExc_StopIteration
,
"EOF"
);
goto
unlock
;
}
struct
token
token
;
_PyToken_Init
(
&
token
);
_PyTokenizer_Get
(
it
->
tok
,
&
token
);
int
type
=
token
.
type
;
if
(
type
==
ERRORTOKEN
) {
if
(!
PyErr_Occurred
()) {
_tokenizer_error
(
it
);
assert
(
PyErr_Occurred
());
}
goto
exit
;
}
_PyToken_View
view
;
_PyToken_GetView
(
it
->
tok
,
&
token
,
&
view
);
const
char
*
token_start
=
view
.
text
;
PyObject
*
str
;
if
(
token
.
span
.
start
<
0
) {
assert
(
token
.
span
.
start
==
-1
&&
token
.
span
.
end
==
-1
);
str
=
Py_GetConstant
(
Py_CONSTANT_EMPTY_STR
);
}
else
{
str
=
PyUnicode_FromStringAndSize
(
token_start
,
view
.
length
);
}
if
(
str
==
NULL
) {
goto
exit
;
}
int
is_trailing_token
=
type
==
ENDMARKER
||
(
type
==
DEDENT
&&
view
.
at_eof
);
PyObject
*
line
=
NULL
;
int
line_changed
=
1
;
if
(
it
->
extra_tokens
&&
is_trailing_token
) {
line
=
Py_GetConstant
(
Py_CONSTANT_EMPTY_STR
);
}
else
{
Py_ssize_t
size
=
view
.
line_length
;
if
(
size
>=
1
&&
view
.
implicit_newline
) {
size
-=
1
;
}
line
=
_get_current_line
(
it
,
token
.
end_loc
.
lineno
,
view
.
line
,
size
,
&
line_changed
);
}
if
(
line
==
NULL
) {
Py_DECREF
(
str
);
goto
exit
;
}
Py_ssize_t
lineno
=
token
.
start_loc
.
lineno
;
Py_ssize_t
end_lineno
=
token
.
end_loc
.
lineno
;
Py_ssize_t
col_offset
=
-1
;
Py_ssize_t
end_col_offset
=
-1
;
if
(
_get_col_offsets
(
it
,
&
token
,
&
view
,
line
,
line_changed
,
&
col_offset
,
&
end_col_offset
)
<
0
) {
Py_DECREF
(
str
);
goto
exit
;
}
if
(
it
->
extra_tokens
) {
if
(
is_trailing_token
) {
lineno
=
end_lineno
=
lineno
+
1
;
col_offset
=
end_col_offset
=
0
;
}
// Necessary adjustments to match the original Python tokenize
// implementation
if
(
type
>
DEDENT
&&
type
<
OP
) {
type
=
OP
;
}
else
if
(
type
==
NEWLINE
) {
if
(!
view
.
implicit_newline
) {
Py_DECREF
(
str
);
assert
(
token_start
!=
NULL
);
if
(
token_start
[
0
]
==
'\r'
) {
str
=
PyUnicode_FromString
(
"\r\n"
);
}
else
{
str
=
PyUnicode_FromString
(
"\n"
);
}
}
end_col_offset
++
;
}
else
if
(
type
==
NL
) {
if
(
view
.
implicit_newline
) {
Py_DECREF
(
str
);
str
=
Py_GetConstant
(
Py_CONSTANT_EMPTY_STR
);
}
}
if
(
str
==
NULL
) {
goto
exit
;
}
}
result
=
Py_BuildValue
(
"(iN(nn)(nn)O)"
,
type
,
str
,
lineno
,
col_offset
,
end_lineno
,
end_col_offset
,
line
);
exit
:
_PyToken_Free
(
&
token
);
if
(
type
==
ENDMARKER
) {
it
->
done
=
1
;
}
unlock
:;
Py_END_CRITICAL_SECTION
();
return
result
;
}
static
void
tokenizeriter_dealloc
(
PyObject
*
op
)
{
tokenizeriterobject
*
it
=
(
tokenizeriterobject
*
)
op
;
PyTypeObject
*
tp
=
Py_TYPE
(
it
);
PyObject_GC_UnTrack
(
it
);
Py_XDECREF
(
it
->
last_line
);
_PyTokenizer_Free
(
it
->
tok
);
tp
->
tp_free
(
it
);
Py_DECREF
(
tp
);
}
static
int
tokenizeriter_traverse
(
PyObject
*
op
,
visitproc
visit
,
void
*
arg
)
{
tokenizeriterobject
*
it
=
(
tokenizeriterobject
*
)
op
;
Py_VISIT
(
Py_TYPE
(
it
));
Py_VISIT
(
it
->
last_line
);
return
_PyTokenizer_Traverse
(
it
->
tok
,
visit
,
arg
);
}
static
PyType_Slot
tokenizeriter_slots
[]
=
{
{
Py_tp_new
,
tokenizeriter_new
},
{
Py_tp_dealloc
,
tokenizeriter_dealloc
},
{
Py_tp_traverse
,
tokenizeriter_traverse
},
{
Py_tp_getattro
,
PyObject_GenericGetAttr
},
{
Py_tp_iter
,
PyObject_SelfIter
},
{
Py_tp_iternext
,
tokenizeriter_next
},
{
0
,
NULL
},
};
static
PyType_Spec
tokenizeriter_spec
=
{
.
name
=
"_tokenize.TokenizerIter"
,
.
basicsize
=
sizeof
(
tokenizeriterobject
),
.
flags
=
(
Py_TPFLAGS_DEFAULT
|
Py_TPFLAGS_IMMUTABLETYPE
|
Py_TPFLAGS_HAVE_GC
),
.
slots
=
tokenizeriter_slots
,
};
static
int
tokenizemodule_exec
(
PyObject
*
m
)
{
tokenize_state
*
state
=
get_tokenize_state
(
m
);
if
(
state
==
NULL
) {
return
-1
;
}
state
->
TokenizerIter
=
(
PyTypeObject
*
)
PyType_FromModuleAndSpec
(
m
,
&
tokenizeriter_spec
,
NULL
);
if
(
state
->
TokenizerIter
==
NULL
) {
return
-1
;
}
if
(
PyModule_AddType
(
m
,
state
->
TokenizerIter
)
<
0
) {
return
-1
;
}
return
0
;
}
static
PyMethodDef
tokenize_methods
[]
=
{
{
NULL
,
NULL
,
0
,
NULL
}
/* Sentinel */
};
static
PyModuleDef_Slot
tokenizemodule_slots
[]
=
{
_Py_ABI_SLOT
,
{
Py_mod_exec
,
tokenizemodule_exec
},
{
Py_mod_multiple_interpreters
,
Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
},
{
Py_mod_gil
,
Py_MOD_GIL_NOT_USED
},
{
0
,
NULL
}
};
static
int
tokenizemodule_traverse
(
PyObject
*
m
,
visitproc
visit
,
void
*
arg
)
{
tokenize_state
*
state
=
get_tokenize_state
(
m
);
Py_VISIT
(
state
->
TokenizerIter
);
return
0
;
}
static
int
tokenizemodule_clear
(
PyObject
*
m
)
{
tokenize_state
*
state
=
get_tokenize_state
(
m
);
Py_CLEAR
(
state
->
TokenizerIter
);
return
0
;
}
static
void
tokenizemodule_free
(
void
*
m
)
{
tokenizemodule_clear
((
PyObject
*
)
m
);
}
static
struct
PyModuleDef
_tokenizemodule
=
{
PyModuleDef_HEAD_INIT
,
.
m_name
=
"_tokenize"
,
.
m_size
=
sizeof
(
tokenize_state
),
.
m_slots
=
tokenizemodule_slots
,
.
m_methods
=
tokenize_methods
,
.
m_traverse
=
tokenizemodule_traverse
,
.
m_clear
=
tokenizemodule_clear
,
.
m_free
=
tokenizemodule_free
,
};
PyMODINIT_FUNC
PyInit__tokenize
(
void
)
{
return
PyModuleDef_Init
(
&
_tokenizemodule
);
}
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
Manage cookies
Do not share my personal information
You cant perform that action at this time.
Web Proxy Viewer |
New URL
|
Original Page