/pypy/interpreter/miscutils.py

https://github.com/mesalock-linux/mesapy · Python · 70 lines · 64 code · 3 blank · 3 comment · 0 complexity · 080e817f2b243f095939dfb1ade4e08b MD5 · raw file

  1. """
  2. Miscellaneous utilities.
  3. """
  4. from rpython.rlib.listsort import make_timsort_class
  5. from rpython.rlib.objectmodel import not_rpython
  6. class ThreadLocals:
  7. """Pseudo thread-local storage, for 'space.threadlocals'.
  8. This is not really thread-local at all; the intention is that the PyPy
  9. implementation of the 'thread' module knows how to provide a real
  10. implementation for this feature, and patches 'space.threadlocals' when
  11. 'thread' is initialized.
  12. """
  13. _immutable_fields_ = ['_value?']
  14. _value = None
  15. def get_ec(self):
  16. return self._value
  17. def enter_thread(self, space):
  18. self._value = space.createexecutioncontext()
  19. def try_enter_thread(self, space):
  20. return False
  21. def signals_enabled(self):
  22. return True
  23. def enable_signals(self, space):
  24. pass
  25. def disable_signals(self, space):
  26. pass
  27. def getallvalues(self):
  28. return {0: self._value}
  29. def _cleanup_(self):
  30. # should still be unfilled at this point during translation.
  31. # but in some corner cases it is not... unsure why
  32. self._value = None
  33. @not_rpython
  34. def make_weak_value_dictionary(space, keytype, valuetype):
  35. if space.config.translation.rweakref:
  36. from rpython.rlib.rweakref import RWeakValueDictionary
  37. return RWeakValueDictionary(keytype, valuetype)
  38. else:
  39. class FakeWeakValueDict(object):
  40. def __init__(self):
  41. self._dict = {}
  42. def get(self, key):
  43. return self._dict.get(key, None)
  44. def set(self, key, value):
  45. self._dict[key] = value
  46. return FakeWeakValueDict()
  47. _StringBaseTimSort = make_timsort_class()
  48. class StringSort(_StringBaseTimSort):
  49. def lt(self, a, b):
  50. return a < b
  51. def string_sort(lst):
  52. """Sort a (resizable) list of strings."""
  53. sorter = StringSort(lst, len(lst))
  54. sorter.sort()