From 4a42866eee1d79b2d07c3fc5472d67f19929d077 2019-12-01 03:06:49 From: Matthias Bussonnier Date: 2019-12-01 03:06:49 Subject: [PATCH] More cleaning and enforcing of types. Also drop some reminder python 2 compatibility imports. And use some context manager when appropriate instead of manual closing --- diff --git a/IPython/paths.py b/IPython/paths.py index 73631b5..bbe3d5c 100644 --- a/IPython/paths.py +++ b/IPython/paths.py @@ -70,7 +70,7 @@ def get_ipython_dir() -> str: return ipdir -def get_ipython_cache_dir(): +def get_ipython_cache_dir() -> str: """Get the cache directory it is created if it does not exist.""" xdgdir = get_xdg_cache_dir() if xdgdir is None: @@ -81,13 +81,14 @@ def get_ipython_cache_dir(): elif not _writable_dir(xdgdir): return get_ipython_dir() - return py3compat.cast_unicode(ipdir, fs_encoding) + return ipdir -def get_ipython_package_dir(): +def get_ipython_package_dir() -> str: """Get the base directory where IPython itself is installed.""" ipdir = os.path.dirname(IPython.__file__) - return py3compat.cast_unicode(ipdir, fs_encoding) + assert isinstance(ipdir, str) + return ipdir def get_ipython_module_path(module_str): diff --git a/IPython/utils/path.py b/IPython/utils/path.py index f677f1c..5276f86 100644 --- a/IPython/utils/path.py +++ b/IPython/utils/path.py @@ -169,7 +169,7 @@ class HomeDirError(Exception): pass -def get_home_dir(require_writable=False): +def get_home_dir(require_writable=False) -> str: """Return the 'home' directory, as a unicode string. Uses os.path.expanduser('~'), and checks for writability. @@ -197,21 +197,18 @@ def get_home_dir(require_writable=False): if not _writable_dir(homedir) and os.name == 'nt': # expanduser failed, use the registry to get the 'My Documents' folder. try: - try: - import winreg as wreg # Py 3 - except ImportError: - import _winreg as wreg # Py 2 - key = wreg.OpenKey( + import winreg as wreg + 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 if (not require_writable) or _writable_dir(homedir): - return py3compat.cast_unicode(homedir, fs_encoding) + assert isinstance(homedir, str), "Homedir shoudl be unicode not bytes" + return homedir else: raise HomeDirError('%s is not a writable dir, ' 'set $HOME environment variable to override' % homedir)