-
Notifications
You must be signed in to change notification settings - Fork 130
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
Use file object directly in temporary_config()
#1598
Use file object directly in temporary_config()
#1598
Conversation
The context manager uses `NamedTemporaryFile` to store the current configuration, to later restore them. Instead of passing the file object directly to the save function, it just passes the file name, i.e. the save (and the load function) will open the file again, which is in itself not a problem. However, on the Github Windows image this leads to a permission error (using the created file object is fine). This commit solves this by adding the `file` argument to `Config.save()` that allows to pass a file object directly to the function. The same change is applied to the load function of the config object.
It is known that DaCe does not work out of the box on [MacOSX](https://spcldace.readthedocs.io/en/latest/setup/installation.html#common-issues-with-the-dace-python-module) and [Windows](spcl/dace#1598). Furthermore, since JaCe is a pure Python project we relay on the portability of DaCe and Jax and in the end Python. Thus every crossplatform problem should be handled by DaCe and Jax and not JaCe.
…he previous code repopened the file with `open()` that has `rt` the new file must also be opened that way.
temporary_config()
context manager.temporary_config()
context manager
temporary_config()
context managertemporary_config()
@@ -39,10 +40,11 @@ def temporary_config(): | |||
Config.set("optimizer", "autooptimize", value=True) | |||
foo() | |||
""" | |||
with tempfile.NamedTemporaryFile() as fp: | |||
Config.save(fp.name) | |||
with tempfile.NamedTemporaryFile(mode='w+t') as fp: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does 't' mean in mode
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It means text mode, on UNIX \n
is the newline character, but on Windows it is \r\n
(https://en.wikipedia.org/wiki/Newline#Representation). In that mode \r\n
is transformed into \n
it is essentially a compatibility layer, see help(open)
.
I used it because it preserves old behaviour, but it is probably not needed.
The context manager uses
NamedTemporaryFile
to store the current configuration, to later restore them. Instead of passing the file object directly to the save function, it just passes the file name, i.e. the save (and the load function) will open the file again, which is in itself not a problem. However, on the Github Windows image this leads to a permission error (using the created file object is fine).This commit solves this by adding the
file
argument toConfig.save()
that allows to pass a file object directly to the function. The same change is applied to the load function of the config object.