# Buggy code
def __init__(self):
    super(CPPStandaloneDevice, self).__init__()
    #: Dictionary mapping `ArrayVariable` objects to their globally
    #: unique name
    self.arrays = {}
    #: Dictionary mapping `ArrayVariable` objects to their value or to
    #: ``None`` if the value (potentially) depends on executed code. This
    #: mechanism allows to access state variables in standalone mode if
    #: their value is known at run time
    self.array_cache = {}
    #: List of all dynamic arrays
    #: Dictionary mapping `DynamicArrayVariable` objects with 1 dimension to
    #: their globally unique name
    self.dynamic_arrays = {}
    #: Dictionary mapping `DynamicArrayVariable` objects with 2 dimensions
    #: to their globally unique name
    self.dynamic_arrays_2d = {}
    #: List of all arrays to be filled with zeros (list of (var, varname) )
    self.zero_arrays = []
    #: List of all arrays to be filled with numbers (list of
    #: (var, varname, start) tuples
    self.arange_arrays = []

    #: Whether the simulation has been run
    self.has_been_run = False

    #: Whether a run should trigger a build
    self.build_on_run = False

    #: build options
    self.build_options = None

    #: The directory which contains the generated code and results
    self.project_dir = None

    #: Whether to generate profiling information (stored in an instance
    #: variable to be accessible during CodeObject generation)
    self.enable_profiling = False

    #: CodeObjects that use profiling (users can potentially enable
    #: profiling only for a subset of runs)
    self.profiled_codeobjects = []

    #: Dict of all static saved arrays
    self.static_arrays = {}

    self.code_objects = {}
    self.main_queue = []
    self.runfuncs = {}
    self.networks = []
    self.net_synapses = []
    self.static_array_specs = []
    self.report_func = ""
    self.synapses = []

    #: Code lines that have been manually added with `device.insert_code`
    #: Dictionary mapping slot names to lists of lines.
    #: Note that the main slot is handled separately as part of `main_queue`
    self.code_lines = {
        "before_start": [],
        "after_start": [],
        "before_end": [],
        "after_end": [],
    }

    self.clocks = set([])

    self.extra_compile_args = []
    self.define_macros = []
    self.headers = []
    self.include_dirs = ["brianlib/randomkit"]
    if sys.platform == "win32":
        self.include_dirs += [os.path.join(sys.prefix, "Library", "include")]
    else:
        self.include_dirs += [os.path.join(sys.prefix, "include")]
    self.library_dirs = ["brianlib/randomkit"]
    if sys.platform == "win32":
        self.library_dirs += [os.path.join(sys.prefix, "Library", "Lib")]
    else:
        self.library_dirs += [os.path.join(sys.prefix, "lib")]
    self.runtime_library_dirs = []
    if sys.platform.startswith("linux"):
        self.runtime_library_dirs += [os.path.join(sys.prefix, "lib")]
    self.run_environment_variables = {}
    if sys.platform.startswith("darwin"):
        if "DYLD_LIBRARY_PATH" in os.environ:
            dyld_library_path = (
                os.environ["DYLD_LIBRARY_PATH"] + ":" + os.path.join(sys.prefix, "lib")
            )
        else:
            dyld_library_path = os.path.join(sys.prefix, "lib")
        self.run_environment_variables["DYLD_LIBRARY_PATH"] = dyld_library_path
    self.libraries = []
    if sys.platform == "win32":
        self.libraries += ["advapi32"]
    self.extra_link_args = []
    self.writer = None


