Skip to content

Commit

Permalink
[Fix] Fix pytorch2.0 does not support sys._getframe used to remove pr…
Browse files Browse the repository at this point in the history
…ivate keys (#979)

* [Fix] fix pytorch2.0 not support sys._getframe to remove private key

* Update mmengine/structures/base_data_element.py

---------

Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
  • Loading branch information
Harold-lkk and zhouzaida authored Mar 14, 2023
1 parent ad33a7d commit 67ce04d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions mmengine/structures/base_data_element.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import sys
from typing import Any, Iterator, Optional, Tuple, Type, Union

import numpy as np
Expand Down Expand Up @@ -309,7 +308,16 @@ def keys(self) -> list:
Returns:
list: Contains all keys in data_fields.
"""
return list(self._data_fields)
# We assume that the name of the attribute related to property is
# '_' + the name of the property. We use this rule to filter out
# private keys.
# TODO: Use a more robust way to solve this problem
private_keys = {
'_' + key
for key in self._data_fields
if isinstance(getattr(type(self), key, None), property)
}
return list(self._data_fields - private_keys)

def metainfo_keys(self) -> list:
"""
Expand Down Expand Up @@ -466,12 +474,7 @@ def set_field(self,
raise AttributeError(
f'Cannot set {name} to be a field of data '
f'because {name} is already a metainfo field')
# The name only added to `data_fields` when it is not the
# attribute related to property(methods decorated by @property).
if not isinstance(
getattr(type(self),
sys._getframe(1).f_code.co_name, None), property):
self._data_fields.add(name)
self._data_fields.add(name)
super().__setattr__(name, value)

# Tensor-like methods
Expand Down

0 comments on commit 67ce04d

Please sign in to comment.