Commit e80bbb3e authored by Stefan Behnel's avatar Stefan Behnel

Avoid "__name__" hack in "Method" scanner action class and replace it with a...

Avoid "__name__" hack in "Method" scanner action class and replace it with a simple "__repr__()" implementation.
parent ba9ea5f7
# cython: language_level=3
cdef class Action:
cdef perform(self, token_stream, text)
......@@ -14,9 +15,8 @@ cdef class Call(Action):
cpdef same_as(self, other)
cdef class Method(Action):
cdef object name
cdef str name
cdef dict kwargs
cdef readonly object __name__ # for tracing the scanner
cdef class Begin(Action):
cdef object state_name
......
......@@ -67,13 +67,18 @@ class Method(Action):
def __init__(self, name, **kwargs):
self.name = name
self.kwargs = kwargs or None
self.__name__ = name # for Plex tracing
def perform(self, token_stream, text):
method = getattr(token_stream, self.name)
# self.kwargs is almost always unused => avoid call overhead
return method(text, **self.kwargs) if self.kwargs is not None else method(text)
def __repr__(self):
kwargs = (
', '.join(sorted(['%s=%r' % item for item in self.kwargs.items()]))
if self.kwargs is not None else '')
return "Method(%s%s%s)" % (self.name, ', ' if kwargs else '', kwargs)
def same_as(self, other):
return isinstance(other, Method) and self.name == other.name and self.kwargs == other.kwargs
......
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