Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print useful error message on exception #142

Merged
merged 2 commits into from
Apr 7, 2021

Conversation

certik
Copy link
Contributor

@certik certik commented Apr 7, 2021

Sometimes apparently the 'traceback' can be None, which results in throwing a TypeError exception.

With this fix the actual underlying error will get printed, so that users can fix it.

Fixes #141.

Sometimes apparently the 'traceback' can be None, which results in throwing a `TypeError` exception. 

With this fix the actual underlying error will get printed, so that users can fix it.

Fixes jupyter#141.
@certik
Copy link
Contributor Author

certik commented Apr 7, 2021

With this PR, I now get the following output:

(xeuslf) lfortran/lfortran(ast1) $ jupyter nbconvert --to notebook --execute --ExecutePreprocessor.timeout=60 --output Demo1_out.ipynb share/lfortran/nb/Demo1.ipynb
[NbConvertApp] Converting notebook share/lfortran/nb/Demo1.ipynb to notebook
Run with XEUS 1.0.1
[NbConvertApp] Executing notebook with kernel: fortran
Traceback (most recent call last):
  File "/Users/certik/miniforge3/envs/xeuslf/bin/jupyter-nbconvert", line 11, in <module>
    sys.exit(main())
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/jupyter_core/application.py", line 254, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/traitlets/config/application.py", line 845, in launch_instance
    app.start()
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/nbconvertapp.py", line 350, in start
    self.convert_notebooks()
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/nbconvertapp.py", line 524, in convert_notebooks
    self.convert_single_notebook(notebook_filename)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/nbconvertapp.py", line 489, in convert_single_notebook
    output, resources = self.export_single_notebook(notebook_filename, resources, input_buffer=input_buffer)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/nbconvertapp.py", line 418, in export_single_notebook
    output, resources = self.exporter.from_filename(notebook_filename, resources=resources)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/exporters/exporter.py", line 181, in from_filename
    return self.from_file(f, resources=resources, **kw)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/exporters/exporter.py", line 199, in from_file
    return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/exporters/notebook.py", line 32, in from_notebook_node
    nb_copy, resources = super().from_notebook_node(nb, resources, **kw)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/exporters/exporter.py", line 143, in from_notebook_node
    nb_copy, resources = self._preprocess(nb_copy, resources)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/exporters/exporter.py", line 318, in _preprocess
    nbc, resc = preprocessor(nbc, resc)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/preprocessors/base.py", line 47, in __call__
    return self.preprocess(nb, resources)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/preprocessors/execute.py", line 79, in preprocess
    self.execute()
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbclient/util.py", line 74, in wrapped
    return just_run(coro(*args, **kwargs))
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbclient/util.py", line 53, in just_run
    return loop.run_until_complete(coro)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbclient/client.py", line 553, in async_execute
    await self.async_execute_cell(
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/preprocessors/execute.py", line 123, in async_execute_cell
    cell, resources = self.preprocess_cell(cell, self.resources, cell_index)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbconvert/preprocessors/execute.py", line 146, in preprocess_cell
    cell = run_sync(NotebookClient.async_execute_cell)(self, cell, index, store_history=self.store_history)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbclient/util.py", line 74, in wrapped
    return just_run(coro(*args, **kwargs))
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbclient/util.py", line 53, in just_run
    return loop.run_until_complete(coro)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nest_asyncio.py", line 70, in run_until_complete
    return f.result()
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/asyncio/futures.py", line 201, in result
    raise self._exception
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/asyncio/tasks.py", line 256, in __step
    result = coro.send(None)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbclient/client.py", line 857, in async_execute_cell
    self._check_raise_for_error(cell, exec_reply)
  File "/Users/certik/miniforge3/envs/xeuslf/lib/python3.9/site-packages/nbclient/client.py", line 760, in _check_raise_for_error
    raise CellExecutionError.from_cell_and_msg(cell, exec_reply_content)
nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell:
------------------
integer :: i
do i = 1, 4
    if (i == 3) cycle
    print *, "variable i =", i
end do
------------------


CompilerError: input:1:1 semantic error: Symbol already declared

^~~~~~~~~~~~~

Which is an error coming from our LFortran Jupyter kernel, and that is our problem to fix. But at least we know what the problem is, which before was hard to figure out.

@davidbrochart
Copy link
Member

Sometimes apparently the 'traceback' can be None, which results in throwing a TypeError exception.

I suppose you are using a xeus-based kernel, @JohanMabille do you know why we would have an error without a traceback?

Co-authored-by: David Brochart <david.brochart@gmail.com>
@certik
Copy link
Contributor Author

certik commented Apr 7, 2021

Yes, we use the xeus based kernel. Great suggestion to simplify the code, I was thinking there is some one liner, but couldn't think of any. :)

@davidbrochart davidbrochart merged commit 0c08e27 into jupyter:master Apr 7, 2021
@davidbrochart
Copy link
Member

Thanks @certik!

@JohanMabille
Copy link
Member

@JohanMabille do you know why we would have an error without a traceback?

Returning the traceback in the reply message must be implemented in the kernel itself since it is specific to the interpreter / compiler used.

@davidbrochart
Copy link
Member

Would it make sense to have an empty list by default in xeus?

@davidbrochart
Copy link
Member

@certik do you need an nbclient release soon?

@certik certik deleted the patch-1 branch April 7, 2021 16:14
@certik
Copy link
Contributor Author

certik commented Apr 7, 2021

Here is where we (currently) return an empty traceback:

https://gitlab.com/lfortran/lfortran/-/blob/3b2272ce6b6b9d05b237a537d546d52602e58fe3/src/lfortran/fortran_kernel.cpp#L251

result["traceback"] = {};

What should we do instead?

@davidbrochart, no I don't need a release soon. I can fix our bug locally and then everything works. I just submitted this PR for the future once we hit another error.

@davidbrochart
Copy link
Member

result["traceback"] = {};

Shouldn't it be an empty array?

result["traceback"] = json::array();

@certik
Copy link
Contributor Author

certik commented Apr 7, 2021

Thanks for the suggestion. I actually don't know. @JohanMabille, is that the case?

@JohanMabille
Copy link
Member

Indeed, the traceback is supposed to be an array when the status is 'error'. You can find more detail in the protocol documentation.

@JohanMabille
Copy link
Member

I actually wonder if we should provide functions for building the messages in xeus, with the expected arguments (and their types), when the reply has to be built in the kernel itself. This would help kernel authors to focus on the interpreter / language features without having to check how to "finish" to implement the protocol.

@certik
Copy link
Contributor Author

certik commented Apr 8, 2021

Thanks, I'll change it to an array. Yes, that would be really helpful to provide more helper routines --- I really struggled to "finish" the protocol, and as you can see, even got it wrong.

@certik
Copy link
Contributor Author

certik commented May 13, 2021

I tested using the nl::json::array() instead of {} and it works! I implemented it in: https://gitlab.com/lfortran/lfortran/-/merge_requests/927.

@blink1073 blink1073 added the bug Something isn't working label Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: can only join an iterable
4 participants