prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Memory leak in RoutablePage
  When resolving/reversing URLs, `RoutablePage` calls Djangos `django.core.urlresolvers.get_resolver` function to build a resolver object.
  
  This function is wrapped in an unlimited lru cache. As each time we call it is usually with a different page instance, this lru cache would grow forever.
  
  I've not seen any issues caused by this in the wild, but worth fixing.
  
  
  </issue>
  <code>
  [start of wagtail/contrib/wagtailroutablepage/models.py]
  1 from __future__ import unicode_literals
  2 
  3 from six import string_types
  4 
  5 from django.http import Http404
  6 from django.core.urlresolvers import get_resolver
  7 from django.core.exceptions import ImproperlyConfigured
  8 
  9 from wagtail.wagtailcore.models import Page
  10 from wagtail.wagtailcore.url_routing import RouteResult
  11 
  12 
  13 class RoutablePageMixin(object):
  14     """
  15     This class can be mixed in to a Page subclass to allow urlconfs to be
  16     embedded inside pages.
  17     """
  18     #: Set this to a tuple of ``django.conf.urls.url`` objects.
  19     subpage_urls = None
  20 
  21     def reverse_subpage(self, name, args=None, kwargs=None):
  22         """
  23         This method does the same job as Djangos' built in "urlresolvers.reverse()" function for subpage urlconfs.
  24         """
  25         args = args or []
  26         kwargs = kwargs or {}
  27 
  28         if self.subpage_urls is None:
  29             raise ImproperlyConfigured("You must set 'subpage_urls' on " + type(self).__name__)
  30 
  31         resolver = get_resolver(self.subpage_urls)
  32         return resolver.reverse(name, *args, **kwargs)
  33 
  34     def resolve_subpage(self, path):
  35         """
  36         This finds a view method/function from a URL path.
  37         """
  38         if self.subpage_urls is None:
  39             raise ImproperlyConfigured("You must set 'subpage_urls' on " + type(self).__name__)
  40 
  41         resolver = get_resolver(self.subpage_urls)
  42         view, args, kwargs = resolver.resolve(path)
  43 
  44         # If view is a string, find it as an attribute of self
  45         if isinstance(view, string_types):
  46             view = getattr(self, view)
  47 
  48         return view, args, kwargs
  49 
  50     def route(self, request, path_components):
  51         """
  52         This hooks the subpage urls into Wagtails routing.
  53         """
  54         if self.live:
  55             try:
  56                 path = '/'
  57                 if path_components:
  58                     path += '/'.join(path_components) + '/'
  59 
  60                 view, args, kwargs = self.resolve_subpage(path)
  61                 return RouteResult(self, args=(view, args, kwargs))
  62             except Http404:
  63                 pass
  64 
  65         return super(RoutablePageMixin, self).route(request, path_components)
  66 
  67     def serve(self, request, view, args, kwargs):
  68         return view(request, *args, **kwargs)
  69 
  70     def serve_preview(self, request, mode_name):
  71         view, args, kwargs = self.resolve_subpage('/')
  72         return view(request, *args, **kwargs)
  73 
  74 
  75 class RoutablePage(RoutablePageMixin, Page):
  76     """
  77     This class extends Page by adding methods to allow urlconfs
  78     to be embedded inside pages
  79     """
  80 
  81     is_abstract = True
  82 
  83     class Meta:
  84         abstract = True
  85
  [end of wagtail/contrib/wagtailroutablepage/models.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/wagtail/contrib/wagtailroutablepage/models.py b/wagtail/contrib/wagtailroutablepage/models.py
  --- a/wagtail/contrib/wagtailroutablepage/models.py
  +++ b/wagtail/contrib/wagtailroutablepage/models.py
  @@ -3,8 +3,7 @@
   from six import string_types
   
   from django.http import Http404
  -from django.core.urlresolvers import get_resolver
  -from django.core.exceptions import ImproperlyConfigured
  +from django.core.urlresolvers import RegexURLResolver
   
   from wagtail.wagtailcore.models import Page
   from wagtail.wagtailcore.url_routing import RouteResult
  @@ -18,28 +17,36 @@
       #: Set this to a tuple of ``django.conf.urls.url`` objects.
       subpage_urls = None
   
  +    @classmethod
  +    def get_subpage_urls(cls):
  +        if cls.subpage_urls:
  +            return cls.subpage_urls
  +
  +        return ()
  +
  +    @classmethod
  +    def get_resolver(cls):
  +        if '_routablepage_urlresolver' not in cls.__dict__:
  +            subpage_urls = cls.get_subpage_urls()
  +            cls._routablepage_urlresolver = RegexURLResolver(r'^/', subpage_urls)
  +
  +        return cls._routablepage_urlresolver
  +
       def reverse_subpage(self, name, args=None, kwargs=None):
           """
  -        This method does the same job as Djangos' built in "urlresolvers.reverse()" function for subpage urlconfs.
  +        This method does the same job as Djangos' built in
  +        "urlresolvers.reverse()" function for subpage urlconfs.
           """
           args = args or []
           kwargs = kwargs or {}
   
  -        if self.subpage_urls is None:
  -            raise ImproperlyConfigured("You must set 'subpage_urls' on " + type(self).__name__)
  -
  -        resolver = get_resolver(self.subpage_urls)
  -        return resolver.reverse(name, *args, **kwargs)
  +        return self.get_resolver().reverse(name, *args, **kwargs)
   
       def resolve_subpage(self, path):
           """
           This finds a view method/function from a URL path.
           """
  -        if self.subpage_urls is None:
  -            raise ImproperlyConfigured("You must set 'subpage_urls' on " + type(self).__name__)
  -
  -        resolver = get_resolver(self.subpage_urls)
  -        view, args, kwargs = resolver.resolve(path)
  +        view, args, kwargs = self.get_resolver().resolve(path)
   
           # If view is a string, find it as an attribute of self
           if isinstance(view, string_types):
  
verification_info: {"golden_diff": "diff --git a/wagtail/contrib/wagtailroutablepage/models.py b/wagtail/contrib/wagtailroutablepage/models.py\n--- a/wagtail/contrib/wagtailroutablepage/models.py\n+++ b/wagtail/contrib/wagtailroutablepage/models.py\n@@ -3,8 +3,7 @@\n from six import string_types\n \n from django.http import Http404\n-from django.core.urlresolvers import get_resolver\n-from django.core.exceptions import ImproperlyConfigured\n+from django.core.urlresolvers import RegexURLResolver\n \n from wagtail.wagtailcore.models import Page\n from wagtail.wagtailcore.url_routing import RouteResult\n@@ -18,28 +17,36 @@\n     #: Set this to a tuple of ``django.conf.urls.url`` objects.\n     subpage_urls = None\n \n+    @classmethod\n+    def get_subpage_urls(cls):\n+        if cls.subpage_urls:\n+            return cls.subpage_urls\n+\n+        return ()\n+\n+    @classmethod\n+    def get_resolver(cls):\n+        if '_routablepage_urlresolver' not in cls.__dict__:\n+            subpage_urls = cls.get_subpage_urls()\n+            cls._routablepage_urlresolver = RegexURLResolver(r'^/', subpage_urls)\n+\n+        return cls._routablepage_urlresolver\n+\n     def reverse_subpage(self, name, args=None, kwargs=None):\n         \"\"\"\n-        This method does the same job as Djangos' built in \"urlresolvers.reverse()\" function for subpage urlconfs.\n+        This method does the same job as Djangos' built in\n+        \"urlresolvers.reverse()\" function for subpage urlconfs.\n         \"\"\"\n         args = args or []\n         kwargs = kwargs or {}\n \n-        if self.subpage_urls is None:\n-            raise ImproperlyConfigured(\"You must set 'subpage_urls' on \" + type(self).__name__)\n-\n-        resolver = get_resolver(self.subpage_urls)\n-        return resolver.reverse(name, *args, **kwargs)\n+        return self.get_resolver().reverse(name, *args, **kwargs)\n \n     def resolve_subpage(self, path):\n         \"\"\"\n         This finds a view method/function from a URL path.\n         \"\"\"\n-        if self.subpage_urls is None:\n-            raise ImproperlyConfigured(\"You must set 'subpage_urls' on \" + type(self).__name__)\n-\n-        resolver = get_resolver(self.subpage_urls)\n-        view, args, kwargs = resolver.resolve(path)\n+        view, args, kwargs = self.get_resolver().resolve(path)\n \n         # If view is a string, find it as an attribute of self\n         if isinstance(view, string_types):\n", "issue": "Memory leak in RoutablePage\nWhen resolving/reversing URLs, `RoutablePage` calls Djangos `django.core.urlresolvers.get_resolver` function to build a resolver object.\n\nThis function is wrapped in an unlimited lru cache. As each time we call it is usually with a different page instance, this lru cache would grow forever.\n\nI've not seen any issues caused by this in the wild, but worth fixing.\n\n", "before_files": [{"content": "from __future__ import unicode_literals\n\nfrom six import string_types\n\nfrom django.http import Http404\nfrom django.core.urlresolvers import get_resolver\nfrom django.core.exceptions import ImproperlyConfigured\n\nfrom wagtail.wagtailcore.models import Page\nfrom wagtail.wagtailcore.url_routing import RouteResult\n\n\nclass RoutablePageMixin(object):\n    \"\"\"\n    This class can be mixed in to a Page subclass to allow urlconfs to be\n    embedded inside pages.\n    \"\"\"\n    #: Set this to a tuple of ``django.conf.urls.url`` objects.\n    subpage_urls = None\n\n    def reverse_subpage(self, name, args=None, kwargs=None):\n        \"\"\"\n        This method does the same job as Djangos' built in \"urlresolvers.reverse()\" function for subpage urlconfs.\n        \"\"\"\n        args = args or []\n        kwargs = kwargs or {}\n\n        if self.subpage_urls is None:\n            raise ImproperlyConfigured(\"You must set 'subpage_urls' on \" + type(self).__name__)\n\n        resolver = get_resolver(self.subpage_urls)\n        return resolver.reverse(name, *args, **kwargs)\n\n    def resolve_subpage(self, path):\n        \"\"\"\n        This finds a view method/function from a URL path.\n        \"\"\"\n        if self.subpage_urls is None:\n            raise ImproperlyConfigured(\"You must set 'subpage_urls' on \" + type(self).__name__)\n\n        resolver = get_resolver(self.subpage_urls)\n        view, args, kwargs = resolver.resolve(path)\n\n        # If view is a string, find it as an attribute of self\n        if isinstance(view, string_types):\n            view = getattr(self, view)\n\n        return view, args, kwargs\n\n    def route(self, request, path_components):\n        \"\"\"\n        This hooks the subpage urls into Wagtails routing.\n        \"\"\"\n        if self.live:\n            try:\n                path = '/'\n                if path_components:\n                    path += '/'.join(path_components) + '/'\n\n                view, args, kwargs = self.resolve_subpage(path)\n                return RouteResult(self, args=(view, args, kwargs))\n            except Http404:\n                pass\n\n        return super(RoutablePageMixin, self).route(request, path_components)\n\n    def serve(self, request, view, args, kwargs):\n        return view(request, *args, **kwargs)\n\n    def serve_preview(self, request, mode_name):\n        view, args, kwargs = self.resolve_subpage('/')\n        return view(request, *args, **kwargs)\n\n\nclass RoutablePage(RoutablePageMixin, Page):\n    \"\"\"\n    This class extends Page by adding methods to allow urlconfs\n    to be embedded inside pages\n    \"\"\"\n\n    is_abstract = True\n\n    class Meta:\n        abstract = True\n", "path": "wagtail/contrib/wagtailroutablepage/models.py"}]}