Skip to content

Commit 1d5479b

Browse files
authored
gh-117411: move PyFutureFeatures to pycore_symtable.h and make it private (#117412)
1 parent 5fd1897 commit 1d5479b

File tree

11 files changed

+49
-42
lines changed

11 files changed

+49
-42
lines changed

Include/cpython/compile.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,8 @@ typedef struct {
3232
#define _PyCompilerFlags_INIT \
3333
(PyCompilerFlags){.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION}
3434

35-
/* source location information */
36-
typedef struct {
37-
int lineno;
38-
int end_lineno;
39-
int col_offset;
40-
int end_col_offset;
41-
} _PyCompilerSrcLocation;
42-
43-
#define SRC_LOCATION_FROM_AST(n) \
44-
(_PyCompilerSrcLocation){ \
45-
.lineno = (n)->lineno, \
46-
.end_lineno = (n)->end_lineno, \
47-
.col_offset = (n)->col_offset, \
48-
.end_col_offset = (n)->end_col_offset }
49-
5035
/* Future feature support */
5136

52-
typedef struct {
53-
int ff_features; /* flags set by future statements */
54-
_PyCompilerSrcLocation ff_location; /* location of last future statement */
55-
} PyFutureFeatures;
56-
5737
#define FUTURE_NESTED_SCOPES "nested_scopes"
5838
#define FUTURE_GENERATORS "generators"
5939
#define FUTURE_DIVISION "division"

Include/internal/pycore_compile.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_symtable.h" // _Py_SourceLocation
12+
1113
struct _arena; // Type defined in pycore_pyarena.h
1214
struct _mod; // Type defined in pycore_ast.h
1315

@@ -27,7 +29,7 @@ extern int _PyCompile_AstOptimize(
2729
int optimize,
2830
struct _arena *arena);
2931

30-
static const _PyCompilerSrcLocation NO_LOCATION = {-1, -1, -1, -1};
32+
struct _Py_SourceLocation;
3133

3234
extern int _PyAST_Optimize(
3335
struct _mod *,
@@ -44,7 +46,7 @@ typedef struct {
4446
typedef struct {
4547
int i_opcode;
4648
int i_oparg;
47-
_PyCompilerSrcLocation i_loc;
49+
_Py_SourceLocation i_loc;
4850
_PyCompile_ExceptHandlerInfo i_except_handler_info;
4951

5052
/* Used by the assembler */
@@ -65,7 +67,7 @@ typedef struct {
6567
int _PyCompile_InstructionSequence_UseLabel(_PyCompile_InstructionSequence *seq, int lbl);
6668
int _PyCompile_InstructionSequence_Addop(_PyCompile_InstructionSequence *seq,
6769
int opcode, int oparg,
68-
_PyCompilerSrcLocation loc);
70+
_Py_SourceLocation loc);
6971
int _PyCompile_InstructionSequence_ApplyLabelMap(_PyCompile_InstructionSequence *seq);
7072

7173
typedef struct {

Include/internal/pycore_flowgraph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ typedef struct {
1818
struct _PyCfgBuilder;
1919

2020
int _PyCfgBuilder_UseLabel(struct _PyCfgBuilder *g, _PyCfgJumpTargetLabel lbl);
21-
int _PyCfgBuilder_Addop(struct _PyCfgBuilder *g, int opcode, int oparg, _PyCompilerSrcLocation loc);
21+
int _PyCfgBuilder_Addop(struct _PyCfgBuilder *g, int opcode, int oparg, _Py_SourceLocation loc);
2222

2323
struct _PyCfgBuilder* _PyCfgBuilder_New(void);
2424
void _PyCfgBuilder_Free(struct _PyCfgBuilder *g);

Include/internal/pycore_symtable.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ typedef enum _comprehension_type {
2929
SetComprehension = 3,
3030
GeneratorExpression = 4 } _Py_comprehension_ty;
3131

32+
/* source location information */
33+
typedef struct {
34+
int lineno;
35+
int end_lineno;
36+
int col_offset;
37+
int end_col_offset;
38+
} _Py_SourceLocation;
39+
40+
#define SRC_LOCATION_FROM_AST(n) \
41+
(_Py_SourceLocation){ \
42+
.lineno = (n)->lineno, \
43+
.end_lineno = (n)->end_lineno, \
44+
.col_offset = (n)->col_offset, \
45+
.end_col_offset = (n)->end_col_offset }
46+
47+
static const _Py_SourceLocation NO_LOCATION = {-1, -1, -1, -1};
48+
49+
/* __future__ information */
50+
typedef struct {
51+
int ff_features; /* flags set by future statements */
52+
_Py_SourceLocation ff_location; /* location of last future statement */
53+
} _PyFutureFeatures;
54+
3255
struct _symtable_entry;
3356

3457
struct symtable {
@@ -44,7 +67,7 @@ struct symtable {
4467
consistency with the corresponding
4568
compiler structure */
4669
PyObject *st_private; /* name of current class or NULL */
47-
PyFutureFeatures *st_future; /* module's future features that affect
70+
_PyFutureFeatures *st_future; /* module's future features that affect
4871
the symbol table */
4972
int recursion_depth; /* current recursion depth */
5073
int recursion_limit; /* recursion limit */
@@ -100,7 +123,7 @@ extern int _PyST_IsFunctionLike(PySTEntryObject *);
100123
extern struct symtable* _PySymtable_Build(
101124
struct _mod *mod,
102125
PyObject *filename,
103-
PyFutureFeatures *future);
126+
_PyFutureFeatures *future);
104127
extern PySTEntryObject* _PySymtable_Lookup(struct symtable *, void *);
105128

106129
extern void _PySymtable_Free(struct symtable *);
@@ -150,7 +173,7 @@ extern struct symtable* _Py_SymtableStringObjectFlags(
150173
int _PyFuture_FromAST(
151174
struct _mod * mod,
152175
PyObject *filename,
153-
PyFutureFeatures* futures);
176+
_PyFutureFeatures* futures);
154177

155178
#ifdef __cplusplus
156179
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Move ``PyFutureFeatures`` to an internal header and make it private.

Python/assemble.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "pycore_compile.h"
66
#include "pycore_opcode_utils.h" // IS_BACKWARDS_JUMP_OPCODE
77
#include "pycore_opcode_metadata.h" // is_pseudo_target, _PyOpcode_Caches
8+
#include "pycore_symtable.h" // _Py_SourceLocation
89

910

1011
#define DEFAULT_CODE_SIZE 128
@@ -21,7 +22,7 @@
2122
return ERROR; \
2223
}
2324

24-
typedef _PyCompilerSrcLocation location;
25+
typedef _Py_SourceLocation location;
2526
typedef _PyCompile_Instruction instruction;
2627
typedef _PyCompile_InstructionSequence instr_sequence;
2728

Python/compile.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@
7070
((C)->c_flags.cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
7171
&& ((C)->u->u_ste->ste_type == ModuleBlock))
7272

73-
typedef _PyCompilerSrcLocation location;
73+
typedef _Py_SourceLocation location;
7474
typedef struct _PyCfgBuilder cfg_builder;
7575

7676
#define LOCATION(LNO, END_LNO, COL, END_COL) \
77-
((const _PyCompilerSrcLocation){(LNO), (END_LNO), (COL), (END_COL)})
77+
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
7878

7979
/* Return true if loc1 starts after loc2 ends. */
8080
static inline bool
@@ -408,7 +408,7 @@ handled by the symbol analysis pass.
408408
struct compiler {
409409
PyObject *c_filename;
410410
struct symtable *c_st;
411-
PyFutureFeatures c_future; /* module's __future__ */
411+
_PyFutureFeatures c_future; /* module's __future__ */
412412
PyCompilerFlags c_flags;
413413

414414
int c_optimize; /* optimization level */
@@ -585,7 +585,7 @@ int
585585
_PyCompile_AstOptimize(mod_ty mod, PyObject *filename, PyCompilerFlags *cf,
586586
int optimize, PyArena *arena)
587587
{
588-
PyFutureFeatures future;
588+
_PyFutureFeatures future;
589589
if (!_PyFuture_FromAST(mod, filename, &future)) {
590590
return -1;
591591
}

Python/flowgraph.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
#define DEFAULT_BLOCK_SIZE 16
2424

25-
typedef _PyCompilerSrcLocation location;
25+
typedef _Py_SourceLocation location;
2626
typedef _PyCfgJumpTargetLabel jump_target_label;
2727

2828
typedef struct _PyCfgInstruction {
2929
int i_opcode;
3030
int i_oparg;
31-
_PyCompilerSrcLocation i_loc;
31+
_Py_SourceLocation i_loc;
3232
struct _PyCfgBasicblock *i_target; /* target block (if jump instruction) */
3333
struct _PyCfgBasicblock *i_except; /* target block when exception is raised */
3434
} cfg_instr;
@@ -92,7 +92,7 @@ static const jump_target_label NO_LABEL = {-1};
9292
#define IS_LABEL(L) (!SAME_LABEL((L), (NO_LABEL)))
9393

9494
#define LOCATION(LNO, END_LNO, COL, END_COL) \
95-
((const _PyCompilerSrcLocation){(LNO), (END_LNO), (COL), (END_COL)})
95+
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
9696

9797
static inline int
9898
is_block_push(cfg_instr *i)

Python/future.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "Python.h"
22
#include "pycore_ast.h" // _PyAST_GetDocString()
3+
#include "pycore_symtable.h" // _PyFutureFeatures
34
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
45

56
#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
67

78
static int
8-
future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
9+
future_check_features(_PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
910
{
1011
int i;
1112

@@ -53,7 +54,7 @@ future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
5354
}
5455

5556
static int
56-
future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
57+
future_parse(_PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
5758
{
5859
if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) {
5960
return 1;
@@ -98,10 +99,10 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
9899

99100

100101
int
101-
_PyFuture_FromAST(mod_ty mod, PyObject *filename, PyFutureFeatures *ff)
102+
_PyFuture_FromAST(mod_ty mod, PyObject *filename, _PyFutureFeatures *ff)
102103
{
103104
ff->ff_features = 0;
104-
ff->ff_location = (_PyCompilerSrcLocation){-1, -1, -1, -1};
105+
ff->ff_location = (_Py_SourceLocation){-1, -1, -1, -1};
105106

106107
if (!future_parse(ff, mod, filename)) {
107108
return 0;

Python/symtable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ symtable_new(void)
387387
}
388388

389389
struct symtable *
390-
_PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
390+
_PySymtable_Build(mod_ty mod, PyObject *filename, _PyFutureFeatures *future)
391391
{
392392
struct symtable *st = symtable_new();
393393
asdl_stmt_seq *seq;
@@ -2757,7 +2757,7 @@ _Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
27572757
_PyArena_Free(arena);
27582758
return NULL;
27592759
}
2760-
PyFutureFeatures future;
2760+
_PyFutureFeatures future;
27612761
if (!_PyFuture_FromAST(mod, filename, &future)) {
27622762
_PyArena_Free(arena);
27632763
return NULL;

Python/traceback.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include "pycore_ast.h" // asdl_seq_GET()
77
#include "pycore_call.h" // _PyObject_CallMethodFormat()
8-
#include "pycore_compile.h" // _PyAST_Optimize()
98
#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
109
#include "pycore_frame.h" // _PyFrame_GetCode()
1110
#include "pycore_interp.h" // PyInterpreterState.gc

0 commit comments

Comments
 (0)