Skip to content

Commit f143e1c

Browse files
Fix open symlinks (#3098)
- Cherry-pick of #3096 - Cherry-pick of #3097 test_examples/622/ - passed
1 parent f6f1513 commit f143e1c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

nncf/common/tensor_statistics/statistics_serializer.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from typing import Any, Dict, Optional, Tuple, cast
1717

1818
import nncf
19+
from nncf.common.utils.os import fail_if_symlink
20+
from nncf.common.utils.os import safe_open
1921

2022
METADATA_FILE = "statistics_metadata.json"
2123

@@ -35,7 +37,7 @@ def load_metadata(dir_path: Path) -> Dict[str, Any]:
3537
"""
3638
metadata_file = dir_path / METADATA_FILE
3739
if metadata_file.exists():
38-
with open(metadata_file, "r") as f:
40+
with safe_open(metadata_file, "r") as f:
3941
return cast(Dict[str, Any], json.load(f))
4042
return {"mapping": {}, "metadata": {}}
4143

@@ -47,7 +49,7 @@ def save_metadata(metadata: Dict[str, Any], dir_path: Path) -> None:
4749
:param dir_path: The directory where the metadata file will be stored.
4850
"""
4951
metadata_file = dir_path / METADATA_FILE
50-
with open(metadata_file, "w") as f:
52+
with safe_open(metadata_file, "w") as f:
5153
json.dump(metadata, f, indent=4)
5254

5355

@@ -70,6 +72,7 @@ def load_from_dir(dir_path: str) -> Tuple[Dict[str, Any], Dict[str, str]]:
7072
continue # Skip the metadata file
7173

7274
try:
75+
fail_if_symlink(statistics_file)
7376
with gzip.open(statistics_file, "rb") as f:
7477
sanitized_name = statistics_file.name
7578
original_name = mapping.get(sanitized_name, sanitized_name)
@@ -101,6 +104,7 @@ def dump_to_dir(
101104
mapping[sanitized_name] = original_name
102105

103106
try:
107+
fail_if_symlink(file_path)
104108
with gzip.open(file_path, "wb") as f:
105109
pickle.dump(statistics_value, f)
106110
except (IOError, pickle.PicklingError) as e:

0 commit comments

Comments
 (0)