Skip to content

Commit 9621bd3

Browse files
committed
sanitize filename
1 parent d046a96 commit 9621bd3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/lightning/pytorch/profilers/profiler.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import logging
1717
import os
18+
import re
1819
from abc import ABC, abstractmethod
1920
from collections.abc import Generator
2021
from contextlib import contextmanager
@@ -81,6 +82,7 @@ def _prepare_filename(
8182
action_name: Optional[str] = None,
8283
extension: str = ".txt",
8384
split_token: str = "-", # noqa: S107
85+
sanitize: bool = True,
8486
) -> str:
8587
args = []
8688
if self._stage is not None:
@@ -91,7 +93,15 @@ def _prepare_filename(
9193
args.append(str(self._local_rank))
9294
if action_name is not None:
9395
args.append(action_name)
94-
return split_token.join(args) + extension
96+
base = split_token.join(args)
97+
if sanitize:
98+
# Replace a set of path-unsafe characters across platforms with '_'
99+
base = re.sub(r"[\\/:*?\"<>|\n\r\t]", "_", base)
100+
base = re.sub(r"_+", "_", base)
101+
base = base.strip()
102+
if not base:
103+
base = "profile"
104+
return base + extension
95105

96106
def _prepare_streams(self) -> None:
97107
if self._write_stream is not None:

0 commit comments

Comments
 (0)