Commits

Marius Gedminas committed c538a2c

Handle non-string __module__ everywhere in leak.py

Comments (0)

Files changed (2)

         rows = []
         for obj in gc.get_objects():
             objtype = type(obj)
-            if objtype.__module__ + "." + objtype.__name__ == typename:
+            if "%s.%s" % (objtype.__module__, objtype.__name__) == typename:
                 rows.append("<p class='obj'>%s</p>"
                             % ReferrerTree(obj, req).get_repr(obj))
         if not rows:
         for obj in all_objs:
             if id(obj) == objid:
                 objtype = type(obj)
-                if objtype.__module__ + "." + objtype.__name__ != typename:
+                if "%s.%s" % (objtype.__module__, objtype.__name__) != typename:
                     rows = ["<h3>The object you requested is no longer "
                             "of the correct type.</h3>"]
                 else:
         for obj in all_objs:
             if id(obj) == objid:
                 objtype = type(obj)
-                if objtype.__module__ + "." + objtype.__name__ != typename:
+                if "%s.%s" % (objtype.__module__, objtype.__name__) != typename:
                     rows = ["<h3>The object you requested is no longer "
                             "of the correct type.</h3>"]
                 else:
                 continue
 
             # Exclude all functions and classes from this module or reftree.
-            mod = getattr(ref, "__module__", "")
+            mod = str(getattr(ref, "__module__", ""))
             if mod is None:
                 continue
             if "dozer" in mod or "reftree" in mod or mod == '__main__':
     def get_repr(self, obj, referent=None):
         """Return an HTML tree block describing the given object."""
         objtype = type(obj)
-        typename = objtype.__module__ + "." + objtype.__name__
+        typename = "%s.%s" % (objtype.__module__, objtype.__name__)
         prettytype = typename.replace("__builtin__.", "")
 
         name = getattr(obj, "__name__", "")

dozer/tests/test_leak.py

 import unittest
 
+from webob import Request
+
 from dozer.leak import Dozer
+from dozer.leak import ReferrerTree
 
 
 class DozerForTests(Dozer):
 
 class EvilProxyClass(object):
 
+    some_constant = object()
+
     def __init__(self, obj):
         self.obj = obj
 
         evil_proxy = EvilProxyClass(object) # keep a reference to it
         dozer.tick()
 
+    def test_trace_all_handles_types_with_broken_module(self):
+        dozer = DozerForTests()
+        evil_proxy = EvilProxyClass(object) # keep a reference to it
+        dozer.trace_all(None, 'no-such-module.No-Such-Type')
+
+    def test_trace_one_handles_types_with_broken_module(self):
+        dozer = DozerForTests()
+        evil_proxy = EvilProxyClass(object) # keep a reference to it
+        dozer.trace_one(None, 'no-such-module.No-Such-Type', id(evil_proxy))
+
+    def test_tree_handles_types_with_broken_module(self):
+        dozer = DozerForTests()
+        evil_proxy = EvilProxyClass(object) # keep a reference to it
+        req = Request(dict(
+                PATH_INFO='/nosuchtype/%d' % id(evil_proxy),
+        ))
+        req.base_path = '/_dozer'
+        dozer.tree(req)
+
+
+class TestReferrerTree(unittest.TestCase):
+
+    def test_get_repr_handles_types_with_broken_module(self):
+        req = Request(dict(PATH_INFO='/whatevs'))
+        req.base_path = '/_dozer'
+        tree = ReferrerTree(None, req)
+        evil_proxy = EvilProxyClass(object)
+        tree.get_repr(evil_proxy)
+
+    def test_gen_handles_types_with_broken_module(self):
+        req = Request({'PATH_INFO': '/whatevs', 'wsgi.url_scheme': 'http',
+                       'HTTP_HOST': 'localhost'})
+        req.base_path = '/_dozer'
+        tree = ReferrerTree(None, req)
+        tree.maxdepth = 10
+        tree.seen = {}
+        list(tree._gen(EvilProxyClass.some_constant))
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.