diff --git a/IPython/utils/path.py b/IPython/utils/path.py index d52cad0..f2f1ea0 100644 --- a/IPython/utils/path.py +++ b/IPython/utils/path.py @@ -198,12 +198,11 @@ def get_home_dir(require_writable=False) -> str: # expanduser failed, use the registry to get the 'My Documents' folder. try: import winreg as wreg - key = wreg.OpenKey( + with wreg.OpenKey( wreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" - ) - homedir = wreg.QueryValueEx(key,'Personal')[0] - key.Close() + ) as key: + homedir = wreg.QueryValueEx(key,'Personal')[0] except: pass diff --git a/IPython/utils/tests/test_path.py b/IPython/utils/tests/test_path.py index b34f9ca..987177b 100644 --- a/IPython/utils/tests/test_path.py +++ b/IPython/utils/tests/test_path.py @@ -171,8 +171,12 @@ def test_get_home_dir_8(): env.pop(key, None) class key: + def __enter__(self): + pass def Close(self): pass + def __exit__(*args, **kwargs): + pass with patch.object(wreg, 'OpenKey', return_value=key()), \ patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]):