beam: fix _key mixed-tuple crash in toposort modules (sortable wrapper)
This commit is contained in:
1 parent
528a370bb7
commit
f605e513e0
2 files changed
+44
-17
No files matched your search
@@ -169,12 +169,26 @@ def topo_sort(graph: Mapping[Any, Iterable[Any]]) -> TopoResult:
|
||||
return TopoResult(sorted=None, cycle=cycle)
|
||||
|
||||
|
||||
class _Sortable:
|
||||
__slots__ = ("x", "tn")
|
||||
def __init__(self, x):
|
||||
self.x = x
|
||||
self.tn = type(x).__name__
|
||||
def __lt__(self, other):
|
||||
if not isinstance(other, _Sortable):
|
||||
return NotImplemented
|
||||
if self.tn != other.tn:
|
||||
return self.tn < other.tn
|
||||
try:
|
||||
return self.x < other.x
|
||||
except Exception:
|
||||
rx, ry = repr(self.x), repr(other.x)
|
||||
if rx != ry:
|
||||
return rx < ry
|
||||
return id(self.x) < id(other.x)
|
||||
def __eq__(self, other):
|
||||
return isinstance(other, _Sortable) and self.x == other.x
|
||||
|
||||
|
||||
def _key(x):
|
||||
tn = type(x).__name__
|
||||
if isinstance(x, (set, frozenset)):
|
||||
return (1, tn, repr(x), id(x))
|
||||
try:
|
||||
_ = x < x
|
||||
except Exception:
|
||||
return (1, tn, repr(x), id(x))
|
||||
return (0, tn, x)
|
||||
return _Sortable(x)
|
||||
@@ -169,13 +169,26 @@ def topo_sort(graph: Mapping[Any, Iterable[Any]]) -> TopoResult:
|
||||
return TopoResult(sorted=None, cycle=cycle)
|
||||
|
||||
|
||||
class _Sortable:
|
||||
__slots__ = ("x", "tn")
|
||||
def __init__(self, x):
|
||||
self.x = x
|
||||
self.tn = type(x).__name__
|
||||
def __lt__(self, other):
|
||||
if not isinstance(other, _Sortable):
|
||||
return NotImplemented
|
||||
if self.tn != other.tn:
|
||||
return self.tn < other.tn
|
||||
try:
|
||||
return self.x < other.x
|
||||
except Exception:
|
||||
rx, ry = repr(self.x), repr(other.x)
|
||||
if rx != ry:
|
||||
return rx < ry
|
||||
return id(self.x) < id(other.x)
|
||||
def __eq__(self, other):
|
||||
return isinstance(other, _Sortable) and self.x == other.x
|
||||
|
||||
|
||||
def _key(x):
|
||||
tn = type(x).__name__
|
||||
# Probe whether instances of this type support ordering; if not, fall back
|
||||
# to a repr/id-based key so mixed or unorderable node types never crash
|
||||
# the ready-queue sort or cycle reconstruction.
|
||||
try:
|
||||
_ = x < x
|
||||
except (TypeError, Exception):
|
||||
return (1, tn, repr(x), id(x))
|
||||
return (0, tn, x)
|
||||
return _Sortable(x)
|
||||
Reference in new issue
Block a user