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

FileExistsError does not exist in Python 2 #459

Closed
raphtown opened this issue Apr 24, 2019 · 5 comments · Fixed by #517
Closed

FileExistsError does not exist in Python 2 #459

raphtown opened this issue Apr 24, 2019 · 5 comments · Fixed by #517

Comments

@raphtown
Copy link

FileExistsError was only added in Python 3. If an exception is thrown by mkdir, then the exception is not caught and properly dealt with.

except FileExistsError: # Catch race conditions

@JarnoRFB
Copy link
Collaborator

Thanks for reporting! what would be the exception in Python 2?

@raphtown
Copy link
Author

Thanks for the response! Something like OSError I would assume?

See https://stackoverflow.com/questions/20790580/python-specifically-handle-file-exists-exception

@boeddeker
Copy link
Contributor

@onurgu made a fix in #372. It looks that you can only raise a sacred.utils.FileExistsError in py2 but not catch it.

@Qwlouse
Copy link
Collaborator

Qwlouse commented May 15, 2019

In theory the fix from #372 should work. @raphtown does the problem persist with sacred from the current master?

@boeddeker
Copy link
Contributor

As far as I know, except does something like an isinstance check and this check will fail, when a system function throws an OSError with the error code.
Here an example code to test this statement:

import sys
import os
# from sacred.utils import FileExistsError

if sys.version_info[0] == 2:
    import errno

    class FileExistsError(OSError):
        def __init__(self, msg):
            super(FileExistsError, self).__init__(errno.EEXIST, msg)

try:
	os.mkdir('dummy')
except FileExistsError:
	print('catched')
else:
	print('not catched')
$ python3 sacred_py2_FileExists.py  # create the dir
not catched
$ python3 sacred_py2_FileExists.py  # dir exists
catched
$ python2 sacred_py2_FileExists.py  #  dir exists
Traceback (most recent call last):
  File "sacred_py2_FileExists.py", line 16, in <module>
    os.mkdir('dummy')
OSError: [Errno 17] File exists: 'dummy'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants