Debian Codegraph @

brian2/parsing/dependencies.py

raw
1 import ast
2 from collections import namedtuple
3
4 from brian2.utils.stringtools import deindent
5
6 __all__ = ["abstract_code_dependencies"]
7
8
9 def get_read_write_funcs(parsed_code):
10 allids = set()
11 read = set()
12 write = set()
13 funcs = set()
14 for node in ast.walk(parsed_code):
15 if node.__class__ is ast.Name:
16 allids.add(node.id)
17 if node.ctx.__class__ is ast.Store:
18 write.add(node.id)
19 elif node.ctx.__class__ is ast.Load:
20 read.add(node.id)
21 else:
22 raise SyntaxError
23 elif node.__class__ is ast.Call:
24 funcs.add(node.func.id)
25
26 read = read - funcs
27
28 # check that there's no funky stuff going on with functions
29 if funcs.intersection(write):
30 raise SyntaxError("Cannot assign to functions in abstract code")
31
32 return allids, read, write, funcs
33
34
35 def abstract_code_dependencies(code, known_vars=None, known_funcs=None):
36 """
37 Analyses identifiers used in abstract code blocks
38
39 Parameters
40 ----------
41
42 code : str
43 The abstract code block.
44 known_vars : set
45 The set of known variable names.
46 known_funcs : set
47 The set of known function names.
48
49 Returns
50 -------
51
52 results : namedtuple with the following fields
53 ``all``
54 The set of all identifiers that appear in this code block,
55 including functions.
56 ``read``
57 The set of values that are read, excluding functions.
58 ``write``
59 The set of all values that are written to.
60 ``funcs``
61 The set of all function names.
62 ``known_all``
63 The set of all identifiers that appear in this code block and
64 are known.
65 ``known_read``
66 The set of known values that are read, excluding functions.
67 ``known_write``
68 The set of known values that are written to.
69 ``known_funcs``
70 The set of known functions that are used.
71 ``unknown_read``
72 The set of all unknown variables whose values are read. Equal
73 to ``read-known_vars``.
74 ``unknown_write``
75 The set of all unknown variables written to. Equal to
76 ``write-known_vars``.
77 ``unknown_funcs``
78 The set of all unknown function names, equal to
79 ``funcs-known_funcs``.
80 ``undefined_read``
81 The set of all unknown variables whose values are read before they
82 are written to. If this set is nonempty it usually indicates an
83 error, since a variable that is read should either have been
84 defined in the code block (in which case it will appear in
85 ``newly_defined``) or already be known.
86 ``newly_defined``
87 The set of all variable names which are newly defined in this
88 abstract code block.
89 """
90 if known_vars is None:
91 known_vars = set()
92 if known_funcs is None:
93 known_funcs = set()
94 if not isinstance(known_vars, set):
95 known_vars = set(known_vars)
96 if not isinstance(known_funcs, set):
97 known_funcs = set(known_funcs)
98
99 code = deindent(code, docstring=True)
100 parsed_code = ast.parse(code, mode="exec")
101
102 # Get the list of all variables that are read from and written to,
103 # ignoring the order
104 allids, read, write, funcs = get_read_write_funcs(parsed_code)
105
106 # Now check if there are any values that are unknown and read before
107 # they are written to
108 defined = known_vars.copy()
109 newly_defined = set()
110 undefined_read = set()
111 for line in parsed_code.body:
112 _, cur_read, cur_write, _ = get_read_write_funcs(line)
113 undef = cur_read - defined
114 undefined_read |= undef
115 newly_defined |= (cur_write - defined) - undefined_read
116 defined |= cur_write
117
118 # Return the results as a named tuple
119 results = dict(
120 all=allids,
121 read=read,
122 write=write,
123 funcs=funcs,
124 known_all=allids.intersection(known_vars.union(known_funcs)),
125 known_read=read.intersection(known_vars),
126 known_write=write.intersection(known_vars),
127 known_funcs=funcs.intersection(known_funcs),
128 unknown_read=read - known_vars,
129 unknown_write=write - known_vars,
130 unknown_funcs=funcs - known_funcs,
131 undefined_read=undefined_read,
132 newly_defined=newly_defined,
133 )
134 return namedtuple("AbstractCodeDependencies", list(results.keys()))(**results)