{"id":166631,"date":"2019-12-26T08:19:44","date_gmt":"2019-12-26T00:19:44","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=166631"},"modified":"2019-12-13T13:20:49","modified_gmt":"2019-12-13T05:20:49","slug":"python-code-debugging","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-code-debugging.html","title":{"rendered":"Python \u4ee3\u7801\u8c03\u8bd5\u2014\u4f7f\u7528 pdb \u8c03\u8bd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\npdb \u662f python \u81ea\u5e26\u7684\u4e00\u4e2a\u5305\uff0c\u4e3a python \u7a0b\u5e8f\u63d0\u4f9b\u4e86\u4e00\u79cd\u4ea4\u4e92\u7684\u6e90\u4ee3\u7801\u8c03\u8bd5\u529f\u80fd\uff0c\u4e3b\u8981\u7279\u6027\u5305\u62ec\u8bbe\u7f6e\u65ad\u70b9\u3001\u5355\u6b65\u8c03\u8bd5\u3001\u8fdb\u5165\u51fd\u6570\u8c03\u8bd5\u3001\u67e5\u770b\u5f53\u524d\u4ee3\u7801\u3001\u67e5\u770b\u6808\u7247\u6bb5\u3001\u52a8\u6001\u6539\u53d8\u53d8\u91cf\u7684\u503c\u7b49\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e0b\u9762\u7ed3\u5408\u5177\u4f53\u7684\u5b9e\u4f8b\u8bb2\u8ff0\u5982\u4f55\u4f7f\u7528 pdb \u8fdb\u884c\u8c03\u8bd5\u3002
\n<\/strong><\/div>\n
\u6e05\u5355 1. \u6d4b\u8bd5\u4ee3\u7801\u793a\u4f8b<\/strong><\/span><\/div>\n
import pdb\r\na = \"aaa\"\r\npdb.set_trace()\r\nb = \"bbb\"\r\nc = \"ccc\"\r\nfinal = a + b + c\r\nprint final<\/pre>\n

\u5f00\u59cb\u8c03\u8bd5\uff1a\u76f4\u63a5\u8fd0\u884c\u811a\u672c\uff0c\u4f1a\u505c\u7559\u5728 pdb.set_trace() \u5904\uff0c\u9009\u62e9 n+enter \u53ef\u4ee5\u6267\u884c\u5f53\u524d\u7684 statement\u3002\u5728\u7b2c\u4e00\u6b21\u6309\u4e0b\u4e86 n+enter \u4e4b\u540e\u53ef\u4ee5\u76f4\u63a5\u6309 enter \u8868\u793a\u91cd\u590d\u6267\u884c\u4e0a\u4e00\u6761 debug \u547d\u4ee4\u3002<\/p>\n

\u6e05\u5355 2. \u5229\u7528 pdb \u8c03\u8bd5<\/strong><\/span><\/div>\n
[root@rcc-pok-idg-2255 ~]# python epdb1.py\r\n> \/root\/epdb1.py(4)?()\r\n-> b = \"bbb\"\r\n(Pdb) n\r\n> \/root\/epdb1.py(5)?()\r\n-> c = \"ccc\"\r\n(Pdb)\r\n> \/root\/epdb1.py(6)?()\r\n-> final = a + b + c\r\n(Pdb) list\r\n1 import pdb\r\n2 a = \"aaa\"\r\n3 pdb.set_trace()\r\n4 b = \"bbb\"\r\n5 c = \"ccc\"\r\n6 -> final = a + b + c\r\n7 print final\r\n[EOF]\r\n(Pdb)\r\n[EOF]\r\n(Pdb) n\r\n> \/root\/epdb1.py(7)?()\r\n-> print final\r\n(Pdb)<\/pre>\n

\u9000\u51fa debug\uff1a\u4f7f\u7528 quit \u6216\u8005 q \u53ef\u4ee5\u9000\u51fa\u5f53\u524d\u7684 debug\uff0c\u4f46\u662f quit \u4f1a\u4ee5\u4e00\u79cd\u975e\u5e38\u7c97\u9c81\u7684\u65b9\u5f0f\u9000\u51fa\u7a0b\u5e8f\uff0c\u5176\u7ed3\u679c\u662f\u76f4\u63a5 crash\u3002<\/p>\n

\u6e05\u5355 3. \u9000\u51fa debug<\/strong><\/span><\/div>\n
[root@rcc-pok-idg-2255 ~]# python epdb1.py\r\n> \/root\/epdb1.py(4)?()\r\n-> b = \"bbb\"\r\n(Pdb) n\r\n> \/root\/epdb1.py(5)?()\r\n-> c = \"ccc\"\r\n(Pdb) q\r\nTraceback (most recent call last):\r\nFile \"epdb1.py\", line 5, in ?\r\nc = \"ccc\"\r\nFile \"epdb1.py\", line 5, in ?\r\nc = \"ccc\"\r\nFile \"\/usr\/lib64\/python2.4\/bdb.py\", line 48, in trace_dispatch\r\nreturn self.dispatch_line(frame)\r\nFile \"\/usr\/lib64\/python2.4\/bdb.py\", line 67, in dispatch_line\r\nif self.quitting: raise BdbQuit\r\nbdb.BdbQuit<\/pre>\n

\u6253\u5370\u53d8\u91cf\u7684\u503c\uff1a\u5982\u679c\u9700\u8981\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u6253\u5370\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 p \u52a0\u4e0a\u53d8\u91cf\u540d\uff0c\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f\u6253\u5370\u4ec5\u4ec5\u5728\u5f53\u524d\u7684 statement \u5df2\u7ecf\u88ab\u6267\u884c\u4e86\u4e4b\u540e\u624d\u80fd\u770b\u5230\u5177\u4f53\u7684\u503c\uff0c\u5426\u5219\u4f1a\u62a5 NameError: < exceptions.NameError \u2026 ....> \u9519\u8bef\u3002<\/p>\n

\u6e05\u5355 4. debug \u8fc7\u7a0b\u4e2d\u6253\u5370\u53d8\u91cf<\/strong><\/span><\/div>\n
[root@rcc-pok-idg-2255 ~]# python epdb1.py\r\n> \/root\/epdb1.py(4)?()\r\n-> b = \"bbb\"\r\n(Pdb) n\r\n> \/root\/epdb1.py(5)?()\r\n-> c = \"ccc\"\r\n(Pdb) p b\r\n'bbb'\r\n(Pdb)\r\n'bbb'\r\n(Pdb) n\r\n> \/root\/epdb1.py(6)?()\r\n-> final = a + b + c\r\n(Pdb) p c\r\n'ccc'\r\n(Pdb) p final\r\n*** NameError:\r\n(Pdb) n\r\n> \/root\/epdb1.py(7)?()\r\n-> print final\r\n(Pdb) p final\r\n'aaabbbccc'\r\n(Pdb)<\/pre>\n

\u4f7f\u7528 c \u53ef\u4ee5\u505c\u6b62\u5f53\u524d\u7684 debug \u4f7f\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u3002\u5982\u679c\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u7ee7\u7eed\u6709 set_statement() \u7684\u7533\u660e\uff0c\u5219\u53c8\u4f1a\u91cd\u65b0\u8fdb\u5165\u5230 debug \u7684\u72b6\u6001\uff0c\u8bfb\u8005\u53ef\u4ee5\u5728\u4ee3\u7801 print final \u4e4b\u524d\u518d\u52a0\u4e0a set_trace() \u9a8c\u8bc1\u3002<\/p>\n

\u6e05\u5355 5. \u505c\u6b62 debug \u7ee7\u7eed\u6267\u884c\u7a0b\u5e8f<\/strong><\/span><\/div>\n
[root@rcc-pok-idg-2255 ~]# python epdb1.py\r\n> \/root\/epdb1.py(4)?()\r\n-> b = \"bbb\"\r\n(Pdb) n\r\n> \/root\/epdb1.py(5)?()\r\n-> c = \"ccc\"\r\n(Pdb) c\r\naaabbbccc<\/pre>\n

\u663e\u793a\u4ee3\u7801\uff1a\u5728 debug \u7684\u65f6\u5019\u4e0d\u4e00\u5b9a\u80fd\u8bb0\u4f4f\u5f53\u524d\u7684\u4ee3\u7801\u5757\uff0c\u5982\u8981\u8981\u67e5\u770b\u5177\u4f53\u7684\u4ee3\u7801\u5757\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 list \u6216\u8005 l \u547d\u4ee4\u663e\u793a\u3002list \u4f1a\u7528\u7bad\u5934 -> \u6307\u5411\u5f53\u524d debug \u7684\u8bed\u53e5\u3002<\/p>\n

\u6e05\u5355 6. debug \u8fc7\u7a0b\u4e2d\u663e\u793a\u4ee3\u7801<\/strong><\/span><\/div>\n
[root@rcc-pok-idg-2255 ~]# python epdb1.py\r\n> \/root\/epdb1.py(4)?()\r\n-> b = \"bbb\"\r\n(Pdb) list\r\n1 import pdb\r\n2 a = \"aaa\"\r\n3 pdb.set_trace()\r\n4 -> b = \"bbb\"\r\n5 c = \"ccc\"\r\n6 final = a + b + c\r\n7 pdb.set_trace()\r\n8 print final\r\n[EOF]\r\n(Pdb) c\r\n> \/root\/epdb1.py(8)?()\r\n-> print final\r\n(Pdb) list\r\n3 pdb.set_trace()\r\n4 b = \"bbb\"\r\n5 c = \"ccc\"\r\n6 final = a + b + c\r\n7 pdb.set_trace()\r\n8 -> print final\r\n[EOF]\r\n(Pdb)<\/pre>\n

\u5728\u4f7f\u7528\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c debug<\/p>\n

\u6e05\u5355 7. \u4f7f\u7528\u51fd\u6570\u7684\u4f8b\u5b50<\/strong><\/span><\/div>\n
import pdb\r\ndef combine(s1,s2): # define subroutine combine, which...\r\ns3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...\r\ns3 = '\"' + s3 +'\"' # encloses it in double quotes,...\r\nreturn s3 # and returns it.\r\na = \"aaa\"\r\npdb.set_trace()\r\nb = \"bbb\"\r\nc = \"ccc\"\r\nfinal = combine(a,b)\r\nprint final<\/pre>\n

\u5982\u679c\u76f4\u63a5\u4f7f\u7528 n \u8fdb\u884c debug \u5219\u5230 final=combine(a,b) \u8fd9\u53e5\u7684\u65f6\u5019\u4f1a\u5c06\u5176\u5f53\u505a\u666e\u901a\u7684\u8d4b\u503c\u8bed\u53e5\u5904\u7406\uff0c\u8fdb\u5165\u5230 print final\u3002\u5982\u679c\u60f3\u8981\u5bf9\u51fd\u6570\u8fdb\u884c debug \u5982\u4f55\u5904\u7406\u5462 ? \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 s \u8fdb\u5165\u51fd\u6570\u5757\u3002\u51fd\u6570\u91cc\u9762\u7684\u5355\u6b65\u8c03\u8bd5\u4e0e\u4e0a\u9762\u7684\u4ecb\u7ecd\u7c7b\u4f3c\u3002\u5982\u679c\u4e0d\u60f3\u5728\u51fd\u6570\u91cc\u5355\u6b65\u8c03\u8bd5\u53ef\u4ee5\u5728\u65ad\u70b9\u5904\u76f4\u63a5\u6309 r \u9000\u51fa\u5230\u8c03\u7528\u7684\u5730\u65b9\u3002<\/p>\n

\u6e05\u5355 8. \u5bf9\u51fd\u6570\u8fdb\u884c debug<\/strong><\/span><\/div>\n
[root@rcc-pok-idg-2255 ~]# python epdb2.py\r\n> \/root\/epdb2.py(10)?()\r\n-> b = \"bbb\"\r\n(Pdb) n\r\n> \/root\/epdb2.py(11)?()\r\n-> c = \"ccc\"\r\n(Pdb) n\r\n> \/root\/epdb2.py(12)?()\r\n-> final = combine(a,b)\r\n(Pdb) s\r\n--Call--\r\n> \/root\/epdb2.py(3)combine()\r\n-> def combine(s1,s2): # define subroutine combine, which...\r\n(Pdb) n\r\n> \/root\/epdb2.py(4)combine()\r\n-> s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...\r\n(Pdb) list\r\n1 import pdb\r\n2\r\n3 def combine(s1,s2): # define subroutine combine, which...\r\n4 -> s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...\r\n5 s3 = '\"' + s3 +'\"' # encloses it in double quotes,...\r\n6 return s3 # and returns it.\r\n7\r\n8 a = \"aaa\"\r\n9 pdb.set_trace()\r\n10 b = \"bbb\"\r\n11 c = \"ccc\"\r\n(Pdb) n\r\n> \/root\/epdb2.py(5)combine()\r\n-> s3 = '\"' + s3 +'\"' # encloses it in double quotes,...\r\n(Pdb) n\r\n> \/root\/epdb2.py(6)combine()\r\n-> return s3 # and returns it.\r\n(Pdb) n\r\n--Return--\r\n> \/root\/epdb2.py(6)combine()->'\"aaabbbaaa\"'\r\n-> return s3 # and returns it.\r\n(Pdb) n\r\n> \/root\/epdb2.py(13)?()\r\n-> print final\r\n(Pdb)<\/pre>\n

\u5728\u8c03\u8bd5\u7684\u65f6\u5019\u52a8\u6001\u6539\u53d8\u503c \u3002\u5728\u8c03\u8bd5\u7684\u65f6\u5019\u53ef\u4ee5\u52a8\u6001\u6539\u53d8\u53d8\u91cf\u7684\u503c\uff0c\u5177\u4f53\u5982\u4e0b\u5b9e\u4f8b\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u4e0b\u9762\u6709\u4e2a\u9519\u8bef\uff0c\u539f\u56e0\u662f b \u5df2\u7ecf\u88ab\u8d4b\u503c\u4e86\uff0c\u5982\u679c\u60f3\u91cd\u65b0\u6539\u53d8 b \u7684\u8d4b\u503c\uff0c\u5219\u5e94\u8be5\u4f7f\u7528\uff01 B\u3002<\/p>\n

\u6e05\u5355 9. \u5728\u8c03\u8bd5\u7684\u65f6\u5019\u52a8\u6001\u6539\u53d8\u503c<\/strong><\/span><\/div>\n
[root@rcc-pok-idg-2255 ~]# python epdb2.py\r\n> \/root\/epdb2.py(10)?()\r\n-> b = \"bbb\"\r\n(Pdb) var = \"1234\"\r\n(Pdb) b = \"avfe\"\r\n*** The specified object '= \"avfe\"' is not a function\r\nor was not found along sys.path.\r\n(Pdb) !b=\"afdfd\"\r\n(Pdb)<\/pre>\n

pdb \u8c03\u8bd5\u6709\u4e2a\u660e\u663e\u7684\u7f3a\u9677\u5c31\u662f\u5bf9\u4e8e\u591a\u7ebf\u7a0b\uff0c\u8fdc\u7a0b\u8c03\u8bd5\u7b49\u652f\u6301\u5f97\u4e0d\u591f\u597d\uff0c\u540c\u65f6\u6ca1\u6709\u8f83\u4e3a\u76f4\u89c2\u7684\u754c\u9762\u663e\u793a\uff0c\u4e0d\u592a\u9002\u5408\u5927\u578b\u7684 python \u9879\u76ee\u3002\u800c\u5728\u8f83\u5927\u7684 python \u9879\u76ee\u4e2d\uff0c\u8fd9\u4e9b\u8c03\u8bd5\u9700\u6c42\u6bd4\u8f83\u5e38\u89c1\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528\u66f4\u4e3a\u9ad8\u7ea7\u7684\u8c03\u8bd5\u5de5\u5177\u3002\u63a5\u4e0b\u6765\u5c06\u4ecb\u7ecd PyCharm IDE \u7684\u8c03\u8bd5\u65b9\u6cd5 .<\/p>\n","protected":false},"excerpt":{"rendered":"

import pdb a = “aaa” pdb.set_trace() b = “bbb” c = “ccc […]<\/p>\n","protected":false},"author":1481,"featured_media":166633,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-166631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/166631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=166631"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/166631\/revisions"}],"predecessor-version":[{"id":166634,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/166631\/revisions\/166634"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/166633"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=166631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=166631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=166631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}