_compat.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2006 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """
  15. Support functions for dealing with differences in platforms, including Python
  16. versions and implementations.
  17. This file should have no imports from the rest of zope.interface because it is
  18. used during early bootstrapping.
  19. """
  20. import os
  21. import sys
  22. PY313_OR_OLDER = sys.version_info < (3, 14)
  23. def _normalize_name(name):
  24. if isinstance(name, bytes):
  25. name = str(name, 'ascii')
  26. if isinstance(name, str):
  27. return name
  28. raise TypeError("name must be a string or ASCII-only bytes")
  29. PYPY = hasattr(sys, 'pypy_version_info')
  30. def _c_optimizations_required():
  31. """
  32. Return a true value if the C optimizations are required.
  33. This uses the ``PURE_PYTHON`` variable as documented in `_use_c_impl`.
  34. """
  35. pure_env = os.environ.get('PURE_PYTHON')
  36. require_c = pure_env == "0"
  37. return require_c
  38. def _c_optimizations_available():
  39. """
  40. Return the C optimization module, if available, otherwise
  41. a false value.
  42. If the optimizations are required but not available, this
  43. raises the ImportError.
  44. This does not say whether they should be used or not.
  45. """
  46. catch = () if _c_optimizations_required() else (ImportError,)
  47. try:
  48. from zope.interface import _zope_interface_coptimizations as c_opt
  49. return c_opt
  50. except catch: # pragma: no cover (only Jython doesn't build extensions)
  51. return False
  52. def _c_optimizations_ignored():
  53. """
  54. The opposite of `_c_optimizations_required`.
  55. """
  56. pure_env = os.environ.get('PURE_PYTHON')
  57. return pure_env is not None and pure_env != "0"
  58. def _should_attempt_c_optimizations():
  59. """
  60. Return a true value if we should attempt to use the C optimizations.
  61. This takes into account whether we're on PyPy and the value of the
  62. ``PURE_PYTHON`` environment variable, as defined in `_use_c_impl`.
  63. """
  64. is_pypy = hasattr(sys, 'pypy_version_info')
  65. if _c_optimizations_required():
  66. return True
  67. if is_pypy:
  68. return False
  69. return not _c_optimizations_ignored()
  70. def _use_c_impl(py_impl, name=None, globs=None):
  71. """
  72. Decorator. Given an object implemented in Python, with a name like
  73. ``Foo``, import the corresponding C implementation from
  74. ``zope.interface._zope_interface_coptimizations`` with the name
  75. ``Foo`` and use it instead.
  76. If the ``PURE_PYTHON`` environment variable is set to any value
  77. other than ``"0"``, or we're on PyPy, ignore the C implementation
  78. and return the Python version. If the C implementation cannot be
  79. imported, return the Python version. If ``PURE_PYTHON`` is set to
  80. 0, *require* the C implementation (let the ImportError propagate);
  81. note that PyPy can import the C implementation in this case (and all
  82. tests pass).
  83. In all cases, the Python version is kept available. in the module
  84. globals with the name ``FooPy`` and the name ``FooFallback`` (both
  85. conventions have been used; the C implementation of some functions
  86. looks for the ``Fallback`` version, as do some of the Sphinx
  87. documents).
  88. Example::
  89. @_use_c_impl
  90. class Foo(object):
  91. ...
  92. """
  93. name = name or py_impl.__name__
  94. globs = globs or sys._getframe(1).f_globals
  95. def find_impl():
  96. if not _should_attempt_c_optimizations():
  97. return py_impl
  98. c_opt = _c_optimizations_available()
  99. if not c_opt: # pragma: no cover (Jython doesn't build extensions)
  100. return py_impl
  101. __traceback_info__ = c_opt
  102. return getattr(c_opt, name)
  103. c_impl = find_impl()
  104. # Always make available by the FooPy name and FooFallback
  105. # name (for testing and documentation)
  106. globs[name + 'Py'] = py_impl
  107. globs[name + 'Fallback'] = py_impl
  108. return c_impl