Skip to content

Commit

Permalink
Proper fix for flake8-bugbear warning B006
Browse files Browse the repository at this point in the history
The whole idea was to cache the result of a couple functions,
do that explictly with @functools.cache.
  • Loading branch information
DimitriPapadopoulos committed Jan 3, 2024
1 parent 8611d05 commit efb7997
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,18 @@ def get_provider(moduleOrReq):
return _find_adapter(_provider_factories, loader)(module)


def _macos_vers(_cache=None):
if not _cache:
version = platform.mac_ver()[0]
# fallback for MacPorts
if version == '':
plist = '/System/Library/CoreServices/SystemVersion.plist'
if os.path.exists(plist):
if hasattr(plistlib, 'readPlist'):
plist_content = plistlib.readPlist(plist)
if 'ProductVersion' in plist_content:
version = plist_content['ProductVersion']

_cache = [version.split('.')]
return _cache[0]
@functools.cache
def _macos_vers():
version = platform.mac_ver()[0]
# fallback for MacPorts
if version == '':
plist = '/System/Library/CoreServices/SystemVersion.plist'
if os.path.exists(plist):
if hasattr(plistlib, 'readPlist'):
plist_content = plistlib.readPlist(plist)
if 'ProductVersion' in plist_content:
version = plist_content['ProductVersion']
return version.split('.')


def _macos_arch(machine):
Expand Down Expand Up @@ -2422,13 +2420,9 @@ def _cygwin_patch(filename): # pragma: nocover
return os.path.abspath(filename) if sys.platform == 'cygwin' else filename


def _normalize_cached(filename, _cache=None):
_cache = _cache or {}
try:
return _cache[filename]
except KeyError:
_cache[filename] = result = normalize_path(filename)
return result
@functools.cache
def _normalize_cached(filename):
return normalize_path(filename)


def _is_egg_path(path):
Expand Down

0 comments on commit efb7997

Please sign in to comment.