Skip to content

Commit

Permalink
Merge pull request #27 from Josverl/traceback
Browse files Browse the repository at this point in the history
Show backtrace when a micropython exception occurs
  • Loading branch information
Josverl authored Jan 17, 2024
2 parents 23de01d + e375f09 commit 2ac0e58
Show file tree
Hide file tree
Showing 37 changed files with 781 additions and 598 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ scratch

.idea
.coverage
.coverage.*
107 changes: 94 additions & 13 deletions poetry.lock

Large diffs are not rendered by default.

33 changes: 31 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ exclude = ["**/tests/**", "**/*-test", "**/samples.py"]

[tool.poetry.dependencies]
python = "^3.8"
mpremote = "1.21.0"
loguru = "^0.7.0"
mpremote = "^1.22.0"
loguru = "^0.7.2"
ipympl = "^0.9.3"

[tool.poetry.extras]
Expand All @@ -43,6 +43,7 @@ bqplot = "^0.12.39"
numpy = "^1.24.2"
notebook = "^6.5.4"
pylance = "^0.4.3"
pytest-cov = "^4.1.0"


[tool.poetry.group.tools]
Expand Down Expand Up @@ -76,3 +77,31 @@ exclude = '''
| dist
)/
'''

[tool.pytest.ini_options]
addopts = "--cov=src/micropython_magic --cov-report html"

[tool.coverage.paths]
source = [
"src/",
]

[tool.coverage.run]
omit = [
"**/exp_magics.py",
]
relative_files = true
parallel = false
branch = true

[tool.coverage.html]
directory = "coverage"

[tool.coverage.xml]
output = "coverage/coverage.xml"

[tool.coverage.json]
output = "coverage/coverage.json"

[tool.coverage.lcov]
output = "coverage/coverage.lcov"
4 changes: 2 additions & 2 deletions samples/board_control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 1,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -372,7 +372,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
164 changes: 164 additions & 0 deletions samples/traceback.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook gives an example of how errors that may occur on the MCU are returned to the notebook.\n",
"\n",
"If an Error or Exception occurs on the MCU, the nodebook will receive a message with the error information.\n",
"The nodebook will then display the traceback and Error or Exeption.\n",
"The traceback is logged as one or more warnings, and the Exception or Error is logged as an error.\n",
"\n",
"- `File \"<stdin>\"` refers to the cell that was executed on the MCU.\n",
"- the line number and function / class names are also given.\n",
"- if a module is loaded from the MCU filesystem, filename is also logged.\n",
"\n",
"_Note 1:_ If the log level is set to DEBUG or TRACE additional information will be logged. \n",
"_Note 2:_ Use the `%xmode` magic to limit the amount of information that is displayed when an exception is raised.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MicroPythonMagic(Magics) options\n",
"------------------------------\n",
"MicroPythonMagic.loglevel=<UseEnum>\n",
" Choices: any of ['TRACE', 'DEBUG', 'INFO', 'WARNING', 'ERROR']\n",
" Current: <LogLevel.WARNING: 'WARNING'>\n",
"MicroPythonMagic.timeout=<Float>\n",
" Current: 300.0\n",
"MicroPythonMagic.xmode=<Unicode>\n",
" Current: 'Minimal'\n"
]
}
],
"source": [
"%config MicroPythonMagic.loglevel = \"WARNING\"\n",
"%config MicroPythonMagic"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"tags": [
"raises-exception"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current ip.InteractiveTB.mode='Minimal'\n"
]
},
{
"ename": "ConnectionError",
"evalue": "no device found",
"output_type": "error",
"traceback": [
"\u001b[1;31mConnectionError\u001b[0m\u001b[1;31m:\u001b[0m no device found\n"
]
}
],
"source": [
"# %%micropython\n",
"import sys\n",
"\n",
"\n",
"def lumberjack():\n",
" bright_side_of_death()\n",
"\n",
"\n",
"def bright_side_of_death():\n",
" return tuple()[0]\n",
"\n",
"\n",
"print(\"Hello World\")\n",
"lumberjack()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%micropython --writefile petstore.py --reset\n",
"\n",
"class Parrot:\n",
" def __init__(self):\n",
" self.state = 'ALIVE'\n",
"\n",
" def pinin_for_fjords(self):\n",
" self.state = 'DEAD'\n",
"\n",
" def is_pinin(self):\n",
" return self.state == 'DEAD'\n",
" \n",
" def feed(self):\n",
" self.state = 'ALIVE'\n",
" \n",
" def is_stiff(self):\n",
" return self.state in ['STIFF', 'DEAD']\n",
"\n",
"\n",
"class NorwegianBlue(Parrot):\n",
" def is_stiff(self):\n",
" raise AssertionError('I am not dead yet!')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"raises-exception"
]
},
"outputs": [],
"source": [
"# %%micropython\n",
"\n",
"from petstore import NorwegianBlue # type: ignore\n",
"\n",
"polly = NorwegianBlue()\n",
"\n",
"print(polly.state)\n",
"polly.pinin_for_fjords()\n",
"print(polly.state)\n",
"polly.feed()\n",
"polly.is_stiff()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
4 changes: 4 additions & 0 deletions src/micropython_magic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
from IPython.core.interactiveshell import InteractiveShell
from loguru import logger as log

from .logger import patch_MCUlog, set_log_level
from .magic_transformer import comment_magic_transformer
from .octarine import MicroPythonMagic

set_log_level("WARNING")
log.patch(patch_MCUlog)

# from .exp_magics import ExpMagics


Expand Down
2 changes: 1 addition & 1 deletion src/micropython_magic/exp_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from IPython.utils.text import LSString, SList
from loguru import logger as log

from micropython_magic.octarine import MicroPythonMagic, PrettyOutput
from micropython_magic.octarine import MicroPythonMagic


@magics_class
Expand Down
Loading

0 comments on commit 2ac0e58

Please sign in to comment.