| 1 |
""" |
| 2 |
Miscellaneous utilities. |
| 3 |
""" |
| 4 |
|
| 5 |
from rpython.rlib.listsort import make_timsort_class |
| 6 |
from rpython.rlib.objectmodel import not_rpython |
| 7 |
|
| 8 |
|
| 9 |
class ThreadLocals: |
| 10 |
"""Pseudo thread-local storage, for 'space.threadlocals'. |
| 11 |
This is not really thread-local at all; the intention is that the PyPy |
| 12 |
implementation of the 'thread' module knows how to provide a real |
| 13 |
implementation for this feature, and patches 'space.threadlocals' when |
| 14 |
'thread' is initialized. |
| 15 |
""" |
| 16 |
_immutable_fields_ = ['_value?'] |
| 17 |
_value = None |
| 18 |
|
| 19 |
def get_ec(self): |
| 20 |
return self._value |
| 21 |
|
| 22 |
def enter_thread(self, space): |
| 23 |
self._value = space.createexecutioncontext() |
| 24 |
|
| 25 |
def try_enter_thread(self, space): |
| 26 |
return False |
| 27 |
|
| 28 |
def signals_enabled(self): |
| 29 |
return True |
| 30 |
|
| 31 |
def enable_signals(self, space): |
| 32 |
pass |
| 33 |
|
| 34 |
def disable_signals(self, space): |
| 35 |
pass |
| 36 |
|
| 37 |
def getallvalues(self): |
| 38 |
return {0: self._value} |
| 39 |
|
| 40 |
def _cleanup_(self): |
| 41 |
|
| 42 |
|
| 43 |
self._value = None |
| 44 |
|
| 45 |
def setup_threads(self, space): |
| 46 |
|
| 47 |
pass |
| 48 |
|
| 49 |
def reinit_threads(self, space): |
| 50 |
|
| 51 |
pass |
| 52 |
|
| 53 |
|
| 54 |
@not_rpython |
| 55 |
def make_weak_value_dictionary(space, keytype, valuetype): |
| 56 |
if space.config.translation.rweakref: |
| 57 |
from rpython.rlib.rweakref import RWeakValueDictionary |
| 58 |
return RWeakValueDictionary(keytype, valuetype) |
| 59 |
else: |
| 60 |
class FakeWeakValueDict(object): |
| 61 |
def __init__(self): |
| 62 |
self._dict = {} |
| 63 |
def get(self, key): |
| 64 |
return self._dict.get(key, None) |
| 65 |
def set(self, key, value): |
| 66 |
self._dict[key] = value |
| 67 |
return FakeWeakValueDict() |
| 68 |
|
| 69 |
|
| 70 |
_StringBaseTimSort = make_timsort_class() |
| 71 |
|
| 72 |
class StringSort(_StringBaseTimSort): |
| 73 |
def lt(self, a, b): |
| 74 |
return a < b |
| 75 |
|
| 76 |
def string_sort(lst): |
| 77 |
"""Sort a (resizable) list of strings.""" |
| 78 |
sorter = StringSort(lst, len(lst)) |
| 79 |
sorter.sort() |