# Buggy code
def run(self, module, post_check):
    """Execute the configured source code in a module and run any post
    checks.

    Args:
        module (Module) : a module to execute the configured code in.

        post_check(callable) : a function that can raise an exception
            if expected post-conditions are not met after code execution.

    """
    try:
        # Simulate the sys.path behaviour decribed here:
        #
        # https://docs.python.org/2/library/sys.html#sys.path
        _cwd = os.getcwd()
        _sys_path = list(sys.path)
        _sys_argv = list(sys.argv)
        sys.path.insert(0, os.path.dirname(self._path))
        sys.argv = [os.path.basename(self._path)] + self._argv

        exec(self._code, module.__dict__)
        post_check()

    except Exception as e:
        self._failed = True
        self._error_detail = traceback.format_exc()

        exc_type, exc_value, exc_traceback = sys.exc_info()
        filename, line_number, func, txt = traceback.extract_tb(exc_traceback)[-1]

        self._error = '%s\nFile "%s", line %d, in %s:\n%s' % (
            str(e),
            os.path.basename(filename),
            line_number,
            func,
            txt,
        )

    finally:
        # undo sys.path, CWD fixups
        os.chdir(_cwd)
        sys.path = _sys_path
        sys.argv = _sys_argv
        self.ran = True


# Traceback (Exception)
(venv) $ bokeh serve myapp.py
2018-06-27 12:59:45,343 Starting Bokeh server version 0.13.0 (running on Tornado 5.0.1)
2018-06-27 12:59:45,345 Bokeh app running at: http://localhost:5006/myapp
2018-06-27 12:59:45,345 Starting Bokeh server with process id: 13979
0.5604607908584933
2018-06-27 13:00:06,274 200 GET /myapp (::1) 96.25ms
2018-06-27 13:00:06,437 101 GET /myapp/ws?bokeh-protocol-version=1.0&amp;bokeh-session-id=OxhSy81ZBM8URWfk6wp7OTH7bTictF12u6pI8o7fkIUs (::1) 0.64ms
2018-06-27 13:00:06,437 WebSocket connection opened
2018-06-27 13:00:06,438 ServerConnection created
0.6452850662545722
0.2762922805351794
2018-06-27 13:00:11,120 Error running application handler <bokeh.application.handlers.script.ScriptHandler object at 0x7feae4861a58>: boom!
File "myapp.py", line 46, in <module>:
raise Exception('boom!') Traceback (most recent call last):
File "/home/matt/Clients/QEye/venv/lib/python3.5/site-packages/bokeh/application/handlers/code_runner.py", line 163, in run
exec(self._code, module.__dict__)
File "/home/matt/Clients/QEye/isolate/myapp.py", line 46, in <module>
raise Exception('boom!')
Exception: boom!

2018-06-27 13:00:11,125 200 GET /myapp (::1) 25.45ms
2018-06-27 13:00:11,132 WebSocket connection closed: code=1001, reason=None
2018-06-27 13:00:11,207 101 GET /myapp/ws?bokeh-protocol-version=1.0&amp;bokeh-session-id=Y5IlTpoIxVs42drth0tKZonyuwGnUNM3zKK6OAG5eg6n (::1) 0.60ms
2018-06-27 13:00:11,208 WebSocket connection opened
2018-06-27 13:00:11,208 ServerConnection created

# Fixed code
def run(self, module, post_check):
    """Execute the configured source code in a module and run any post
    checks.

    Args:
        module (Module) : a module to execute the configured code in.

        post_check(callable) : a function that can raise an exception
            if expected post-conditions are not met after code execution.

    """
    try:
        # Simulate the sys.path behaviour decribed here:
        #
        # https://docs.python.org/2/library/sys.html#sys.path
        _cwd = os.getcwd()
        _sys_path = list(sys.path)
        _sys_argv = list(sys.argv)
        sys.path.insert(0, os.path.dirname(self._path))
        sys.argv = [os.path.basename(self._path)] + self._argv

        exec(self._code, module.__dict__)
        post_check()

    except Exception as e:
        self._failed = True
        self._error_detail = traceback.format_exc()

        _exc_type, _exc_value, exc_traceback = sys.exc_info()
        filename, line_number, func, txt = traceback.extract_tb(exc_traceback)[-1]

        self._error = '%s\nFile "%s", line %d, in %s:\n%s' % (
            str(e),
            os.path.basename(filename),
            line_number,
            func,
            txt,
        )

    finally:
        # undo sys.path, CWD fixups
        os.chdir(_cwd)
        sys.path = _sys_path
        sys.argv = _sys_argv
        self.ran = True