| 1 |
"""Common code between queues and channels.""" |
| 2 |
|
| 3 |
|
| 4 |
class ItemInterpreterDestroyed(Exception): |
| 5 |
"""Raised when trying to get an item whose interpreter was destroyed.""" |
| 6 |
|
| 7 |
|
| 8 |
class classonly: |
| 9 |
"""A non-data descriptor that makes a value only visible on the class. |
| 10 |
|
| 11 |
This is like the "classmethod" builtin, but does not show up on |
| 12 |
instances of the class. It may be used as a decorator. |
| 13 |
""" |
| 14 |
|
| 15 |
def __init__(self, value): |
| 16 |
self.value = value |
| 17 |
self.getter = classmethod(value).__get__ |
| 18 |
self.name = None |
| 19 |
|
| 20 |
def __set_name__(self, cls, name): |
| 21 |
if self.name is not None: |
| 22 |
raise TypeError('already used') |
| 23 |
self.name = name |
| 24 |
|
| 25 |
def __get__(self, obj, cls): |
| 26 |
if obj is not None: |
| 27 |
raise AttributeError(self.name) |
| 28 |
# called on the class |
| 29 |
return self.getter(None, cls) |
| 30 |
|
| 31 |
|
| 32 |
class UnboundItem: |
| 33 |
"""Represents a cross-interpreter item no longer bound to an interpreter. |
| 34 |
|
| 35 |
An item is unbound when the interpreter that added it to the |
| 36 |
cross-interpreter container is destroyed. |
| 37 |
""" |
| 38 |
|
| 39 |
__slots__ = () |
| 40 |
|
| 41 |
@classonly |
| 42 |
def singleton(cls, kind, module, name='UNBOUND'): |
| 43 |
doc = cls.__doc__ |
| 44 |
if doc: |
| 45 |
doc = doc.replace( |
| 46 |
'cross-interpreter container', kind, |
| 47 |
).replace( |
| 48 |
'cross-interpreter', kind, |
| 49 |
) |
| 50 |
subclass = type( |
| 51 |
f'Unbound{kind.capitalize()}Item', |
| 52 |
(cls,), |
| 53 |
{ |
| 54 |
"_MODULE": module, |
| 55 |
"_NAME": name, |
| 56 |
"__doc__": doc, |
| 57 |
}, |
| 58 |
) |
| 59 |
return object.__new__(subclass) |
| 60 |
|
| 61 |
_MODULE = __name__ |
| 62 |
_NAME = 'UNBOUND' |
| 63 |
|
| 64 |
def __new__(cls): |
| 65 |
raise Exception(f'use {cls._MODULE}.{cls._NAME}') |
| 66 |
|
| 67 |
def __repr__(self): |
| 68 |
return f'{self._MODULE}.{self._NAME}' |
| 69 |
# return f'interpreters._queues.UNBOUND' |
| 70 |
|
| 71 |
|
| 72 |
UNBOUND = object.__new__(UnboundItem) |
| 73 |
UNBOUND_ERROR = object() |
| 74 |
UNBOUND_REMOVE = object() |
| 75 |
|
| 76 |
_UNBOUND_CONSTANT_TO_FLAG = { |
| 77 |
UNBOUND_REMOVE: 1, |
| 78 |
UNBOUND_ERROR: 2, |
| 79 |
UNBOUND: 3, |
| 80 |
} |
| 81 |
_UNBOUND_FLAG_TO_CONSTANT = {v: k |
| 82 |
for k, v in _UNBOUND_CONSTANT_TO_FLAG.items()} |
| 83 |
|
| 84 |
|
| 85 |
def serialize_unbound(unbound): |
| 86 |
op = unbound |
| 87 |
try: |
| 88 |
flag = _UNBOUND_CONSTANT_TO_FLAG[op] |
| 89 |
except KeyError: |
| 90 |
raise NotImplementedError(f'unsupported unbound replacement op {op!r}') |
| 91 |
return flag, |
| 92 |
|
| 93 |
|
| 94 |
def resolve_unbound(flag, exctype_destroyed): |
| 95 |
try: |
| 96 |
op = _UNBOUND_FLAG_TO_CONSTANT[flag] |
| 97 |
except KeyError: |
| 98 |
raise NotImplementedError(f'unsupported unbound replacement op {flag!r}') |
| 99 |
if op is UNBOUND_REMOVE: |
| 100 |
# "remove" not possible here |
| 101 |
raise NotImplementedError |
| 102 |
elif op is UNBOUND_ERROR: |
| 103 |
raise exctype_destroyed("item's original interpreter destroyed") |
| 104 |
elif op is UNBOUND: |
| 105 |
return UNBOUND |
| 106 |
else: |
| 107 |
raise NotImplementedError(repr(op)) |