# Traceback (jinja2.exceptions.UndefinedError)
ERROR      Brian 2 encountered an unexpected error. If you think this is bug in Brian 2, please report this issue either to the mailing list at <http://groups.google.com/group/brian-development/>, or to the issue tracker at <https://github.com/brian-team/brian2/issues>. Please include this file with debug information in your report: /var/folders/_n/ml20bf7n0_g3sz165pd3nrjm0000gn/T/brian_debug_1uynejn5.log  Additionally, you can also include a copy of the script that was run, available at: /var/folders/_n/ml20bf7n0_g3sz165pd3nrjm0000gn/T/brian_script_m_84dmg4.py You can also include a copy of the redirected std stream outputs, available at /var/folders/_n/ml20bf7n0_g3sz165pd3nrjm0000gn/T/brian_stdout_d2yd1ugv.log and /var/folders/_n/ml20bf7n0_g3sz165pd3nrjm0000gn/T/brian_stderr_xkxqltw0.log Thanks! [brian2]
Traceback (most recent call last):
File "/Users/fpbatta/src/assembly_sequences/scripts/run_simulation_simple.py", line 69, in <module>
nn1 = network_sim()
File "/Users/fpbatta/src/assembly_sequences/scripts/run_simulation_simple.py", line 59, in network_sim
bb.get_device().build(directory='PETH_standalone', compile=True, run=True, debug=False)
File "/Users/fpbatta/.conda/envs/brian23/lib/python3.8/site-packages/brian2/devices/cpp_standalone/device.py", line 1265, in build
self.generate_objects_source(self.writer, self.arange_arrays,
File "/Users/fpbatta/.conda/envs/brian23/lib/python3.8/site-packages/brian2/devices/cpp_standalone/device.py", line 633, in generate_objects_source
arr_tmp = self.code_object_class().templater.objects(
File "/Users/fpbatta/.conda/envs/brian23/lib/python3.8/site-packages/brian2/codegen/templates.py", line 210, in __call__
return MultiTemplate(module)
File "/Users/fpbatta/.conda/envs/brian23/lib/python3.8/site-packages/brian2/codegen/templates.py", line 226, in __init__
s = autoindent_postfilter(str(f()))
File "/Users/fpbatta/.conda/envs/brian23/lib/python3.8/site-packages/jinja2/runtime.py", line 675, in __call__
return self._invoke(arguments, autoescape)
File "/Users/fpbatta/.conda/envs/brian23/lib/python3.8/site-packages/jinja2/runtime.py", line 679, in _invoke
rv = self._func(*arguments)
File "/Users/fpbatta/.conda/envs/brian23/lib/python3.8/site-packages/brian2/devices/cpp_standalone/templates/objects.cpp", line 122, in macro
if(f{{name}}.is_open())
File "/Users/fpbatta/.conda/envs/brian23/lib/python3.8/site-packages/jinja2/runtime.py", line 747, in _fail_with_undefined_error
raise self._undefined_exception(self._undefined_message)
jinja2.exceptions.UndefinedError: dict object has no element <DynamicArrayVariable(dimensions=Dimension(),  dtype=int32, scalar=False, constant=True, read_only=True)>

# Fixed code
def __init__(self):
    super(CPPStandaloneDevice, self).__init__()
    #: Dictionary mapping `ArrayVariable` objects to their globally
    #: unique name
    self.arrays = {}
    #: Dictionary mapping `ArrayVariable` objects to their value or to
    #: ``None`` if the value (potentially) depends on executed code. This
    #: mechanism allows to access state variables in standalone mode if
    #: their value is known at run time
    self.array_cache = {}
    #: List of all dynamic arrays
    #: Dictionary mapping `DynamicArrayVariable` objects with 1 dimension to
    #: their globally unique name
    self.dynamic_arrays = {}
    #: Dictionary mapping `DynamicArrayVariable` objects with 2 dimensions
    #: to their globally unique name
    self.dynamic_arrays_2d = {}
    #: List of all arrays to be filled with zeros (list of (var, varname) )
    self.zero_arrays = []
    #: List of all arrays to be filled with numbers (list of
    #: (var, varname, start) tuples
    self.arange_arrays = []

    #: Whether the simulation has been run
    self.has_been_run = False

    #: Whether a run should trigger a build
    self.build_on_run = False

    #: build options
    self.build_options = None

    #: The directory which contains the generated code and results
    self.project_dir = None

    #: Whether to generate profiling information (stored in an instance
    #: variable to be accessible during CodeObject generation)
    self.enable_profiling = False

    #: CodeObjects that use profiling (users can potentially enable
    #: profiling only for a subset of runs)
    self.profiled_codeobjects = []

    #: Dict of all static saved arrays
    self.static_arrays = {}

    self.code_objects = {}
    self.main_queue = []
    self.runfuncs = {}
    self.networks = set()
    self.static_array_specs = []
    self.report_func = ""
    self.synapses = []

    #: Code lines that have been manually added with `device.insert_code`
    #: Dictionary mapping slot names to lists of lines.
    #: Note that the main slot is handled separately as part of `main_queue`
    self.code_lines = {
        "before_start": [],
        "after_start": [],
        "before_end": [],
        "after_end": [],
    }

    self.clocks = set([])

    self.extra_compile_args = []
    self.define_macros = []
    self.headers = []
    self.include_dirs = ["brianlib/randomkit"]
    if sys.platform == "win32":
        self.include_dirs += [os.path.join(sys.prefix, "Library", "include")]
    else:
        self.include_dirs += [os.path.join(sys.prefix, "include")]
    self.library_dirs = ["brianlib/randomkit"]
    if sys.platform == "win32":
        self.library_dirs += [os.path.join(sys.prefix, "Library", "Lib")]
    else:
        self.library_dirs += [os.path.join(sys.prefix, "lib")]
    self.runtime_library_dirs = []
    if sys.platform.startswith("linux"):
        self.runtime_library_dirs += [os.path.join(sys.prefix, "lib")]
    self.run_environment_variables = {}
    if sys.platform.startswith("darwin"):
        if "DYLD_LIBRARY_PATH" in os.environ:
            dyld_library_path = (
                os.environ["DYLD_LIBRARY_PATH"] + ":" + os.path.join(sys.prefix, "lib")
            )
        else:
            dyld_library_path = os.path.join(sys.prefix, "lib")
        self.run_environment_variables["DYLD_LIBRARY_PATH"] = dyld_library_path
    self.libraries = []
    if sys.platform == "win32":
        self.libraries += ["advapi32"]
    self.extra_link_args = []
    self.writer = None