ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Debugging ဆိုတာ စုံထောက်အလုပ်နဲ့ တူပါတယ်။ Program က မှားတဲ့ အဖြေထုတ်တာ ဒါမှမဟုတ် crash ဖြစ်တာက "ရာဇဝတ်မှု" ပါ။ စုံထောက်က သံသယရှိသူကို ချက်ချင်း ဖမ်းမယ့်အစား အထောက်အထားတွေ စုပါတယ် — ဘယ်အချိန်မှာ ဘာဖြစ်ခဲ့လဲ၊ ဘယ်သူ ဘယ်နေရာမှာ ရှိခဲ့လဲ ဆိုတာပါ။ Code မှာတော့ အဲဒီ အထောက်အထားတွေက variable value တွေ၊ type တွေနဲ့ function တွေ ခေါ်ခဲ့တဲ့ အစဉ်ပါ။
Python က အထောက်အထား စုဖို့ ကိရိယာတွေ ပေးထားပါတယ်။ Traceback က crash ဖြစ်တဲ့ နေရာကို ပြပါတယ်၊ print() နဲ့ logging က value တွေကို ပြပါတယ်၊ breakpoint() ကတော့ program ကို ခဏရပ်ပြီး အထဲကို ဝင်ကြည့်ခွင့် ပေးပါတယ်။ ကိရိယာတွေထက် ပိုအရေးကြီးတာက အစဉ်တကျ ရှာတဲ့ နည်းလမ်းပါ။
Problem → Why → Solution
ပြဿနာ: လက္ခဏာကိုပဲ ဖုံးလိုက်တဲ့ "fix"
Sales report တစ်ခုက ZeroDivisionError နဲ့ crash ဖြစ်တယ် ဆိုပါစို့။ အမြန်ဆုံး "ပြင်နည်း" က average() ထဲမှာ try/except ထည့်ပြီး 0 ပြန်ပေးလိုက်တာပါ။ Crash တော့ ပျောက်သွားပါတယ်၊ ဒါပေမယ့် report က ငွေပေးချေပြီးသား order နှစ်ခု ရှိနေရက်နဲ့ average 0 လို့ ပြနေပါတယ်။ Bug အစစ်က average() ထဲမှာ မဟုတ်ပါဘူး — data ထဲမှာ status က "Paid" နဲ့ "PAID" ဖြစ်နေပြီး filter က "paid" (အသေး) ကိုပဲ ရှာနေလို့ list အလွတ် ထွက်လာတာပါ။
def paid_amounts(orders):
return [o["amount"] for o in orders if o["status"] == "paid"]
def average(values):
try:
return sum(values) / len(values)
except ZeroDivisionError:
return 0 # "fixed": the crash is gone, the bug is not
orders = [{"status": "Paid", "amount": 40}, {"status": "PAID", "amount": 60}]
print(average(paid_amounts(orders))) # prints 0, the right answer is 50.0ဘာကြောင့် ဒီလို ဖြစ်ရတာလဲ
Error ပေါ်တဲ့ line နဲ့ bug စတင်တဲ့ line က မကြာခဏ မတူပါဘူး။ ZeroDivisionError က လက္ခဏာ (symptom) ပဲ ဖြစ်ပြီး မှားတဲ့ state (list အလွတ်) က အစောပိုင်း filter မှာ ဝင်လာတာပါ။ "ဘယ်မှာ crash ဖြစ်လဲ" ဆိုတာနဲ့ မရပ်ဘဲ "ဒီ value မှားက ဘယ်ကနေ လာတာလဲ" လို့ နောက်ပြန် လိုက်မေးမှ root cause ကို တွေ့ပါမယ်။
Core concept: ခန့်မှန်းတာ မဟုတ်ဘဲ အဆင့်ဆင့် ရှာတာ
Debugging ကို ထပ်ခါထပ်ခါ အသုံးချလို့ရတဲ့ အဆင့်ခြောက်ဆင့် process တစ်ခုအဖြစ် မြင်ကြည့်ပါ။ အဆင့်တိုင်းက ရှာရမယ့် နေရာကို ကျဉ်းသွားစေပါတယ်။ ကျော်သွားရင် နာရီပေါင်းများစွာ မှားတဲ့ file ထဲမှာ ရှာနေမိတတ်ပါတယ်။
1. Reproduce
Bug ကို အမြဲတမ်း ပြန်ဖြစ်အောင် လုပ်နိုင်တဲ့ input နဲ့ command ကို ရှာပါ။ ပြန်မဖြစ်နိုင်ရင် ပြင်ပြီးကြောင်းလည်း သက်သေမပြနိုင်ပါဘူး။
2. Minimize
Bug ပေါ်နေသေးတဲ့ အသေးဆုံး input ရရောက်တဲ့အထိ data နဲ့ code ကို ဖြတ်ချပါ။ Row တစ်ထောင်ထက် row နှစ်ကြောင်းက ပိုဖတ်ရလွယ်ပါတယ်။
3. Hypothesis
"filter က list အလွတ် ပြန်ပေးနေတယ်" လို စမ်းသပ်လို့ရတဲ့ ယူဆချက် တစ်ခုကို စာလုံးနဲ့ ရေးချပါ။
4. Evidence
print(f"{x=}")၊ logging ဒါမှမဟုတ် breakpoint() နဲ့ ယူဆချက်ကို စစ်ပါ။ Value တင်မကဘဲ type, caller နဲ့ data ဝင်လာတဲ့ နေရာကိုပါ ကြည့်ပါ။
5–6. Fix + regression test
Bug စတင်တဲ့ နေရာမှာ ပြင်ပြီး အရင်က fail ဖြစ်ခဲ့တဲ့ input နဲ့ test တစ်ခု ထည့်ပါ။ အဲဒီ test က နောင်တစ်ချိန် bug ပြန်ဝင်လာရင် ချက်ချင်း သတိပေးပါမယ်။
Evidence စုတဲ့ ကိရိယာ တစ်ခုစီမှာ ကိုယ်ပိုင် အလုပ် ရှိပါတယ်။ print(f"{numbers=}") (Python 3.8 ကစပြီး) က variable နာမည်နဲ့ value ကို တစ်ခါတည်း ပြလို့ ယာယီ စစ်ဆေးမှုအတွက် အမြန်ဆုံးပါ။ assert condition, "message" က "ဒီနေရာမှာ ဒါ မှန်ကိုမှန်ရမယ်" ဆိုတဲ့ ယူဆချက်ကို code ထဲ ရေးထားတာပါ၊ မှားရင် AssertionError နဲ့ ချက်ချင်း ရပ်ပါတယ်။ logging ကတော့ level (DEBUG, INFO, WARNING, ERROR) တွေ ရှိလို့ code ကို မဖျက်ဘဲ အသေးစိတ် message တွေကို အဖွင့်အပိတ် လုပ်လို့ရပါတယ်။
assert က input စစ်ဖို့ မဟုတ်ပါ
Python ကို -O flag နဲ့ run ရင် assert statement တွေ အကုန် ဖယ်ရှားခံရပါတယ်။ ဒါကြောင့် assert ကို developer ရဲ့ ယူဆချက်တွေ စစ်ဖို့ပဲ သုံးပါ။ User input ဒါမှမဟုတ် file data ကို စစ်ဖို့ကတော့ if နဲ့ စစ်ပြီး ValueError လို exception ကို raise လုပ်ပါ။
- Symptom ပေါ်တဲ့ line ကို ရှာ
- အဲဒီ value ဘယ်က လာလဲ မေး
- Caller တစ်ဆင့်ချင်း နောက်ပြန် လိုက်
- Value စမှားတဲ့ နေရာမှာ ပြင်
Crash ဖြစ်တဲ့ line က အဖြေ မဟုတ်ဘဲ ရှာဖွေမှုရဲ့ အစမှတ်ပဲ ဖြစ်ပါတယ်။
အထွေထွေ ပုံစံ
import logging
logging.basicConfig(level=logging.DEBUG) # show DEBUG and above
log = logging.getLogger(__name__)
print(f"{variable=}") # name=value, quick check
assert condition, "assumption that must hold"
log.debug("state: %s", value) # switchable detail
breakpoint() # pause here in pdbbasicConfig() က ဘယ် level ကစပြီး ပြမလဲ သတ်မှတ်ပါတယ်။ f"{variable=}" က နာမည်နဲ့ value ကို ပြပြီး assert က ယူဆချက်ကို စစ်ပါတယ်။ log.debug() က level ပိတ်ထားရင် ဘာမှ မထုတ်ပါဘူး၊ breakpoint() ကတော့ အဲဒီ line မှာ debugger ကို ဖွင့်ပါတယ်။
Example: ဘာကြောင့် ပျမ်းမျှ မတွက်နိုင်တာလဲ
ဒီ example မှာ calculate_average() ကို input နှစ်မျိုးနဲ့ ခေါ်ပါတယ်။ ပထမ list က number တွေမို့ အဆင်ပြေပါတယ်။ ဒုတိယ list ကတော့ file ထဲက ဖတ်လာတဲ့ value တွေလို string တွေ ဖြစ်နေလို့ TypeError ဖြစ်ပါတယ်။ Debug print နဲ့ log message တွေက ဘာကြောင့်လဲဆိုတာ ပြောပြပါတယ်။
Logging ရဲ့ default handler က stderr ကို ရေးပါတယ်။ ဒီမှာတော့ stream=sys.stdout ပေးထားလို့ log line တွေက print() output တွေနဲ့ အစဉ်အတိုင်း ပေါ်ပါတယ်။
import logging
import sys
# Send log records to stdout so they appear in order with print()
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout,
format="%(levelname)s:%(name)s:%(message)s")
log = logging.getLogger("stats")
def calculate_average(numbers):
print(f"{numbers=}") # debug print: name and value together
assert numbers, "numbers must not be empty"
total = sum(numbers)
log.debug("total=%s count=%s", total, len(numbers))
return total / len(numbers)
print(f"Average: {calculate_average([10, 20, 30, 40])}")
raw = ["10", "20", "30"] # values read from a file are strings
try:
calculate_average(raw)
except TypeError as err:
log.error("bad input %r: %s", raw, err)
print(f"{type(raw[0])=}")Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 5-6
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout,level=logging.DEBUG ပေးထားလို့ debug message တွေ ပေါ်ပါတယ်။ level=logging.INFO လို့ ပြောင်းလိုက်ရင် code တစ်လိုင်းမှ မဖျက်ဘဲ DEBUG line တွေ ပျောက်သွားပါမယ်။
- Line 11
print(f"{numbers=}") # debug print: name and value together= ပါတဲ့ f-string က numbers=['10', '20', '30'] လို နာမည်ရော value ရော ပြပါတယ်။ String တွေမှာ quote ပါနေတာက number မဟုတ်ကြောင်း ပထမဆုံး အထောက်အထားပါ။
- Line 12
assert numbers, "numbers must not be empty"List အလွတ်ဆိုရင် ZeroDivisionError အထိ မရောက်ခင် ဒီနေရာမှာ message ရှင်းရှင်းနဲ့ ရပ်ပါတယ်။ ဒါက developer ရဲ့ ယူဆချက်ကို စစ်တာပါ။
- Line 14
log.debug("total=%s count=%s", total, len(numbers))%s placeholder နဲ့ argument တွေကို သီးခြား ပေးထားလို့ DEBUG level ပိတ်ထားရင် string ပေါင်းတဲ့ အလုပ်ကို logging က မလုပ်တော့ပါဘူး။
- Lines 23-25
except TypeError as err:Exception ကို log.error() နဲ့ input (%r က repr ပြ) ပါ မှတ်တမ်းတင်ပြီး type(raw[0]) ကို ပြပါတယ်။ <class 'str'> ဆိုတာက root cause ပါ — data ဝင်လာတဲ့ နေရာမှာ int() ပြောင်းဖို့ လိုပါတယ်။
numbers=[10, 20, 30, 40]
DEBUG:stats:total=100 count=4
Average: 25.0
numbers=['10', '20', '30']
ERROR:stats:bad input ['10', '20', '30']: unsupported operand type(s) for +: 'int' and 'str'
type(raw[0])=<class 'str'>Traceback ကို အောက်ကနေ အပေါ် ဖတ်ပါ
Exception ကို ဘယ်သူမှ မဖမ်းရင် Python က traceback ကို print ထုတ်ပါတယ်။ အောက်ဆုံးလိုင်းက exception type နဲ့ message ဖြစ်ပြီး ဘာမှားလဲ ပြောပါတယ်။ အပေါ်က frame တွေက program စတဲ့ နေရာကနေ error ဖြစ်တဲ့ နေရာအထိ function ခေါ်ခဲ့တဲ့ လမ်းကြောင်းပါ၊ "most recent call last" ဆိုတဲ့အတိုင်း နောက်ဆုံး ခေါ်ခဲ့တဲ့ frame က အောက်ဆုံးမှာ ရှိပါတယ်။
Reading a traceback
-------------------
Traceback (most recent call last):
File "report.py", line 14, in <module> <- 3. who started the chain
print(calculate_average(raw))
File "report.py", line 5, in calculate_average
total = sum(numbers) <- 2. the line that failed
TypeError: unsupported operand type(s) for +: 'int' and 'str'
^-- 1. read this first: exception type + messagebreakpoint() နဲ့ pdb
Print statement ဆယ်ခု ထည့်နေမယ့်အစား breakpoint() တစ်ခုကို သံသယရှိတဲ့ line မတိုင်ခင် ထည့်လိုက်ပါ (Python 3.7 ကစပြီး)။ Program က အဲဒီမှာ ရပ်ပြီး (Pdb) prompt ပေါ်လာပါတယ်။ အဲဒီမှာ variable တိုင်းကို ကြည့်လို့ရ၊ expression တွေ run လို့ရပြီး တစ်လိုင်းချင်း ဆက်သွားလို့ ရပါတယ်။ PYTHONBREAKPOINT=0 environment variable နဲ့ run ရင် breakpoint() call တွေကို ကျော်သွားပါတယ်။
| Command | လုပ်ဆောင်ချက် |
|---|---|
| n (next) | လက်ရှိ line ကို run ပြီး နောက် line မှာ ရပ်၊ function ထဲ မဝင် |
| s (step) | ခေါ်ထားတဲ့ function ထဲ ဝင်သွား |
| p expr | expression တစ်ခုရဲ့ value ကို print |
| w (where) | လက်ရှိ call stack ကို ပြ |
| c (continue) | နောက် breakpoint အထိ ဆက် run |
| q (quit) | Debugger နဲ့ program ကို ရပ် |
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Production server ပေါ်မှာ breakpoint() နဲ့ ရပ်ထားလို့ မရပါဘူး၊ user တွေ စောင့်နေရမှာမို့ပါ။ အဲဒီမှာ logging က အဓိက evidence ဖြစ်လာပါတယ်။ Team တွေက INFO level နဲ့ request တိုင်းရဲ့ id, user နဲ့ ကြာချိန်ကို log လုပ်ထားပြီး ပြဿနာ ဖြစ်တဲ့အခါ အဲဒီ log တွေကို ရှာဖွေပါတယ်။ log.exception() ကို except block ထဲမှာ ခေါ်ရင် message နဲ့အတူ traceback အပြည့်အစုံကိုပါ သိမ်းပေးပါတယ်။
Bug report တစ်ခု ရောက်လာရင် ကျွမ်းကျင်တဲ့ developer တွေက code ကို ချက်ချင်း မပြင်ပါဘူး။ ပထမဆုံး bug ကို ပြန်ဖြစ်စေတဲ့ test တစ်ခု ရေးပြီး fail ဖြစ်တာကို မြင်အောင် လုပ်ပါတယ်။ ပြီးမှ ပြင်ပြီး test pass ဖြစ်တာ ကြည့်ပါတယ်။ အဲဒီ test က test suite ထဲမှာ ထာဝရ ကျန်နေလို့ refactor လုပ်ရင်း bug အဟောင်း ပြန်ဝင်လာတာကို CI က ဖမ်းပေးပါတယ်။ VS Code နဲ့ PyCharm ရဲ့ debugger တွေကလည်း pdb နဲ့ သဘောတရား တူပါတယ် — breakpoint ချ၊ step လုပ်၊ variable ကြည့်ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. except: pass နဲ့ error ကို မျိုချတာ
try: ... except: pass က program ကို ဆက် run စေပေမယ့် ဘာမှားခဲ့လဲ ဆိုတဲ့ အထောက်အထား အကုန်ကို ဖျက်ပစ်ပါတယ်။ ဖမ်းချင်တဲ့ exception type ကိုပဲ နာမည်တပ်ဖမ်းပြီး အနည်းဆုံး log.exception() နဲ့ မှတ်တမ်းတင်ပါ။
2. Error ပေါ်တဲ့ line မှာပဲ ပြင်တာ
Problem section က average() လိုပဲ crash ဖြစ်တဲ့ line ကို ကာကွယ်လိုက်ရုံနဲ့ data မှားက ဆက်စီးနေပါတယ်။ Value မှားက ဘယ်ကလာလဲ နောက်ပြန် လိုက်ပြီး စဝင်တဲ့ နေရာ (ဒီမှာဆိုရင် status ကို .lower() မလုပ်ထားတဲ့ filter) မှာ ပြင်ပါ။
3. Debug print တွေ ကျန်ခဲ့တာ
print(f"{x=}") တွေကို commit လုပ်လိုက်ရင် user output ထဲ ရောပါသွားပါတယ်။ ယာယီ print တွေကို ပြီးတာနဲ့ ဖျက်ပါ၊ ဆက်ထားချင်တဲ့ message ဆိုရင် log.debug() အဖြစ် ပြောင်းပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Code ကို မပြင်ခင် bug ကို ပြန်ဖြစ်စေတဲ့ test ကို အရင်ရေးပြီး fail ဖြစ်တာ မြင်အောင် လုပ်ပါ။
- တစ်ကြိမ်မှာ အပြောင်းအလဲ တစ်ခုပဲ လုပ်ပြီး ရလဒ်ကို စစ်ပါ၊ နှစ်ခု တစ်ပြိုင်တည်း ပြောင်းရင် ဘယ်ဟာက ပြင်ပေးလဲ မသိနိုင်ပါဘူး။
- Module တိုင်းမှာ logging.getLogger(__name__) နဲ့ logger သီးသန့် သုံးပါ၊ log line တွေမှာ ဘယ် module က လာလဲ မြင်ရပါတယ်။
Mini challenge
Mini challenge: Cart total bug ကို ရှာပါ
cart_total() က pen 3 ချောင်း (2 စီ) နဲ့ book 1 အုပ် (10) အတွက် 16 ပြန်ရမှာကို 10 ပဲ ပြန်ပေးနေပါတယ်။ Logging နဲ့ evidence စုပြီး bug ကို ရှာ၊ ပြင်ပြီး regression check တွေ ထည့်ပါ။
- logging ကို DEBUG level နဲ့ stdout ကို ရေးအောင် configure လုပ်ရမယ်
- Item တစ်ခုစီရဲ့ name, price, qty နဲ့ line total ကို log.debug ဒါမှမဟုတ် logging.debug နဲ့ ပြရမယ်
- Bug ကို ပြင်ပြီး Total: 16 ထွက်ရမယ်
- ဒီ cart နဲ့ cart အလွတ်အတွက် assert check နှစ်ခု ထည့်ရမယ်
# TODO: import logging and sys, then configure DEBUG logging to stdout
def cart_total(items):
total = 0
for item in items:
total = item["price"] * item["qty"] # the bug hides in this loop
# TODO: log the item's name, price, qty and line total
return total
cart = [
{"name": "pen", "price": 2, "qty": 3},
{"name": "book", "price": 10, "qty": 1},
]
print(f"Total: {cart_total(cart)}") # prints 10, expected 16
# TODO: once fixed, assert the result for this cart and for an empty cartရလဒ်: Program က ဒီလို print ထုတ်ရပါမယ်: DEBUG: pen: 2 x 3 = 6 DEBUG: book: 10 x 1 = 10 Total: 16 All regression checks passed
အရိပ်အမြွက် ကြည့်ရန်
Line total တွေကို log ထုတ်ကြည့်ရင် 6 နဲ့ 10 နှစ်ခုလုံး မှန်နေတာ တွေ့ပါမယ်။ ဒါဆိုရင် total ထဲကို ဘယ်လို ထည့်နေလဲ (= လား += လား) ကို ကြည့်ပါ။
အဖြေ ကြည့်ရန်
import logging
import sys
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout,
format="%(levelname)s: %(message)s")
def cart_total(items):
total = 0
for item in items:
line = item["price"] * item["qty"]
logging.debug("%s: %s x %s = %s", item["name"], item["price"], item["qty"], line)
total += line # the fix: add, do not overwrite
return total
cart = [
{"name": "pen", "price": 2, "qty": 3},
{"name": "book", "price": 10, "qty": 1},
]
result = cart_total(cart)
print(f"Total: {result}")
# Regression checks: the first one failed before the fix
assert result == 16, result
assert cart_total([]) == 0
print("All regression checks passed")Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Reproduce, minimize, hypothesis, evidence, fix, regression test ဆိုတဲ့ အစဉ်အတိုင်း debug လုပ်ပါ။
- Traceback ကို အောက်ဆုံး line ကနေ စဖတ်ပြီး crash ဖြစ်တဲ့ line ကနေ value မှားရဲ့ မူလနေရာကို နောက်ပြန် လိုက်ပါ။
- print(f"{x=}") က အမြန်စစ်ဖို့၊ logging က ဆက်ထားဖို့၊ breakpoint() က တစ်ဆင့်ချင်း ကြည့်ဖို့ပါ။
- assert က developer ယူဆချက်အတွက်ပဲ ဖြစ်ပြီး -O နဲ့ ပျောက်ပါတယ်၊ input ကို if နဲ့ exception နဲ့ စစ်ပါ။
The Python Tutorial — Python Software Foundation