__future__.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """Record of phased-in incompatible language changes.
  2. Each line is of the form:
  3. FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ","
  4. CompilerFlag ")"
  5. where, normally, OptionalRelease < MandatoryRelease, and both are 5-tuples
  6. of the same form as sys.version_info:
  7. (PY_MAJOR_VERSION, # the 2 in 2.1.0a3; an int
  8. PY_MINOR_VERSION, # the 1; an int
  9. PY_MICRO_VERSION, # the 0; an int
  10. PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" or "final"; string
  11. PY_RELEASE_SERIAL # the 3; an int
  12. )
  13. OptionalRelease records the first release in which
  14. from __future__ import FeatureName
  15. was accepted.
  16. In the case of MandatoryReleases that have not yet occurred,
  17. MandatoryRelease predicts the release in which the feature will become part
  18. of the language.
  19. Else MandatoryRelease records when the feature became part of the language;
  20. in releases at or after that, modules no longer need
  21. from __future__ import FeatureName
  22. to use the feature in question, but may continue to use such imports.
  23. MandatoryRelease may also be None, meaning that a planned feature got
  24. dropped or that the release version is undetermined.
  25. Instances of class _Feature have two corresponding methods,
  26. .getOptionalRelease() and .getMandatoryRelease().
  27. CompilerFlag is the (bitfield) flag that should be passed in the fourth
  28. argument to the builtin function compile() to enable the feature in
  29. dynamically compiled code. This flag is stored in the .compiler_flag
  30. attribute on _Future instances. These values must match the appropriate
  31. #defines of CO_xxx flags in Include/cpython/compile.h.
  32. No feature line is ever to be deleted from this file.
  33. """
  34. all_feature_names = [
  35. "nested_scopes",
  36. "generators",
  37. "division",
  38. "absolute_import",
  39. "with_statement",
  40. "print_function",
  41. "unicode_literals",
  42. "barry_as_FLUFL",
  43. "generator_stop",
  44. "annotations",
  45. ]
  46. __all__ = ["all_feature_names"] + all_feature_names
  47. # The CO_xxx symbols are defined here under the same names defined in
  48. # code.h and used by compile.h, so that an editor search will find them here.
  49. # However, they're not exported in __all__, because they don't really belong to
  50. # this module.
  51. CO_NESTED = 0x0010 # nested_scopes
  52. CO_GENERATOR_ALLOWED = 0 # generators (obsolete, was 0x1000)
  53. CO_FUTURE_DIVISION = 0x20000 # division
  54. CO_FUTURE_ABSOLUTE_IMPORT = 0x40000 # perform absolute imports by default
  55. CO_FUTURE_WITH_STATEMENT = 0x80000 # with statement
  56. CO_FUTURE_PRINT_FUNCTION = 0x100000 # print function
  57. CO_FUTURE_UNICODE_LITERALS = 0x200000 # unicode string literals
  58. CO_FUTURE_BARRY_AS_BDFL = 0x400000
  59. CO_FUTURE_GENERATOR_STOP = 0x800000 # StopIteration becomes RuntimeError in generators
  60. CO_FUTURE_ANNOTATIONS = 0x1000000 # annotations become strings at runtime
  61. class _Feature:
  62. def __init__(self, optionalRelease, mandatoryRelease, compiler_flag):
  63. self.optional = optionalRelease
  64. self.mandatory = mandatoryRelease
  65. self.compiler_flag = compiler_flag
  66. def getOptionalRelease(self):
  67. """Return first release in which this feature was recognized.
  68. This is a 5-tuple, of the same form as sys.version_info.
  69. """
  70. return self.optional
  71. def getMandatoryRelease(self):
  72. """Return release in which this feature will become mandatory.
  73. This is a 5-tuple, of the same form as sys.version_info, or, if
  74. the feature was dropped, or the release date is undetermined, is None.
  75. """
  76. return self.mandatory
  77. def __repr__(self):
  78. return "_Feature" + repr((self.optional,
  79. self.mandatory,
  80. self.compiler_flag))
  81. nested_scopes = _Feature((2, 1, 0, "beta", 1),
  82. (2, 2, 0, "alpha", 0),
  83. CO_NESTED)
  84. generators = _Feature((2, 2, 0, "alpha", 1),
  85. (2, 3, 0, "final", 0),
  86. CO_GENERATOR_ALLOWED)
  87. division = _Feature((2, 2, 0, "alpha", 2),
  88. (3, 0, 0, "alpha", 0),
  89. CO_FUTURE_DIVISION)
  90. absolute_import = _Feature((2, 5, 0, "alpha", 1),
  91. (3, 0, 0, "alpha", 0),
  92. CO_FUTURE_ABSOLUTE_IMPORT)
  93. with_statement = _Feature((2, 5, 0, "alpha", 1),
  94. (2, 6, 0, "alpha", 0),
  95. CO_FUTURE_WITH_STATEMENT)
  96. print_function = _Feature((2, 6, 0, "alpha", 2),
  97. (3, 0, 0, "alpha", 0),
  98. CO_FUTURE_PRINT_FUNCTION)
  99. unicode_literals = _Feature((2, 6, 0, "alpha", 2),
  100. (3, 0, 0, "alpha", 0),
  101. CO_FUTURE_UNICODE_LITERALS)
  102. barry_as_FLUFL = _Feature((3, 1, 0, "alpha", 2),
  103. (4, 0, 0, "alpha", 0),
  104. CO_FUTURE_BARRY_AS_BDFL)
  105. generator_stop = _Feature((3, 5, 0, "beta", 1),
  106. (3, 7, 0, "alpha", 0),
  107. CO_FUTURE_GENERATOR_STOP)
  108. annotations = _Feature((3, 7, 0, "beta", 1),
  109. None,
  110. CO_FUTURE_ANNOTATIONS)