Code instructions (or the lack of) that cause:
Coined in 1947 because of a literal bug jamming a relay of the Mark II Computer at Harvard University.
level=logging.DEBUGOnce completed, test for regressions!
Testing: increases confidence in program behaviour. Compare input, output with specification.
Debugging: finding cause for program error!
Error - best scenarioPython has many built-in Exceptions to better classify errors:
1/0list(range(10))[11]'3'/2def = 1They might prevent bugs, and provide a hint of it’s nature.
Ignoring errors, often creates silent bugs.
ABBA is palindrome? True
12345678987654321 is palindrome? True
123.321 is palindrome? True
abc is palindrome? True
abc is palindrome? True
['c', 'b', 'a'] ['c', 'b', 'a']
abc is palindrome? True
Mutability and aliasing.
'a' same object 'b'? True
'a' is equal to 'b'? True
'a' same object 'b'? False
'a' is equal to 'b'? True
'a' same object 'b'? True
'a' is equal to 'b'? True
Strings are immutable!
['a', 'b', 'c'] ['a', 'b', 'c']
['a', 'b', 'c'] ['c', 'b', 'a']
abc is palindrome? False
python interpreter worksbytecode (sequence of instruction for python VM)
bytecode is between source code and binaryThe AST representation for the expression print(3+4*5), could be like:
graph LR
P[<codespan>print</codespan>] --> Exp["Expression"]
Exp -->A
A[<h2>+</h2>] --> B("3")
A --> C[<b>*</b>]
C --> D("4")
C --> E("5")
python adds and removes items from the stack:
Idea of debuggers:
Your IDE will likely have a debugging option you can integrate
pudb - Terminal (TUI)pdb debugger IDE integrationLets take a look at this implementation of the Fibonacci sequence:
a and b at the end of the last iteration of the for loop.> ./fibonacci.py(1)<module>()
-> def fibonacci(n):
(Pdb) h
Documented commands (type help <topic>):
========================================
EOF c d h list q rv undisplay
a cl debug help ll quit s unt
alias clear disable ignore longlist r source until
args commands display interact n restart step up
b condition down j next return tbreak w
break cont enable jump p retval u whatis
bt continue exit l pp run unalias where
b/clear - create and remove breakpointsl - lists code surrounding the line being executedc - resumes the program execution (until a breakpoint is hit)p - prints the contents of an objects - steps into the current function callbt - prints a backtraceup/down - to go up or down on the call stackbytecodeYou can compile1/disassemble2 Python using python 🤯
Raw bytecode for 'print(3+4*5)': b'e\x00d\x00\x83\x01\x01\x00d\x01S\x00'
Disassembled:
1 0 LOAD_NAME 0 (print)
2 LOAD_CONST 0 (23)
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 1 (None)
10 RETURN_VALUE
strace1 - Linux syscall tracerstrace python3 writeloop.py
...
openat(AT_FDCWD, "/dev/urandom", O_RDONLY|O_CLOEXEC) = 4
fstat(4, {st_mode=S_IFCHR|0666, st_rdev=makedev(0x1, 0x9), ...}) = 0
read(4, "\16\273o\312v\270T\274\36\343\236k0\17}\222\242S\351\231\264\t\26\342SD\35e\203\213f_"..., 2496) = 2496
openat(AT_FDCWD, "example.txt", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = 5
fstat(5, {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
ioctl(5, TCGETS, 0x7ffd5154f660) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(5, 0, SEEK_CUR) = 0
lseek(5, 0, SEEK_CUR) = 0
write(5, "Hello 74!\n", 11) = 11
close(5) = 0
...
strace python3 writeloop.py 2>&1 | grep example.txt
openat(AT_FDCWD, "example.txt", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = 5
Topic 20 (in ch 3) of The pragmatic programmer UHH Library system | MPS ebooks | German ebook via UHH