Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de642bd

Browse files
committed
A self-contained piece of Michael Hudson's patch
#449043 supporting __future__ in simulated shells in support of PEP 264. Much has changed from the patch version: + Repaired bad hex constant for nested_scopes. + Defined symbolic CO_xxx names so global search will find these uses. + Made the exported list of feature names explicit, instead of abusing __all__ for this purpose (and redefined __all__ accordingly). + Added gross .compiler_flag verification to test___future__.py, and reworked it a little to make use of the newly exported explicit list of feature names.
1 parent b0a98e9 commit de642bd

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

Lib/__future__.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
Each line is of the form:
44
5-
FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ")"
5+
FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ","
6+
CompilerFlag ")"
67
78
where, normally, OptionalRelease < MandatoryRelease, and both are 5-tuples
89
of the same form as sys.version_info:
@@ -37,13 +38,37 @@
3738
Instances of class _Feature have two corresponding methods,
3839
.getOptionalRelease() and .getMandatoryRelease().
3940
41+
CompilerFlag is the (bitfield) flag that should be passed in the fourth
42+
argument to the builtin function compile() to enable the feature in
43+
dynamically compiled code. This flag is stored in the .compiler_flag
44+
attribute on _Future instances. These values must match the appropriate
45+
#defines of CO_xxx flags in Include/compile.h.
46+
4047
No feature line is ever to be deleted from this file.
4148
"""
4249

50+
all_feature_names = [
51+
"nested_scopes",
52+
"generators",
53+
"division",
54+
]
55+
56+
__all__ = ["all_feature_names"] + all_feature_names
57+
58+
59+
# The CO_xxx symbols are defined here under the same names used by
60+
# compile.h, so that an editor search will find them here. However,
61+
# they're not exported in __all__, because they don't really belong to
62+
# this module.
63+
CO_NESTED = 0x0010 # nested_scopes
64+
CO_GENERATOR_ALLOWED = 0x1000 # generators
65+
CO_FUTURE_DIVISION = 0x2000 # division
66+
4367
class _Feature:
44-
def __init__(self, optionalRelease, mandatoryRelease):
68+
def __init__(self, optionalRelease, mandatoryRelease, compiler_flag):
4569
self.optional = optionalRelease
4670
self.mandatory = mandatoryRelease
71+
self.compiler_flag = compiler_flag
4772

4873
def getOptionalRelease(self):
4974
"""Return first release in which this feature was recognized.
@@ -63,9 +88,17 @@ def getMandatoryRelease(self):
6388
return self.mandatory
6489

6590
def __repr__(self):
66-
return "Feature(" + `self.getOptionalRelease()` + ", " + \
67-
`self.getMandatoryRelease()` + ")"
91+
return "_Feature(" + `self.getOptionalRelease()` + ", " + \
92+
`self.getMandatoryRelease()` + ")"
93+
94+
nested_scopes = _Feature((2, 1, 0, "beta", 1),
95+
(2, 2, 0, "alpha", 0),
96+
CO_NESTED)
97+
98+
generators = _Feature((2, 2, 0, "alpha", 1),
99+
(2, 3, 0, "final", 0),
100+
CO_GENERATOR_ALLOWED)
68101

69-
nested_scopes = _Feature((2, 1, 0, "beta", 1), (2, 2, 0, "alpha", 0))
70-
generators = _Feature((2, 2, 0, "alpha", 1), (2, 3, 0, "final", 0))
71-
division = _Feature((2, 2, 0, "alpha", 2), (3, 0, 0, "alpha", 0))
102+
division = _Feature((2, 2, 0, "alpha", 2),
103+
(3, 0, 0, "alpha", 0),
104+
CO_FUTURE_DIVISION)

Lib/test/test___future__.py

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

66
GOOD_SERIALS = ("alpha", "beta", "candidate", "final")
77

8-
features = [x for x in dir(__future__) if x[:1] != "_"]
8+
features = __future__.all_feature_names
99
for feature in features:
1010
value = getattr(__future__, feature)
1111
if verbose:
@@ -39,3 +39,8 @@
3939
verify(type(serial) is IntType, "mandatory serial isn't int")
4040
verify(optional < mandatory,
4141
"optional not less than mandatory, and mandatory not None")
42+
43+
verify(hasattr(value, "compiler_flag"),
44+
"feature is missing a .compiler_flag attr")
45+
verify(type(getattr(value, "compiler_flag")) is IntType,
46+
".compiler_flag isn't int")

0 commit comments

Comments
 (0)