From dfb1a19dcf025ef100a14f30568c8d2dc1689026 Mon Sep 17 00:00:00 2001 From: Jeff Tratner Date: Thu, 15 Mar 2018 03:53:39 -0700 Subject: [PATCH] Clear pipfile cache when it seems like we might be mutating --- pipenv/core.py | 1 + pipenv/project.py | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index 00fe1661..3cb8b6cd 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -291,6 +291,7 @@ def ensure_pipfile(validate=True, skip_requirements=False): if validate and project.virtualenv_exists and not PIPENV_SKIP_VALIDATION: # Ensure that Pipfile is using proper casing. p = project.parsed_pipfile + p.clear_pipfile_cache() changed = ensure_proper_casing(pfile=p) # Write changes out to disk. if changed: diff --git a/pipenv/project.py b/pipenv/project.py index 51cfba08..d90ffc12 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -294,6 +294,9 @@ class Project(object): @property def parsed_pipfile(self): + """Parse Pipfile into a TOMLFile and cache it + + (call clear_pipfile_cache() afterwards if mutating)""" # Open the pipfile, read it into memory. with open(self.pipfile_location) as f: contents = f.read() @@ -302,9 +305,12 @@ class Project(object): if cache_key not in _cache.pipfile_cache: parsed = self._parse_pipfile(contents) _cache.pipfile_cache[cache_key] = parsed - # deepcopy likely unnecessary but why not avoid bugs? return _cache.pipfile_cache[cache_key] + def clear_pipfile_cache(self): + """Clear pipfile cache (e.g., so we can mutate parsed pipfile)""" + _cache.pipfile_cache.clear() + def _parse_pipfile(self, contents): # If any outline tables are present... if ('[packages.' in contents) or ('[dev-packages.' in contents): @@ -338,8 +344,10 @@ class Project(object): def _pipfile(self): """Pipfile divided by PyPI and external dependencies.""" pfile = self.parsed_pipfile + # mutation time! + self.clear_pipfile_cache() for section in ('packages', 'dev-packages'): - p_section = pfile.get(section, {}) + p_section = dict(pfile.get(section, {})) for key in list(p_section.keys()): # Normalize key name to PEP 423. norm_key = pep423_name(key) @@ -370,6 +378,7 @@ class Project(object): p['pipenv'] = settings # Write the changes to disk. self.write_toml(p) + self.clear_pipfile_cache() @property def _lockfile(self):