Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix windows bugs #250

Merged
merged 2 commits into from
Oct 19, 2023
Merged
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
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)
Loading