Commit 29bc0218 authored by Stefan Behnel's avatar Stefan Behnel

fixed bug in Visitor cache, reduces lxml compile time by ~20%

parent f277fa1f
...@@ -16,24 +16,31 @@ class BasicVisitor(object): ...@@ -16,24 +16,31 @@ class BasicVisitor(object):
self.dispatch_table = {} self.dispatch_table = {}
def visit(self, obj): def visit(self, obj):
cls = obj.__class__ cls = type(obj)
m = self.dispatch_table.get(cls.__name__) try:
if m is None: handler_method = self.dispatch_table[cls]
except KeyError:
#print "Cache miss for class %s in visitor %s" % (
# cls.__name__, type(self).__name__)
# Must resolve, try entire hierarchy # Must resolve, try entire hierarchy
pattern = "visit_%s" pattern = "visit_%s"
mro = inspect.getmro(cls) mro = inspect.getmro(cls)
for cls in mro: for mro_cls in mro:
m = getattr(self, pattern % cls.__name__, None) try:
if m is not None: handler_method = getattr(self, pattern % mro_cls.__name__)
break break
except AttributeError:
pass
else: else:
print type(self), type(obj) print type(self), type(obj)
if hasattr(self, 'access_path'):
print self.access_path print self.access_path
print self.access_path[-1][0].pos print self.access_path[-1][0].pos
print self.access_path[-1][0].__dict__ print self.access_path[-1][0].__dict__
raise RuntimeError("Visitor does not accept object: %s" % obj) raise RuntimeError("Visitor does not accept object: %s" % obj)
self.dispatch_table[cls.__name__] = m #print "Caching " + cls.__name__
return m(obj) self.dispatch_table[cls] = handler_method
return handler_method(obj)
class TreeVisitor(BasicVisitor): class TreeVisitor(BasicVisitor):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment