Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion torch/csrc/dynamo/framelocals_mapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,22 @@ FrameLocalsMapping::FrameLocalsMapping(FrameLocalsFrameType* frame)

void FrameLocalsMapping::_realize_dict() {
_dict = py::dict();
std::unordered_set<PyObject*> seen;
py::tuple framelocals_names = code_framelocals_names(_code_obj);
PyObject* framelocals_names_ptr = framelocals_names.ptr();
PyCodeObject* co = (PyCodeObject*)_code_obj.ptr();

auto update_mapping = [&](int i) {
DEBUG_CHECK(0 <= i && i < _framelocals.size());
PyObject* value = _framelocals[i].ptr();
PyObject* name_ptr = PyTuple_GET_ITEM(framelocals_names_ptr, i);
if (value == nullptr) {
_dict.attr("pop")(framelocals_names[i], py::none());
if (name_ptr != nullptr && seen.count(name_ptr) != 0) {
_dict.attr("pop")(framelocals_names[i], py::none());
}
} else {
_dict[framelocals_names[i]] = value;
seen.insert(name_ptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, is there a guarantee that entries in framelocals_names with the same string value are the string object? If not, then the unordered_set should use Python string compare for its hash/equality? (We should also try to craft a test case that fails if this update is done incorrectly - I recall encountering failing tests when I made mistakes in the initial implementation, so I'm confused as to why the tests today don't seem to catch incorrect updates.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was my concern as well. I don't know how to make that test case though.

}
};

Expand Down
Loading