Skip to content

Commit

Permalink
fix windows bugs
Browse files Browse the repository at this point in the history
fix windows bugs
  • Loading branch information
huangshiyu13 authored Oct 19, 2023
2 parents 782cb07 + 254cf26 commit 948238d
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions openrl/configs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

""""""


import os
import re
import tempfile

Expand Down Expand Up @@ -83,9 +83,19 @@ def __call__(self, parser, cfg, values, option_string=None):
# Load the rendered content as a dictionary
data = yaml.safe_load(rendered_content)

# Write the result to a temporary file
with tempfile.NamedTemporaryFile("w", delete=True, suffix=".yaml") as temp_file:
# Write the result to a temporary file. Not work on Windows.
# with tempfile.NamedTemporaryFile("w", delete=True, suffix=".yaml") as temp_file:
# yaml.dump(data, temp_file)
# temp_file.seek(0) # Move to the beginning of the file
# # Use the default behavior of ActionConfigFile to handle the temporary file
# super().__call__(parser, cfg, temp_file.name, option_string)

# Write the result to a temporary file. This works on all platforms.
temp_fd, temp_filename = tempfile.mkstemp(suffix=".yaml")
with os.fdopen(temp_fd, 'w') as temp_file:
yaml.dump(data, temp_file)
temp_file.seek(0) # Move to the beginning of the file
try:
# Use the default behavior of ActionConfigFile to handle the temporary file
super().__call__(parser, cfg, temp_file.name, option_string)
super().__call__(parser, cfg, temp_filename, option_string)
finally:
os.remove(temp_filename)

0 comments on commit 948238d

Please sign in to comment.