Skip to content
Next Next commit
bpo-39562: Prevent collision of future and compiler flags
  • Loading branch information
isidentical committed Apr 20, 2020
commit 984c815a1468bf81c69b09b107c43304994b14ff
20 changes: 10 additions & 10 deletions Include/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ typedef struct {

/* These are no longer used. */
#if 0
#define CO_GENERATOR_ALLOWED 0x1000
#define CO_GENERATOR_ALLOWED 0x10000
Comment thread
isidentical marked this conversation as resolved.
Outdated
#endif
#define CO_FUTURE_DIVISION 0x2000
#define CO_FUTURE_ABSOLUTE_IMPORT 0x4000 /* do absolute imports by default */
#define CO_FUTURE_WITH_STATEMENT 0x8000
#define CO_FUTURE_PRINT_FUNCTION 0x10000
#define CO_FUTURE_UNICODE_LITERALS 0x20000

#define CO_FUTURE_BARRY_AS_BDFL 0x40000
#define CO_FUTURE_GENERATOR_STOP 0x80000
#define CO_FUTURE_ANNOTATIONS 0x100000
#define CO_FUTURE_DIVISION 0x20000
Comment thread
isidentical marked this conversation as resolved.
Outdated
#define CO_FUTURE_ABSOLUTE_IMPORT 0x40000 /* do absolute imports by default */
#define CO_FUTURE_WITH_STATEMENT 0x80000
#define CO_FUTURE_PRINT_FUNCTION 0x100000
#define CO_FUTURE_UNICODE_LITERALS 0x200000

#define CO_FUTURE_BARRY_AS_BDFL 0x400000
#define CO_FUTURE_GENERATOR_STOP 0x800000
#define CO_FUTURE_ANNOTATIONS 0x1000000

/* This value is found in the co_cell2arg array when the associated cell
variable does not correspond to an argument. */
Expand Down
16 changes: 8 additions & 8 deletions Lib/__future__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@
# this module.
CO_NESTED = 0x0010 # nested_scopes
CO_GENERATOR_ALLOWED = 0 # generators (obsolete, was 0x1000)
CO_FUTURE_DIVISION = 0x2000 # division
CO_FUTURE_ABSOLUTE_IMPORT = 0x4000 # perform absolute imports by default
CO_FUTURE_WITH_STATEMENT = 0x8000 # with statement
CO_FUTURE_PRINT_FUNCTION = 0x10000 # print function
CO_FUTURE_UNICODE_LITERALS = 0x20000 # unicode string literals
CO_FUTURE_BARRY_AS_BDFL = 0x40000
CO_FUTURE_GENERATOR_STOP = 0x80000 # StopIteration becomes RuntimeError in generators
CO_FUTURE_ANNOTATIONS = 0x100000 # annotations become strings at runtime
CO_FUTURE_DIVISION = 0x20000 # division
Comment thread
isidentical marked this conversation as resolved.
Outdated
CO_FUTURE_ABSOLUTE_IMPORT = 0x40000 # perform absolute imports by default
CO_FUTURE_WITH_STATEMENT = 0x80000 # with statement
CO_FUTURE_PRINT_FUNCTION = 0x100000 # print function
CO_FUTURE_UNICODE_LITERALS = 0x200000 # unicode string literals
CO_FUTURE_BARRY_AS_BDFL = 0x400000
CO_FUTURE_GENERATOR_STOP = 0x800000 # StopIteration becomes RuntimeError in generators
CO_FUTURE_ANNOTATIONS = 0x1000000 # annotations become strings at runtime

class _Feature:
def __init__(self, optionalRelease, mandatoryRelease, compiler_flag):
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_future.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Test various flavors of legal and illegal future statements

import __future__
Comment thread
isidentical marked this conversation as resolved.
Outdated
import ast
import unittest
from test import support
from textwrap import dedent
Expand Down Expand Up @@ -75,6 +77,16 @@ def test_badfuture10(self):
from test import badsyntax_future10
self.check_syntax_error(cm.exception, "badsyntax_future10", 3)

def test_ensure_flags_dont_clash(self):
Comment thread
isidentical marked this conversation as resolved.
Outdated
flags = [
getattr(__future__, future).compiler_flag
for future in __future__.all_feature_names
]
flags.extend(
(ast.PyCF_ALLOW_TOP_LEVEL_AWAIT, ast.PyCF_ONLY_AST, ast.PyCF_TYPE_COMMENTS)
Comment thread
isidentical marked this conversation as resolved.
Outdated
)
Comment thread
isidentical marked this conversation as resolved.
Outdated
self.assertCountEqual(set(flags), flags)

def test_parserhack(self):
# test that the parser.c::future_hack function works as expected
# Note: although this test must pass, it's not testing the original
Expand Down