nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: IDSIA/sacred
Pull request #425 (2019-02-26 16:00:36)

Improve get_handler exception msg
I have sometimes folders in a directory that are identical to named configs. The exception
```
Traceback (most recent call last):
...
  File ".../sacred/config/config_files.py", line 55, in get_handler
    return HANDLER_BY_EXT[extension]
KeyError: ''
```
does not help.

This PR improve the exception msg.


Patch:
diff --git a/sacred/config/config_files.py b/sacred/config/config_files.py
index 11ecc22..27a8498 100644
--- a/sacred/config/config_files.py
+++ b/sacred/config/config_files.py
@@ -35,7 +35,13 @@ if opt.has_yaml:
 
 def get_handler(filename):
     _, extension = os.path.splitext(filename)
-    return HANDLER_BY_EXT[extension]
+    try:
+        return HANDLER_BY_EXT[extension]
+    except KeyError:
+        raise ValueError(
+            'Configuration file "{}" has invalid or unsupported extension '
+            '"{}".'.format(filename, extension)
+        )
 
 
 def load_config_file(filename):


Test patch:
diff --git a/tests/test_config/test_config_files.py b/tests/test_config/test_config_files.py
index 181981b..0b79736 100644
--- a/tests/test_config/test_config_files.py
+++ b/tests/test_config/test_config_files.py
@@ -3,6 +3,7 @@
 from __future__ import division, print_function, unicode_literals
 
 import os
+import re
 import tempfile
 import pytest
 
@@ -35,3 +36,19 @@ def test_load_config_file(ext, handler):
     d = load_config_file(f_name)
     assert d == data
     os.remove(f_name)
+
+
+def test_load_config_file_exception_msg_invalid_ext():
+    handle, f_name = tempfile.mkstemp(suffix='.invalid')
+    f = os.fdopen(handle, "w")  # necessary for windows
+    f.close()
+    try:
+        exception_msg = re.compile(
+            'Configuration file ".*.invalid" has invalid or '
+            'unsupported extension ".invalid".'
+        )
+        with pytest.raises(ValueError) as excinfo:
+            load_config_file(f_name)
+        assert exception_msg.match(excinfo.value.args[0])
+    finally:
+        os.remove(f_name)