Patchwork [1,of,6] templater: remove unused context argument from most resourcemapper functions

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 2, 2018, 7:42 a.m.
Message ID <abb6e1badc8635214d7b.1535874178@mimosa>
Download mbox | patch
Permalink /patch/34247/
State New
Headers show

Comments

Yuya Nishihara - Sept. 2, 2018, 7:42 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1528381674 -32400
#      Thu Jun 07 23:27:54 2018 +0900
# Node ID abb6e1badc8635214d7bd5ef1886fd8f346a97ae
# Parent  213c0493cea01a3d8f0693037e0b0e85860f2109
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.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -548,18 +548,18 @@  class templateresources(templater.resour
             '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 @@  class templateresources(templater.resour
             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 @@  class templateresources(templater.resour
     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 @@  class templateresources(templater.resour
         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,
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -548,7 +548,7 @@  class resourcemapper(object):
     __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 @@  class resourcemapper(object):
         """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 @@  class resourcemapper(object):
         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 @@  class engine(object):
         # 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 @@  class engine(object):
 
     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 @@  class engine(object):
     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)
diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -856,7 +856,7 @@  def runsymbol(context, mapping, key, def
         # 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