Skip to content

Commit f311149

Browse files
committed
Fix Automaton.graph() with indirection
1 parent 63838a3 commit f311149

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

scapy/automaton.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,20 +775,37 @@ def build_graph(self):
775775
s += se
776776

777777
for st in self.states.values():
778-
for n in st.atmt_origfunc.__code__.co_names + st.atmt_origfunc.__code__.co_consts: # noqa: E501
778+
names = list(
779+
st.atmt_origfunc.__code__.co_names +
780+
st.atmt_origfunc.__code__.co_consts
781+
)
782+
while names:
783+
n = names.pop()
779784
if n in self.states:
780-
s += '\t"%s" -> "%s" [ color=green ];\n' % (st.atmt_state, n) # noqa: E501
785+
s += '\t"%s" -> "%s" [ color=green ];\n' % (st.atmt_state, n)
786+
elif n in self.__dict__:
787+
# function indirection
788+
if callable(self.__dict__[n]):
789+
names.extend(self.__dict__[n].__code__.co_names)
790+
names.extend(self.__dict__[n].__code__.co_consts)
781791

782792
for c, k, v in ([("purple", k, v) for k, v in self.conditions.items()] + # noqa: E501
783793
[("red", k, v) for k, v in self.recv_conditions.items()] + # noqa: E501
784794
[("orange", k, v) for k, v in self.ioevents.items()]):
785795
for f in v:
786-
for n in f.__code__.co_names + f.__code__.co_consts:
796+
names = list(f.__code__.co_names + f.__code__.co_consts)
797+
while names:
798+
n = names.pop()
787799
if n in self.states:
788800
line = f.atmt_condname
789801
for x in self.actions[f.atmt_condname]:
790802
line += "\\l>[%s]" % x.__name__
791803
s += '\t"%s" -> "%s" [label="%s", color=%s];\n' % (k, n, line, c) # noqa: E501
804+
elif n in self.__dict__:
805+
# function indirection
806+
if callable(self.__dict__[n]):
807+
names.extend(self.__dict__[n].__code__.co_names)
808+
names.extend(self.__dict__[n].__code__.co_consts)
792809
for k, timers in self.timeout.items():
793810
for timer in timers:
794811
for n in (timer._func.__code__.co_names +

test/scapy/automaton.uts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,27 @@ graph = HelloWorld.build_graph()
446446
assert graph.startswith("digraph")
447447
assert '"BEGIN" -> "END"' in graph
448448

449+
= Automaton graph - with indirection
450+
~ automaton
451+
452+
class HelloWorld(Automaton):
453+
@ATMT.state(initial=1)
454+
def BEGIN(self):
455+
self.count1 = 0
456+
self.count2 = 0
457+
@ATMT.condition(BEGIN)
458+
def cnd_1(self):
459+
self.cnd_generic()
460+
def cnd_generic(self):
461+
raise END
462+
@ATMT.state(final=1)
463+
def END(self):
464+
pass
465+
466+
graph = HelloWorld.build_graph()
467+
assert graph.startswith("digraph")
468+
assert '"BEGIN" -> "END"' in graph
469+
449470
= TCP_client automaton
450471
~ automaton netaccess needs_root
451472
* This test retries on failure because it may fail quite easily

0 commit comments

Comments
 (0)