Skip to content
Snippets Groups Projects
Commit a6803400 authored by Florian Ziemen's avatar Florian Ziemen
Browse files

simple python call with stack trace

to be continued...
parent 0a7b137e
No related branches found
No related tags found
1 merge request!79Draft: Rework debugging lecture to be python based
Pipeline #97778 passed
......@@ -2,7 +2,17 @@
title: "Debugging Strategies"
---
# Debugging vs. Testing {.leftalign}
# What are we looking at?
## Bugs make software misbehave
* Crash
* Freeze
* Bogus result
* Slightly wrong result
* Security vulnerability
* ...
## Debugging vs. Testing
**Testing:** Cover as many use cases as possible to make a program more robust
......@@ -11,87 +21,67 @@ title: "Debugging Strategies"
## Strategies covered in this lecture
- Include debug output/logging mechanisms
- Narrow down the code where the error occured (divide and conquer)
- Use a debugger
- Read the error message
- Include debug output/logging mechanisms
- Narrow down the code where the error occured (divide and conquer)
- Use a debugger
## Other strategies
- [Debugging compiled code](../../old_lectures/debugging-compiled-languages/)
- Search online for the error message / problem description
- Consider the last working state and focus on what changed since
- If changes are committed in a repo,
find the last valid commit (`git bisect`)
- Search online for this or similar issues
- Ask colleagues working with the same code
- Call for vendor support
## Locating the right error message {.leftalign}
::::::::{.columns .smaller}
- [Debugging compiled code](../../old_lectures/debugging-compiled-languages/)
:::{.column width=50%}
```python
def _extend_number(num):
return 10*num + (num % 10) -1
def extend_number(num):
try:
for idx in range(num):
num = _extend_number(num)
except:
num = extend_number(num)
return num
print(extend_number('6'))
```
Expected output:
## Reading an error message {.special}
Try this in python:
```python
import xarray as xr
xr.open_dataset ("/scrattch/k/k202134/test.nc")
```
6543210
```
:::
:::{.column width=50% .fragment}
Actual output (last 17 lines)
```
## Python output for open_dataset {.special}
```python
Traceback (most recent call last):
File "number_extension.py", line 6, in extend_number
for idx in range(num):
RecursionError: maximum recursion depth exceeded while calling a Python object
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/file_manager.py", line 210, in _acquire_with_cache_info
file = self._cache[self._key]
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/lru_cache.py", line 56, in __getitem__
value = self._cache[key]
KeyError: [<class 'netCDF4._netCDF4.Dataset'>, ('/scrattch/k/k202134/test.nc',), 'r', (('clobber', True), ('diskless', False), ('format', 'NETCDF4'), ('persist', False)), '14687e3e-f5b3-4c71-9879-3db9386888df']
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "number_extension.py", line 13, in <module>
print(extend_number('6'))
File "number_extension.py", line 9, in extend_number
num = extend_number(num)
File "number_extension.py", line 9, in extend_number
num = extend_number(num)
File "number_extension.py", line 9, in extend_number
num = extend_number(num)
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
File "<stdin>", line 1, in <module>
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/api.py", line 525, in open_dataset
backend_ds = backend.open_dataset(
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/netCDF4_.py", line 588, in open_dataset
store = NetCDF4DataStore.open(
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/netCDF4_.py", line 389, in open
return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose)
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/netCDF4_.py", line 336, in __init__
self.format = self.ds.data_model
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/netCDF4_.py", line 398, in ds
return self._acquire()
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/netCDF4_.py", line 392, in _acquire
with self._manager.acquire_context(needs_lock) as root:
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/contextlib.py", line 135, in __enter__
return next(self.gen)
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/file_manager.py", line 198, in acquire_context
file, cached = self._acquire_with_cache_info(needs_lock)
File "/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/lib/python3.10/site-packages/xarray/backends/file_manager.py", line 216, in _acquire_with_cache_info
file = self._opener(*self._args, **kwargs)
File "src/netCDF4/_netCDF4.pyx", line 2353, in netCDF4._netCDF4.Dataset.__init__
File "src/netCDF4/_netCDF4.pyx", line 1963, in netCDF4._netCDF4._ensure_nc_success
FileNotFoundError: [Errno 2] No such file or directory: b'/scrattch/k/k202134/test.nc'
```
<!--
issues in line 9 and 13
-->
:::
::::::::
## Hands-On! (5-10min) {.handson}
1. What are the actual issues in the code from the previous slide based on the error messages?
......@@ -101,19 +91,11 @@ issues in line 9 and 13
## Fixing run-time errors
- Produce sensible debug messages to determine code area with the issue
- Use proper compiler flags for debug output
- Use proper flags for debug output
- Check prerequisites/environment
- Create minimal working example
- Use debugger
## Prevent errors
- Testing!
- Using linters (static code analysis) like [ruff](https://github.com/astral-sh/ruff) for Python
- Even ensure proper formatting with tools like [black](https://github.com/psf/black) for Python
- Consider using an IDE with its tools
# Errors due to data access and communication
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment