# Buggy code
def brian_object_exception(message, brianobj, original_exception):
    """
    Returns a `BrianObjectException` derived from the original exception.

    Creates a new class derived from the class of the original exception
    and `BrianObjectException`. This allows exception handling code to
    respond both to the original exception class and `BrianObjectException`.

    See `BrianObjectException` for arguments and notes.
    """
    DerivedBrianObjectException = type(
        "BrianObjectException", (BrianObjectException, original_exception.__class__), {}
    )
    return DerivedBrianObjectException(message, brianobj, original_exception)


# Traceback (SyntaxError)
Traceback (most recent call last):
File "err_test.py", line 5, in <module>
run(0*ms)
File "/home/marcel/programming/brian2/brian2/units/fundamentalunits.py", line 2360, in new_f
result = f(*args, **kwds)
File "/home/marcel/programming/brian2/brian2/core/magic.py", line 371, in run
namespace=namespace, profile=profile, level=2+level)
File "/home/marcel/programming/brian2/brian2/core/magic.py", line 231, in run
namespace=namespace, profile=profile, level=level+1)
File "/home/marcel/programming/brian2/brian2/core/base.py", line 278, in device_override_decorated_function
return func(*args, **kwds)
File "/home/marcel/programming/brian2/brian2/units/fundamentalunits.py", line 2360, in new_f
result = f(*args, **kwds)
File "/home/marcel/programming/brian2/brian2/core/network.py", line 951, in run
self.before_run(namespace)
File "/home/marcel/programming/brian2/brian2/core/base.py", line 278, in device_override_decorated_function
return func(*args, **kwds)
File "/home/marcel/programming/brian2/brian2/core/network.py", line 843, in before_run
raise brian_object_exception("An error occurred when preparing an object.", obj, ex)
BrianObjectException: Original error and traceback:
Traceback (most recent call last):
File "/home/marcel/programming/brian2/brian2/core/network.py", line 841, in before_run
obj.before_run(run_namespace)
File "/home/marcel/programming/brian2/brian2/groups/group.py", line 1093, in before_run
self.update_abstract_code(run_namespace=run_namespace)
File "/home/marcel/programming/brian2/brian2/groups/neurongroup.py", line 267, in update_abstract_code
if not is_boolean_expression(code, variables):
File "/home/marcel/programming/brian2/brian2/parsing/expressions.py", line 64, in is_boolean_expression
mod = ast.parse(expr, mode='eval')
File "/home/marcel/anaconda2/envs/brian2/lib/python2.7/ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
`False
^
SyntaxError: unexpected EOF while parsing

Error encountered with object named "neurongroup_thresholder".
Object was created here (most recent call only, full details in debug log):
File "err_test.py", line 3, in <module>
g = NeuronGroup(1, '', threshold='`False')

An error occurred when preparing an object.   File "<unknown>", line 1

`False

^

SyntaxError: unexpected EOF while parsing
(See above for original error message and traceback.)

# Fixed code
def brian_object_exception(message, brianobj, original_exception):
    """
    Returns a `BrianObjectException` derived from the original exception.

    Creates a new class derived from the class of the original exception
    and `BrianObjectException`. This allows exception handling code to
    respond both to the original exception class and `BrianObjectException`.

    See `BrianObjectException` for arguments and notes.
    """
    DerivedBrianObjectException = type(
        "BrianObjectException", (BrianObjectException, original_exception.__class__), {}
    )
    new_exception = DerivedBrianObjectException(message, brianobj, original_exception)
    # Copy over all exception attributes
    for attribute in dir(original_exception):
        if attribute.startswith("_"):
            continue
        setattr(new_exception, attribute, getattr(original_exception, attribute))
    return new_exception