Mercurial > hg
changeset 39582:28f974d83c0a
templater: remove unused context argument from most resourcemapper functions
While working on demand loading of ctx/fctx objects, I noticed that it's quite
easy to create infinite recursion by carelessly using the template context in
the resource mapper. Let's make that not happen.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Thu, 07 Jun 2018 23:27:54 +0900 |
parents | 68ce242c8b4b |
children | ee1e74ee037c |
files | mercurial/formatter.py mercurial/templater.py mercurial/templateutil.py |
diffstat | 3 files changed, 17 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/formatter.py Mon Sep 10 20:57:18 2018 +0900 +++ b/mercurial/formatter.py Thu Jun 07 23:27:54 2018 +0900 @@ -548,18 +548,18 @@ 'ui': ui, } - def availablekeys(self, context, mapping): + def availablekeys(self, mapping): return {k for k, g in self._gettermap.iteritems() - if g(self, context, mapping, k) is not None} + if g(self, mapping, k) is not None} def knownkeys(self): return self._knownkeys - def lookup(self, context, mapping, key): + def lookup(self, mapping, key): get = self._gettermap.get(key) if not get: return None - return get(self, context, mapping, key) + return get(self, mapping, key) def populatemap(self, context, origmapping, newmapping): mapping = {} @@ -571,7 +571,7 @@ mapping['originalnode'] = orignode return mapping - def _getsome(self, context, mapping, key): + def _getsome(self, mapping, key): v = mapping.get(key) if v is not None: return v @@ -580,7 +580,7 @@ def _hasctx(self, mapping): return 'ctx' in mapping or 'fctx' in mapping - def _getctx(self, context, mapping, key): + def _getctx(self, mapping, key): ctx = mapping.get('ctx') if ctx is not None: return ctx @@ -588,11 +588,11 @@ if fctx is not None: return fctx.changectx() - def _getrepo(self, context, mapping, key): - ctx = self._getctx(context, mapping, 'ctx') + def _getrepo(self, mapping, key): + ctx = self._getctx(mapping, 'ctx') if ctx is not None: return ctx.repo() - return self._getsome(context, mapping, key) + return self._getsome(mapping, key) _gettermap = { 'cache': _getsome,
--- a/mercurial/templater.py Mon Sep 10 20:57:18 2018 +0900 +++ b/mercurial/templater.py Thu Jun 07 23:27:54 2018 +0900 @@ -548,7 +548,7 @@ __metaclass__ = abc.ABCMeta @abc.abstractmethod - def availablekeys(self, context, mapping): + def availablekeys(self, mapping): """Return a set of available resource keys based on the given mapping""" @abc.abstractmethod @@ -556,7 +556,7 @@ """Return a set of supported resource keys""" @abc.abstractmethod - def lookup(self, context, mapping, key): + def lookup(self, mapping, key): """Return a resource for the key if available; otherwise None""" @abc.abstractmethod @@ -565,13 +565,13 @@ with the given new mapping""" class nullresourcemapper(resourcemapper): - def availablekeys(self, context, mapping): + def availablekeys(self, mapping): return set() def knownkeys(self): return set() - def lookup(self, context, mapping, key): + def lookup(self, mapping, key): return None def populatemap(self, context, origmapping, newmapping): @@ -618,7 +618,7 @@ # do not copy symbols which overrides the defaults depending on # new resources, so the defaults will be re-evaluated (issue5612) knownres = self._resources.knownkeys() - newres = self._resources.availablekeys(self, newmapping) + newres = self._resources.availablekeys(newmapping) mapping = {k: v for k, v in origmapping.iteritems() if (k in knownres # not a symbol per self.symbol() or newres.isdisjoint(self._defaultrequires(k)))} @@ -645,7 +645,7 @@ def availableresourcekeys(self, mapping): """Return a set of available resource keys based on the given mapping""" - return self._resources.availablekeys(self, mapping) + return self._resources.availablekeys(mapping) def knownresourcekeys(self): """Return a set of supported resource keys""" @@ -654,7 +654,7 @@ def resource(self, mapping, key): """Return internal data (e.g. cache) used for keyword/function evaluation""" - v = self._resources.lookup(self, mapping, key) + v = self._resources.lookup(mapping, key) if v is None: raise templateutil.ResourceUnavailable( _('template resource not available: %s') % key)
--- a/mercurial/templateutil.py Mon Sep 10 20:57:18 2018 +0900 +++ b/mercurial/templateutil.py Thu Jun 07 23:27:54 2018 +0900 @@ -856,7 +856,7 @@ # old templatekw: expand all keywords and resources # (TODO: drop support for old-style functions. 'f._requires = ()' # can be removed.) - props = {k: context._resources.lookup(context, mapping, k) + props = {k: context._resources.lookup(mapping, k) for k in context._resources.knownkeys()} # pass context to _showcompatlist() through templatekw._showlist() props['templ'] = context