Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_compat.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2001-2012 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. import os
  15. import sys
  16. PYPY = hasattr(sys, 'pypy_version_info')
  17. # We can and do build the C extensions on PyPy, but
  18. # as of Persistent 4.2.5 the persistent C extension is not
  19. # built on PyPy, so importing our C extension will fail anyway.
  20. PURE_PYTHON = os.environ.get('PURE_PYTHON', PYPY)
  21. if sys.version_info[0] < 3: #pragma NO COVER Python2
  22. PY2 = True
  23. PY3 = False
  24. int_types = int, long
  25. xrange = xrange
  26. def compare(x, y):
  27. if x is None:
  28. if y is None:
  29. return 0
  30. else:
  31. return -1
  32. elif y is None:
  33. return 1
  34. else:
  35. return cmp(x, y)
  36. _bytes = str
  37. def _ascii(x):
  38. return bytes(x)
  39. else: #pragma NO COVER Python3
  40. PY2 = False
  41. PY3 = True
  42. int_types = int,
  43. xrange = range
  44. def compare(x, y):
  45. if x is None:
  46. if y is None:
  47. return 0
  48. else:
  49. return -1
  50. elif y is None:
  51. return 1
  52. else:
  53. return (x > y) - (y > x)
  54. _bytes = bytes
  55. def _ascii(x):
  56. return bytes(x, 'ascii')
  57. def import_c_extension(mod_globals):
  58. import importlib
  59. c_module = None
  60. module_name = mod_globals['__name__']
  61. assert module_name.startswith('BTrees.')
  62. module_name = module_name.split('.')[1]
  63. if not PURE_PYTHON:
  64. try:
  65. c_module = importlib.import_module('BTrees._' + module_name)
  66. except ImportError:
  67. pass
  68. if c_module is not None:
  69. new_values = dict(c_module.__dict__)
  70. new_values.pop("__name__", None)
  71. new_values.pop('__file__', None)
  72. new_values.pop('__doc__', None)
  73. mod_globals.update(new_values)
  74. else:
  75. # No C extension, make the Py versions available without that
  76. # extension. The list comprehension both filters and prevents
  77. # concurrent modification errors.
  78. for py in [k for k in mod_globals if k.endswith('Py')]:
  79. mod_globals[py[:-2]] = mod_globals[py]
  80. # Assign the global aliases
  81. prefix = module_name[:2]
  82. for name in ('Bucket', 'Set', 'BTree', 'TreeSet'):
  83. mod_globals[name] = mod_globals[prefix + name]
  84. # Cleanup
  85. del mod_globals['import_c_extension']