提交 2f299464 编写于 作者: A A. Unique TensorFlower 提交者: TensorFlower Gardener

Adding __hash__ for forward compatibility with pybind11 update.

Backward compatible.

Relevant pybind11 change: https://github.com/pybind/pybind11/pull/2291
Note: This pybind11 change makes pybind11 compatible with native Python behavior:
* https://docs.python.org/3/reference/datamodel.html#object.__hash__
* A class that overrides __eq__() and does not define __hash__() will have its __hash__() implicitly set to None.
PiperOrigin-RevId: 327879230
Change-Id: I8573b761ec94e8889b4eb5ac30a71ee0314e6578
上级 1c50283a
......@@ -435,7 +435,7 @@ class DistributedVarOp(object):
self.traceback == o.traceback and self.type == o.type)
def __hash__(self):
return hash((self.name, self.graph, self.traceback, self.type))
return hash((self.name, self.graph, tuple(self.traceback), self.type))
class DistributedVariable(DistributedDelegate, variables_lib.Variable,
......
......@@ -127,6 +127,11 @@ PYBIND11_MODULE(_tf_stack, m) {
// For compatibility with the traceback module.
.def("__eq__", &FrameSummary::operator==)
.def("__ne__", &FrameSummary::operator!=)
.def("__hash__",
[](const FrameSummary& self) {
return py::hash(
py::make_tuple(self.filename, self.lineno, self.name));
})
.def("__getitem__",
[](const FrameSummary& self, const py::object& index) -> py::object {
return py::make_tuple(self.filename, self.lineno, self.name,
......
......@@ -52,6 +52,17 @@ class TFStackTest(test.TestCase):
another_frame0, _ = tf_stack.extract_stack(limit=2)
self.assertEqual(frame0, another_frame0)
def testFrameSummaryEqualityAndHash(self):
# Both defined on the same line to produce identical stacks.
frame1, frame2 = tf_stack.extract_stack(), tf_stack.extract_stack()
self.assertEqual(len(frame1), len(frame2))
for f1, f2 in zip(frame1, frame2):
self.assertEqual(f1, f2)
self.assertEqual(hash(f1), hash(f1))
self.assertEqual(hash(f1), hash(f2))
self.assertEqual(frame1, frame2)
self.assertEqual(hash(tuple(frame1)), hash(tuple(frame2)))
def extract_stack(limit=None):
# Both defined on the same line to produce identical stacks.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册