__future__.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.
  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/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. ]
  45. __all__ = ["all_feature_names"] + all_feature_names
  46. # The CO_xxx symbols are defined here under the same names used by
  47. # compile.h, so that an editor search will find them here. However,
  48. # they're not exported in __all__, because they don't really belong to
  49. # this module.
  50. CO_NESTED = 0x0010 # nested_scopes
  51. CO_GENERATOR_ALLOWED = 0 # generators (obsolete, was 0x1000)
  52. CO_FUTURE_DIVISION = 0x2000 # division
  53. CO_FUTURE_ABSOLUTE_IMPORT = 0x4000 # perform absolute imports by default
  54. CO_FUTURE_WITH_STATEMENT = 0x8000 # with statement
  55. CO_FUTURE_PRINT_FUNCTION = 0x10000 # print function
  56. CO_FUTURE_UNICODE_LITERALS = 0x20000 # unicode string literals
  57. CO_FUTURE_BARRY_AS_BDFL = 0x40000
  58. CO_FUTURE_GENERATOR_STOP = 0x80000 # StopIteration becomes RuntimeError in generators
  59. class _Feature:
  60. def __init__(self, optionalRelease, mandatoryRelease, compiler_flag):
  61. self.optional = optionalRelease
  62. self.mandatory = mandatoryRelease
  63. self.compiler_flag = compiler_flag
  64. def getOptionalRelease(self):
  65. """Return first release in which this feature was recognized.
  66. This is a 5-tuple, of the same form as sys.version_info.
  67. """
  68. return self.optional
  69. def getMandatoryRelease(self):
  70. """Return release in which this feature will become mandatory.
  71. This is a 5-tuple, of the same form as sys.version_info, or, if
  72. the feature was dropped, is None.
  73. """
  74. return self.mandatory
  75. def __repr__(self):
  76. return "_Feature" + repr((self.optional,
  77. self.mandatory,
  78. self.compiler_flag))
  79. nested_scopes = _Feature((2, 1, 0, "beta", 1),
  80. (2, 2, 0, "alpha", 0),
  81. CO_NESTED)
  82. generators = _Feature((2, 2, 0, "alpha", 1),
  83. (2, 3, 0, "final", 0),
  84. CO_GENERATOR_ALLOWED)
  85. division = _Feature((2, 2, 0, "alpha", 2),
  86. (3, 0, 0, "alpha", 0),
  87. CO_FUTURE_DIVISION)
  88. absolute_import = _Feature((2, 5, 0, "alpha", 1),
  89. (3, 0, 0, "alpha", 0),
  90. CO_FUTURE_ABSOLUTE_IMPORT)
  91. with_statement = _Feature((2, 5, 0, "alpha", 1),
  92. (2, 6, 0, "alpha", 0),
  93. CO_FUTURE_WITH_STATEMENT)
  94. print_function = _Feature((2, 6, 0, "alpha", 2),
  95. (3, 0, 0, "alpha", 0),
  96. CO_FUTURE_PRINT_FUNCTION)
  97. unicode_literals = _Feature((2, 6, 0, "alpha", 2),
  98. (3, 0, 0, "alpha", 0),
  99. CO_FUTURE_UNICODE_LITERALS)
  100. barry_as_FLUFL = _Feature((3, 1, 0, "alpha", 2),
  101. (3, 9, 0, "alpha", 0),
  102. CO_FUTURE_BARRY_AS_BDFL)
  103. generator_stop = _Feature((3, 5, 0, "beta", 1),
  104. (3, 7, 0, "alpha", 0),
  105. CO_FUTURE_GENERATOR_STOP)