Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
python-cpython/Python/future.c at 2.3 · sthagen/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 }}
sthagen
/
python-cpython
Public
forked from
python/cpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
9
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Security and quality
Insights
Files
Expand file tree
2.3
Breadcrumbs
python-cpython
/
Python
/
future.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
260 lines (231 loc) · 5.96 KB
2.3
Breadcrumbs
python-cpython
/
Python
/
future.c
Copy path
Top
File metadata and controls
Code
Blame
260 lines (231 loc) · 5.96 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
#include
"Python.h"
#include
"node.h"
#include
"token.h"
#include
"graminit.h"
#include
"compile.h"
#include
"symtable.h"
#define
UNDEFINED_FUTURE_FEATURE
"future feature %.100s is not defined"
#define
FUTURE_IMPORT_STAR
"future statement does not support import *"
/* FUTURE_POSSIBLE() is provided to accomodate doc strings, which is
the only statement that can occur before a future statement.
*/
#define
FUTURE_POSSIBLE
(
FF
) ((FF)->ff_last_lineno == -1)
static
int
future_check_features
(
PyFutureFeatures
*
ff
,
node
*
n
,
const
char
*
filename
)
{
int
i
;
char
*
feature
;
node
*
ch
;
REQ
(
n
,
import_stmt
);
/* must by from __future__ import ... */
for
(
i
=
3
;
i
<
NCH
(
n
);
i
+=
2
) {
ch
=
CHILD
(
n
,
i
);
if
(
TYPE
(
ch
)
==
STAR
) {
PyErr_SetString
(
PyExc_SyntaxError
,
FUTURE_IMPORT_STAR
);
PyErr_SyntaxLocation
(
filename
,
ch
->
n_lineno
);
return
-1
;
}
REQ
(
ch
,
import_as_name
);
feature
=
STR
(
CHILD
(
ch
,
0
));
if
(
strcmp
(
feature
,
FUTURE_NESTED_SCOPES
)
==
0
) {
continue
;
}
else
if
(
strcmp
(
feature
,
FUTURE_GENERATORS
)
==
0
) {
continue
;
}
else
if
(
strcmp
(
feature
,
FUTURE_DIVISION
)
==
0
) {
ff
->
ff_features
|=
CO_FUTURE_DIVISION
;
}
else
if
(
strcmp
(
feature
,
"braces"
)
==
0
) {
PyErr_SetString
(
PyExc_SyntaxError
,
"not a chance"
);
PyErr_SyntaxLocation
(
filename
,
CHILD
(
ch
,
0
)
->
n_lineno
);
return
-1
;
}
else
{
PyErr_Format
(
PyExc_SyntaxError
,
UNDEFINED_FUTURE_FEATURE
,
feature
);
PyErr_SyntaxLocation
(
filename
,
CHILD
(
ch
,
0
)
->
n_lineno
);
return
-1
;
}
}
return
0
;
}
static
void
future_error
(
node
*
n
,
const
char
*
filename
)
{
PyErr_SetString
(
PyExc_SyntaxError
,
"from __future__ imports must occur at the "
"beginning of the file"
);
PyErr_SyntaxLocation
(
filename
,
n
->
n_lineno
);
}
/* Relevant portions of the grammar:
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
file_input: (NEWLINE | stmt)* ENDMARKER
stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
| import_stmt | global_stmt | exec_stmt | assert_stmt
import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
| 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
import_as_name: NAME [NAME NAME]
dotted_as_name: dotted_name [NAME NAME]
dotted_name: NAME ('.' NAME)*
*/
/* future_parse() finds future statements at the beginnning of a
module. The function calls itself recursively, rather than
factoring out logic for different kinds of statements into
different routines.
Return values:
-1 indicates an error occurred, e.g. unknown feature name
0 indicates no feature was found
1 indicates a feature was found
*/
static
int
future_parse
(
PyFutureFeatures
*
ff
,
node
*
n
,
const
char
*
filename
)
{
int
i
,
r
;
loop
:
switch
(
TYPE
(
n
)) {
case
single_input
:
if
(
TYPE
(
CHILD
(
n
,
0
))
==
simple_stmt
) {
n
=
CHILD
(
n
,
0
);
goto
loop
;
}
return
0
;
case
file_input
:
/* Check each statement in the file, starting with the
first, and continuing until the first statement
that isn't a future statement.
*/
for
(
i
=
0
;
i
<
NCH
(
n
);
i
++
) {
node
*
ch
=
CHILD
(
n
,
i
);
if
(
TYPE
(
ch
)
==
stmt
) {
r
=
future_parse
(
ff
,
ch
,
filename
);
/* Need to check both conditions below
to accomodate doc strings, which
causes r < 0.
*/
if
(
r
<
1
&&
!
FUTURE_POSSIBLE
(
ff
))
return
r
;
}
}
return
0
;
case
simple_stmt
:
if
(
NCH
(
n
)
==
2
) {
REQ
(
CHILD
(
n
,
0
),
small_stmt
);
n
=
CHILD
(
n
,
0
);
goto
loop
;
}
else
{
/* Deal with the special case of a series of
small statements on a single line. If a
future statement follows some other
statement, the SyntaxError is raised here.
In all other cases, the symtable pass
raises the exception.
*/
int
found
=
0
,
end_of_future
=
0
;
for
(
i
=
0
;
i
<
NCH
(
n
);
i
+=
2
) {
if
(
TYPE
(
CHILD
(
n
,
i
))
==
small_stmt
) {
r
=
future_parse
(
ff
,
CHILD
(
n
,
i
),
filename
);
if
(
r
<
1
)
end_of_future
=
1
;
else
{
found
=
1
;
if
(
end_of_future
) {
future_error
(
n
,
filename
);
return
-1
;
}
}
}
}
/* If we found one and only one, then the
current lineno is legal.
*/
if
(
found
)
ff
->
ff_last_lineno
=
n
->
n_lineno
+
1
;
else
ff
->
ff_last_lineno
=
n
->
n_lineno
;
if
(
end_of_future
&&
found
)
return
1
;
else
return
0
;
}
case
stmt
:
if
(
TYPE
(
CHILD
(
n
,
0
))
==
simple_stmt
) {
n
=
CHILD
(
n
,
0
);
goto
loop
;
}
else
if
(
TYPE
(
CHILD
(
n
,
0
))
==
expr_stmt
) {
n
=
CHILD
(
n
,
0
);
goto
loop
;
}
else
{
REQ
(
CHILD
(
n
,
0
),
compound_stmt
);
ff
->
ff_last_lineno
=
n
->
n_lineno
;
return
0
;
}
case
small_stmt
:
n
=
CHILD
(
n
,
0
);
goto
loop
;
case
import_stmt
: {
node
*
name
;
if
(
STR
(
CHILD
(
n
,
0
))[
0
]
!=
'f'
) {
/* from */
ff
->
ff_last_lineno
=
n
->
n_lineno
;
return
0
;
}
name
=
CHILD
(
n
,
1
);
if
(
strcmp
(
STR
(
CHILD
(
name
,
0
)),
"__future__"
)
!=
0
)
return
0
;
if
(
future_check_features
(
ff
,
n
,
filename
)
<
0
)
return
-1
;
ff
->
ff_last_lineno
=
n
->
n_lineno
+
1
;
return
1
;
}
/* The cases below -- all of them! -- are necessary to find
and skip doc strings. */
case
expr_stmt
:
case
testlist
:
case
test
:
case
and_test
:
case
not_test
:
case
comparison
:
case
expr
:
case
xor_expr
:
case
and_expr
:
case
shift_expr
:
case
arith_expr
:
case
term
:
case
factor
:
case
power
:
if
(
NCH
(
n
)
==
1
) {
n
=
CHILD
(
n
,
0
);
goto
loop
;
}
break
;
case
atom
:
if
(
TYPE
(
CHILD
(
n
,
0
))
==
STRING
&&
ff
->
ff_found_docstring
==
0
) {
ff
->
ff_found_docstring
=
1
;
return
0
;
}
ff
->
ff_last_lineno
=
n
->
n_lineno
;
return
0
;
default
:
ff
->
ff_last_lineno
=
n
->
n_lineno
;
return
0
;
}
return
0
;
}
PyFutureFeatures
*
PyNode_Future
(
node
*
n
,
const
char
*
filename
)
{
PyFutureFeatures
*
ff
;
ff
=
(
PyFutureFeatures
*
)
PyMem_Malloc
(
sizeof
(
PyFutureFeatures
));
if
(
ff
==
NULL
)
return
NULL
;
ff
->
ff_found_docstring
=
0
;
ff
->
ff_last_lineno
=
-1
;
ff
->
ff_features
=
0
;
if
(
future_parse
(
ff
,
n
,
filename
)
<
0
) {
PyMem_Free
((
void
*
)
ff
);
return
NULL
;
}
return
ff
;
}
You can’t perform that action at this time.