[{"data":1,"prerenderedAt":1164,"buildId":1165},["ShallowReactive",2],{"\u002Fprojects\u002Fclaude-and-python-steamroll-errors":3},{"id":4,"title":5,"body":6,"createdAt":1147,"description":1148,"extension":1149,"meta":1150,"navigation":654,"path":1159,"seo":1160,"stem":1161,"subtitle":1162,"__hash__":1163},"content\u002Fprojects\u002Fclaude-and-python-steamroll-errors.md","Claude AI and Python @steamroll_errors",{"type":7,"value":8,"toc":1145},"minimark",[9,14,23,28,39,43,52,55,63,66,70,79,82,85,98,108,115,125,132,136,139,163,169,173,176,181,190,193,197,246,249,281,294,297,328,332,335,355,405,435,439,458,462,477,480,483,514,518,521,524,710,713,1041,1051,1055,1063,1066,1120,1124,1131,1134,1141],[10,11],"r-post-img",{"alt":12,"src":13},"D.W. from Arthur stating 'That sign can't stop me because I can't read!' while staring at a sign which clearly states 'No girls allowed.' on a bedroom door.","https:\u002F\u002Flive.staticflickr.com\u002F65535\u002F55688942825_787cd1d1f8_b.jpg",[15,16,17,18,22],"p",{},"While it is not common, there have been a few instances in the past years where I need\nmy Python function to finish running whether it encounters errors or not. Rather than\nwrap every single line of code with a ",[19,20,21],"code",{},"try-except-pass"," block I've decided to create a\nmore programmatic solution to this problem in the form of an easy-to-use decorator!",[24,25,27],"h5",{"id":26},"github-repository","GitHub Repository",[15,29,30,31,38],{},"The final code for this project is ",[32,33,37],"a",{"href":34,"rel":35},"https:\u002F\u002Fgithub.com\u002Fanthonyarusso\u002Fpython-steamroll-errors",[36],"nofollow","available on GitHub",".\nI encourage you to download the decorator and try it out for yourself.",[24,40,42],{"id":41},"ai-use-disclosure","AI Use Disclosure",[15,44,45,46,51],{},"Before I get too far into the details of this project I would like to be upfront about my\nuse of ",[32,47,50],{"href":48,"rel":49},"https:\u002F\u002Fclaude.ai",[36],"Claude AI"," to help create this project. This was my first attempt\nat using Claude Code on a project and I must say I was pleasantly (and somewhat horrifically)\nsurprised at its capabilities!",[15,53,54],{},"Admittedly, Claude did most of the heavy lifting for the actual Python code although the idea\n(and many of the initial debug efforts) had already been implemented by myself in the past.",[15,56,57,58,62],{},"This article and its contents were written solely by myself. I'm not ",[59,60,61],"em",{},"that"," lazy. Just regular\nlazy.",[15,64,65],{},"I will also note that Claude made a few interesting mistakes \u002F questionable decisions along\nthe way. I discuss these at the end of this post. 😊",[24,67,69],{"id":68},"inspiration","Inspiration",[15,71,72,73,78],{},"My inspiration for this approach actually comes from\n",[32,74,77],{"href":75,"rel":76},"https:\u002F\u002Fwww.ni.com\u002Fen\u002Fshop\u002Felectronic-test-instrumentation\u002Fapplication-software-for-electronic-test-and-instrumentation-category\u002Fwhat-is-teststand.html",[36],"National Instrument's TestStand","\nwhich features a \"continue on fail\" setting for it's steps such that the broader test suite\ncontinues to execute even if a particular test were to fail its test conditions.",[15,80,81],{},"I've extended this approach beyond high-level test sequencing to instead encompass any\ninstructions imaginable. In short, if I have a function and I need as much code within that\nfunction to execute as possible then this decorator is my best bet.",[15,83,84],{},"As a bit of a contrived use-case, imagine a system which initializes a connection to a vast\narray of dissimilar peripherals. The peripherals each have their own unique set of quirks\nwhich may cause them to fail at any time, even during initialization. Our control system\n(the Python code) should cleanly close any connections to such peripherals if any unhandled\nerror is encountered during operation. Let's suppose that the system will attempt to close\nits connections to peripherals A thru Z in alphabetical order. However, the most common\nerror I find with this approach is that unrelated errors near the beginning of this\n\"shut down\" sequence crash the program entirely and prevent it from safely closing out most\nof the other connections. Then, rather than dealing with a single stalled peripheral which\nneeds to be restarted \u002F cleaned up manually you instead end up with dozens of stalled\nperipherals which are still awaiting input from a program which is no longer running.",[86,87,91],"r-post-img-wide",{"alt":88,"src":89,"max-width":90},"A poorly drawn doodle of an operator singing 'la la la' while driving a steamroller over a crowd of stick figure people who are labeled 'errors'.","https:\u002F\u002Flive.staticflickr.com\u002F65535\u002F55687940997_bdb3a3fbce_b.jpg","500px",[92,93,95],"template",{"v-slot:caption":94},"",[15,96,97],{},"The mental image I had in mind when naming this Python decorator.",[15,99,100,101,104,105,107],{},"Using standard Python, the approach to fix this might be to wrap ",[59,102,103],{},"every single line"," of\ncode in the shutdown sequence with a ",[19,106,21],{}," (or otherwise handle every error\nappropriately but that is not always realistic). By doing so, we can effectively \"skip\"\nany lines of code which cause an error and still continue to close as many connections\nas possible before exiting the program. Obviously this still leaves some peripherals in\na hanging state, but better a few than the majority.",[15,109,110,111,114],{},"This solution, however, presents another problem: a massive mess of ",[19,112,113],{},"try-except-finally","\nboilerplate which no one wants to look at.",[15,116,117,118,120,121,124],{},"What if, dear reader, there were instead a programmatic method which could wrap every\nline of Python source code in a function with a ",[19,119,21],{}," statement ",[59,122,123],{},"at runtime","?",[15,126,127,128,131],{},"Well, look no further than the ",[19,129,130],{},"@steamroll_errors"," decorator!",[24,133,135],{"id":134},"problem-statement","Problem Statement",[15,137,138],{},"With that idea in mind there were still a few goal posts which the proposed Python decorator\nneeded to fulfill. Most importantly were the following four:",[140,141,142,146,149,160],"ol",{},[143,144,145],"li",{},"The decorated function must still play nice with a debugger.",[143,147,148],{},"The decorator must play nice with other decorators on the same function.",[143,150,151,152,155,156,159],{},"The caller should have the option to specify which ",[19,153,154],{},"Exception"," types ",[59,157,158],{},"should"," propagate\nthrough the decorated function and into the calling scope.",[143,161,162],{},"The caller should be able to optionally log any \"steamrolled\" errors rather than\nalways handle the event in complete silence.",[15,164,165,166,168],{},"The first goal point listed above is especially important. My initial attempts at this\ntask successfully wrapped the decorated function's code within ",[19,167,21],{}," blocks\nhowever they did not preserve information about which file and line number the original\nsource code occurred at. This cause the Python debugger to step through the decorated\nfunction in bizarre ways which were impossible to decipher much less debug.",[24,170,172],{"id":171},"the-bumps-along-the-way","The Bumps Along the Way",[15,174,175],{},"There were a couple of major hurdles to implementing this decorator which I'd like to\ndiscuss.",[177,178,180],"h6",{"id":179},"infinite-recursion","Infinite Recursion",[15,182,183,184,186,187,189],{},"First and foremost, when modifying the source code for the decorated function the\n",[19,185,130],{}," decorator itself would be passed along for the ride. This would\ncause the decorator to try and wrap its own statements within ",[19,188,21],{},"\nblocks which would lead to an infinite recursion which crashes Python. Not ideal.",[15,191,192],{},"Finding this error was a bit confusing, I'll admit. However, fixing this issue was\nfairly straightforward: strip the decorator from the source code which was passed\nback in to be modified.",[177,194,196],{"id":195},"handling-nested-code-blocks","Handling Nested Code Blocks",[15,198,199,200,203,204,207,208,203,211,214,215,220,221,223,224,227,228,230,231,234,235,238,239,242,243,245],{},"Next up, there was an issue related to nested code blocks such as ",[19,201,202],{},"for",", ",[19,205,206],{},"while",",\n",[19,209,210],{},"if-elif-else",[19,212,213],{},"with",", etc. statements. Python's\n",[32,216,219],{"href":217,"rel":218},"https:\u002F\u002Fearthly.dev\u002Fblog\u002Fpython-ast\u002F",[36],"abstract syntax tree (AST)","\nconsiders each of these statements to be a separate node. If we were to simply\niterate over the \"code\" within each function then we would only see the ",[19,222,202],{},",\netc. statements which live at the top level and not their internal lines of\ncode. This is not aligned with the intended purpose of the decorator. Instead,\nwe want to recurse ",[59,225,226],{},"into"," each of these blocks and wrap each of their internal\nlines of code with ",[19,229,21],{}," as well. That also holds true for nested\nblocks such as ",[19,232,233],{},"for-if-else",", etc. The solution here was to create a recursive\nfunction for modifying the source code which repeats for such statements. From\nthere we just needed a clearly defined base case for the recursion. In this case\nwe stop recursion when one of the following are encountered: an individual expression\nsuch as assignment, a ",[19,236,237],{},"raise"," statement, OR a call to an external function (or\nany other change of scope for that matter). This limits the modification of source\ncode to a well-defined scope (just the current function) and prevents the deluge\nof bugs which would ensue if we were to recursively wrap ",[59,240,241],{},"ALL"," of Python's\nstandard library in ",[19,244,21],{}," statements.",[15,247,248],{},"There are three other special cases to be aware of here:",[140,250,251,264,275],{},[143,252,253,254,203,257,260,261,245],{},"Nested ",[19,255,256],{},"def",[19,258,259],{},"async def",", and ",[19,262,263],{},"class",[143,265,266,267,270,271,274],{},"Scope declarations: ",[19,268,269],{},"global"," and ",[19,272,273],{},"nonlocal",".",[143,276,277,280],{},[19,278,279],{},"try"," blocks which are internal to the decorated function.",[15,282,253,283,203,285,260,287,289,290,293],{},[19,284,256],{},[19,286,259],{},[19,288,263],{}," statements are technically handled by\nthe \"change of scope\" rule from above. Internally, we use the ",[19,291,292],{},"ast"," library\nto check the code to be modified against any of these statement types and\nif it matches we simply skip the modifications for that node.",[15,295,296],{},"Scope declarations are not modified because these simply update where the\nfunction looks for the particular name when used within its definition. In fact,\nwrapping these statements risks altering the scope in which they are evaluated\nand may cause them to lose their desired effect within the original function.",[15,298,299,300,302,303,306,307,310,311,314,315,317,318,270,321,324,325,327],{},"Finally, ",[19,301,279],{}," blocks which are internal to the decorated function ",[59,304,305],{},"MUST"," be\nallowed to raise errors to at least the corresponding ",[19,308,309],{},"except"," block or else\nwe may interfere with the function author's original control flow. I.e. the\noriginal function could have very well handled the raised error appropriately\nbut if the decorator were to steamroll it at the exact moment the raising\nline of code executes then the original error handling would never be invoked\nand we would needlessly steamroll a recoverable error. In this case, we want\nto treat the ",[19,312,313],{},"try-except"," as a single atomic unit to be wrapped in an outer\n",[19,316,21],{}," layer meaning we only steamroll the error if it manages to\nescape the original function's own error handling. However, there is an\nexception to this exception! ",[19,319,320],{},"else",[19,322,323],{},"finally"," blocks which follow a\n",[19,326,279],{}," block will still have each line of their code wrapped as usual.",[177,329,331],{"id":330},"adding-sensible-error-messages","Adding Sensible Error Messages",[15,333,334],{},"There were a few errors which I could foresee end-users making when using this\ndecorator which I tried to preempt and provide sensible error messages for.\nThese were:",[140,336,337,345],{},[143,338,339,340,342,343,274],{},"Attempting to decorate Python code which isn't a ",[19,341,256],{}," or ",[19,344,259],{},[143,346,347,348,350,351,354],{},"Attempting to pass in a Python ",[19,349,154],{}," type rather than an iterable of\ntypes (it's easy to forget the brackets when you only have a single element\nin your ",[19,352,353],{},"errors_to_propagate"," argument).",[15,356,357,358,361,362,365,366,369,370,373,374,342,376,378,379,381,382,384,385,387,388,203,391,394,395,270,398,401,402,404],{},"Both of these are fairly straightforward. In the first case, we check the type\nof the source code's AST node against the ",[19,359,360],{},"ast.FunctionDef"," and\n",[19,363,364],{},"ast.AsyncFunctionDef"," types. If it doesn't match then we simply raise a\n",[19,367,368],{},"TypeError"," explaining that the decorator must be used ",[59,371,372],{},"DIRECTLY"," before a\n",[19,375,256],{},[19,377,259],{}," statement. Conveniently, this also captures cases where\nother decorators appear between the ",[19,380,130],{}," decorator and the\n",[19,383,256],{}," statement. In this case, the error message clarifies that the order\nof the decorators should always end with ",[19,386,130],{},". This is confirmed\nto work with all your usual decorators including ",[19,389,390],{},"@static_method",[19,392,393],{},"@property",",\nand both the ",[19,396,397],{},"@_func_name_.setter",[19,399,400],{},"@_func_name_.deleter"," decorators which\ncoincide with the ",[19,403,393],{}," decorator.",[15,406,407,408,410,411,413,414,417,418,420,421,424,425,270,428,431,432,434],{},"Finally, if an ",[19,409,154],{}," type is provided directly to the decorator then a\n",[19,412,368],{}," is raised informing the user that the decorator expects an\n",[59,415,416],{},"iterable"," of ",[19,419,154],{}," types and not just a single entry. This one was\nsomewhat tricky to implement because it passes a ",[19,422,423],{},"callable()"," check I\npreviously had in the code. To fix this I simply re-ordered the checks\nto start with an ",[19,426,427],{},"isinstance(..., type)",[19,429,430],{},"issubclass(..., Exception)"," check\nahead of the ",[19,433,423],{}," check.",[24,436,438],{"id":437},"solution","Solution",[15,440,441,442,445,446,449,450,453,454,457],{},"I'm proud to say the final product speaks for itself and passes all test cases\nthat myself and Claude could think of. Moreover, the logger works well with\na wide range of options including callback functions, ",[19,443,444],{},"logging.Logger"," from\nthe Python standard library, and any object supporting the\n",[19,447,448],{},"def write(self, s: str, \u002F) -> object: ..."," function signature. This last\noption is particularly easy to use since you can just feed in a\n",[19,451,452],{},"TextIOWrapper"," directly from a typical ",[19,455,456],{},"open()"," call and immediately start\nlogging which errors were steamrolled by the decorator.",[177,459,461],{"id":460},"debugger-compatibility","Debugger Compatibility",[86,463,466],{"alt":464,"src":465},"An example of the @steamroll_errors Python decorator working properly within a debugpy session.","https:\u002F\u002Flive.staticflickr.com\u002F65535\u002F55688940904_51081db28b_b.jpg",[92,467,468],{"v-slot:caption":94},[15,469,470,471,476],{},"An example of the @steamroll_errors Python decorator working properly within a ",[32,472,475],{"href":473,"rel":474},"https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fdebugpy\u002F",[36],"debugpy"," session.",[15,478,479],{},"Furthermore, the decorator plays nicely with debuggers. It does add a bit of\na strange effect where the same line of code must be \"stepped over\" twice\nin order to progress the debugger. I expect because it's really executing\nmultiple operations for every single \"line\" of code within the decorated\nfunction. However, I consider this to be acceptable and not in urgent need\nof fixing.",[15,481,482],{},"This was achieved using a single line of crucial code which ensures that the\nline number information from the original function is preserved going\nforward into the modified code such that the debugger can still point towards\nthe correct code in its tracebacks:",[484,485,489],"pre",{"className":486,"code":487,"language":488,"meta":94,"style":94},"language-python shiki shiki-themes dark-plus","# Keep line numbers pointing at the original file so tracebacks stay meaningful.\nast.increment_lineno(tree, code.co_firstlineno - 1)\n","python",[19,490,491,500],{"__ignoreMap":94},[492,493,496],"span",{"class":494,"line":495},"line",1,[492,497,499],{"class":498},"sOLPB","# Keep line numbers pointing at the original file so tracebacks stay meaningful.\n",[492,501,503,507,511],{"class":494,"line":502},2,[492,504,506],{"class":505},"sTNBD","ast.increment_lineno(tree, code.co_firstlineno - ",[492,508,510],{"class":509},"sDGX8","1",[492,512,513],{"class":505},")\n",[177,515,517],{"id":516},"example-usage","Example Usage",[15,519,520],{},"Below is example code borrowed from my GitHub repo's README.md of the\ndecorator in action.",[15,522,523],{},"Without arguments...",[484,525,527],{"className":486,"code":526,"language":488,"meta":94,"style":94},"\n# Use the decorator by itself (without arguments) to achieve default behavior.\n# I.e. all unhandled Exception types will be steamrolled without logging.\n@steamroll_errors\ndef bad_function(x: int, y: float) -> complex:\n    b = 1 \u002F 0  # raises ZeroDivisionError\n    a = int(\"hello\")  # raises ValueError\n    d = open(\".\u002Ffake_file.txt\", \"a\")  # raises FileNotFoundError\n\n    o = 3 + 3  # lines which don't produce errors go through the same as usual. :)\n    k = float(3.14)\n\n    return x + (y * 1j)\n",[19,528,529,534,539,546,588,605,625,649,656,673,688,693],{"__ignoreMap":94},[492,530,531],{"class":494,"line":495},[492,532,533],{"class":498},"# Use the decorator by itself (without arguments) to achieve default behavior.\n",[492,535,536],{"class":494,"line":502},[492,537,538],{"class":498},"# I.e. all unhandled Exception types will be steamrolled without logging.\n",[492,540,542],{"class":494,"line":541},3,[492,543,545],{"class":544},"sCudf","@steamroll_errors\n",[492,547,549,552,555,558,562,565,569,571,574,576,579,582,585],{"class":494,"line":548},4,[492,550,256],{"class":551},"scz_3",[492,553,554],{"class":544}," bad_function",[492,556,557],{"class":505},"(",[492,559,561],{"class":560},"s3F5K","x",[492,563,564],{"class":505},": ",[492,566,568],{"class":567},"sNl3T","int",[492,570,203],{"class":505},[492,572,573],{"class":560},"y",[492,575,564],{"class":505},[492,577,578],{"class":567},"float",[492,580,581],{"class":505},") -> ",[492,583,584],{"class":567},"complex",[492,586,587],{"class":505},":\n",[492,589,591,594,596,599,602],{"class":494,"line":590},5,[492,592,593],{"class":505},"    b = ",[492,595,510],{"class":509},[492,597,598],{"class":505}," \u002F ",[492,600,601],{"class":509},"0",[492,603,604],{"class":498},"  # raises ZeroDivisionError\n",[492,606,608,611,613,615,619,622],{"class":494,"line":607},6,[492,609,610],{"class":505},"    a = ",[492,612,568],{"class":567},[492,614,557],{"class":505},[492,616,618],{"class":617},"sKc5r","\"hello\"",[492,620,621],{"class":505},")  ",[492,623,624],{"class":498},"# raises ValueError\n",[492,626,628,631,634,636,639,641,644,646],{"class":494,"line":627},7,[492,629,630],{"class":505},"    d = ",[492,632,633],{"class":544},"open",[492,635,557],{"class":505},[492,637,638],{"class":617},"\".\u002Ffake_file.txt\"",[492,640,203],{"class":505},[492,642,643],{"class":617},"\"a\"",[492,645,621],{"class":505},[492,647,648],{"class":498},"# raises FileNotFoundError\n",[492,650,652],{"class":494,"line":651},8,[492,653,655],{"emptyLinePlaceholder":654},true,"\n",[492,657,659,662,665,668,670],{"class":494,"line":658},9,[492,660,661],{"class":505},"    o = ",[492,663,664],{"class":509},"3",[492,666,667],{"class":505}," + ",[492,669,664],{"class":509},[492,671,672],{"class":498},"  # lines which don't produce errors go through the same as usual. :)\n",[492,674,676,679,681,683,686],{"class":494,"line":675},10,[492,677,678],{"class":505},"    k = ",[492,680,578],{"class":567},[492,682,557],{"class":505},[492,684,685],{"class":509},"3.14",[492,687,513],{"class":505},[492,689,691],{"class":494,"line":690},11,[492,692,655],{"emptyLinePlaceholder":654},[492,694,696,700,703,705,708],{"class":494,"line":695},12,[492,697,699],{"class":698},"sYbnZ","    return",[492,701,702],{"class":505}," x + (y * ",[492,704,510],{"class":509},[492,706,707],{"class":551},"j",[492,709,513],{"class":505},[15,711,712],{},"With optional arguments...",[484,714,716],{"className":486,"code":715,"language":488,"meta":94,"style":94},"# Alternatively, the decorator takes two arguments:\n# 1) errors_to_propagate: Iterable[type[Exception]] -> An iterable of\n#   Exception types which should *not* be steamrolled and allowed to\n#   propagate out of the decorated function.\n# 2) logger: logger: Callable[[SteamrolledErrorLog], object]\n#    | logging.Logger\n#    | logging.LoggerAdapter\n#    | SupportsWrite\n#    | None = None\n#   -> A keyword only argument for either a callback function which takes\n#   a SteamrolledErrorLog as its sole argument OR a logging.Logger object\n#   from the Python standard library OR an object which supports the `def\n#   write(self, s: str, \u002F) -> object: ...` function call such as a\n#   TextIOWrapper returned from a typical `open()` call in Python OR None\n#   which is the default argument and produces no logs for any errors which\n#   are steamrolled.\n\nERROR_LOGGER = open(\".\u002Fsteamrolled_errors.log\", \"+a\", encoding=\"utf-8\")\n\n\n@steamroll_errors([FileNotFoundError, TypeError], logger=ERROR_LOGGER)\ndef bad_function(x: int, y: float) -> complex:\n    b = 1 \u002F 0  # raises ZeroDivisionError\n    a = int(\"hello\")  # raises ValueError\n    d = open(\".\u002Ffake_file.txt\", \"a\")  # raises FileNotFoundError\n\n    o = 3 + 3  # lines which don't produce errors go through the same as usual. :)\n    k = float(3.14)\n\n    return x + (y * 1j)\n\nif __name__ == \"__main__\":\n    # FileNotFoundError propagates out of this function and crashes the program.\n    bad_function(100, 2.77)\n",[19,717,718,723,728,733,738,743,748,753,758,763,768,773,778,784,790,796,802,807,838,843,848,872,901,914,929,948,953,966,979,984,997,1002,1019,1025],{"__ignoreMap":94},[492,719,720],{"class":494,"line":495},[492,721,722],{"class":498},"# Alternatively, the decorator takes two arguments:\n",[492,724,725],{"class":494,"line":502},[492,726,727],{"class":498},"# 1) errors_to_propagate: Iterable[type[Exception]] -> An iterable of\n",[492,729,730],{"class":494,"line":541},[492,731,732],{"class":498},"#   Exception types which should *not* be steamrolled and allowed to\n",[492,734,735],{"class":494,"line":548},[492,736,737],{"class":498},"#   propagate out of the decorated function.\n",[492,739,740],{"class":494,"line":590},[492,741,742],{"class":498},"# 2) logger: logger: Callable[[SteamrolledErrorLog], object]\n",[492,744,745],{"class":494,"line":607},[492,746,747],{"class":498},"#    | logging.Logger\n",[492,749,750],{"class":494,"line":627},[492,751,752],{"class":498},"#    | logging.LoggerAdapter\n",[492,754,755],{"class":494,"line":651},[492,756,757],{"class":498},"#    | SupportsWrite\n",[492,759,760],{"class":494,"line":658},[492,761,762],{"class":498},"#    | None = None\n",[492,764,765],{"class":494,"line":675},[492,766,767],{"class":498},"#   -> A keyword only argument for either a callback function which takes\n",[492,769,770],{"class":494,"line":690},[492,771,772],{"class":498},"#   a SteamrolledErrorLog as its sole argument OR a logging.Logger object\n",[492,774,775],{"class":494,"line":695},[492,776,777],{"class":498},"#   from the Python standard library OR an object which supports the `def\n",[492,779,781],{"class":494,"line":780},13,[492,782,783],{"class":498},"#   write(self, s: str, \u002F) -> object: ...` function call such as a\n",[492,785,787],{"class":494,"line":786},14,[492,788,789],{"class":498},"#   TextIOWrapper returned from a typical `open()` call in Python OR None\n",[492,791,793],{"class":494,"line":792},15,[492,794,795],{"class":498},"#   which is the default argument and produces no logs for any errors which\n",[492,797,799],{"class":494,"line":798},16,[492,800,801],{"class":498},"#   are steamrolled.\n",[492,803,805],{"class":494,"line":804},17,[492,806,655],{"emptyLinePlaceholder":654},[492,808,810,813,815,817,820,822,825,827,830,833,836],{"class":494,"line":809},18,[492,811,812],{"class":505},"ERROR_LOGGER = ",[492,814,633],{"class":544},[492,816,557],{"class":505},[492,818,819],{"class":617},"\".\u002Fsteamrolled_errors.log\"",[492,821,203],{"class":505},[492,823,824],{"class":617},"\"+a\"",[492,826,203],{"class":505},[492,828,829],{"class":560},"encoding",[492,831,832],{"class":505},"=",[492,834,835],{"class":617},"\"utf-8\"",[492,837,513],{"class":505},[492,839,841],{"class":494,"line":840},19,[492,842,655],{"emptyLinePlaceholder":654},[492,844,846],{"class":494,"line":845},20,[492,847,655],{"emptyLinePlaceholder":654},[492,849,851,853,856,859,861,863,866,869],{"class":494,"line":850},21,[492,852,130],{"class":544},[492,854,855],{"class":505},"([",[492,857,858],{"class":567},"FileNotFoundError",[492,860,203],{"class":505},[492,862,368],{"class":567},[492,864,865],{"class":505},"], ",[492,867,868],{"class":560},"logger",[492,870,871],{"class":505},"=ERROR_LOGGER)\n",[492,873,875,877,879,881,883,885,887,889,891,893,895,897,899],{"class":494,"line":874},22,[492,876,256],{"class":551},[492,878,554],{"class":544},[492,880,557],{"class":505},[492,882,561],{"class":560},[492,884,564],{"class":505},[492,886,568],{"class":567},[492,888,203],{"class":505},[492,890,573],{"class":560},[492,892,564],{"class":505},[492,894,578],{"class":567},[492,896,581],{"class":505},[492,898,584],{"class":567},[492,900,587],{"class":505},[492,902,904,906,908,910,912],{"class":494,"line":903},23,[492,905,593],{"class":505},[492,907,510],{"class":509},[492,909,598],{"class":505},[492,911,601],{"class":509},[492,913,604],{"class":498},[492,915,917,919,921,923,925,927],{"class":494,"line":916},24,[492,918,610],{"class":505},[492,920,568],{"class":567},[492,922,557],{"class":505},[492,924,618],{"class":617},[492,926,621],{"class":505},[492,928,624],{"class":498},[492,930,932,934,936,938,940,942,944,946],{"class":494,"line":931},25,[492,933,630],{"class":505},[492,935,633],{"class":544},[492,937,557],{"class":505},[492,939,638],{"class":617},[492,941,203],{"class":505},[492,943,643],{"class":617},[492,945,621],{"class":505},[492,947,648],{"class":498},[492,949,951],{"class":494,"line":950},26,[492,952,655],{"emptyLinePlaceholder":654},[492,954,956,958,960,962,964],{"class":494,"line":955},27,[492,957,661],{"class":505},[492,959,664],{"class":509},[492,961,667],{"class":505},[492,963,664],{"class":509},[492,965,672],{"class":498},[492,967,969,971,973,975,977],{"class":494,"line":968},28,[492,970,678],{"class":505},[492,972,578],{"class":567},[492,974,557],{"class":505},[492,976,685],{"class":509},[492,978,513],{"class":505},[492,980,982],{"class":494,"line":981},29,[492,983,655],{"emptyLinePlaceholder":654},[492,985,987,989,991,993,995],{"class":494,"line":986},30,[492,988,699],{"class":698},[492,990,702],{"class":505},[492,992,510],{"class":509},[492,994,707],{"class":551},[492,996,513],{"class":505},[492,998,1000],{"class":494,"line":999},31,[492,1001,655],{"emptyLinePlaceholder":654},[492,1003,1005,1008,1011,1014,1017],{"class":494,"line":1004},32,[492,1006,1007],{"class":698},"if",[492,1009,1010],{"class":560}," __name__",[492,1012,1013],{"class":505}," == ",[492,1015,1016],{"class":617},"\"__main__\"",[492,1018,587],{"class":505},[492,1020,1022],{"class":494,"line":1021},33,[492,1023,1024],{"class":498},"    # FileNotFoundError propagates out of this function and crashes the program.\n",[492,1026,1028,1031,1034,1036,1039],{"class":494,"line":1027},34,[492,1029,1030],{"class":505},"    bad_function(",[492,1032,1033],{"class":509},"100",[492,1035,203],{"class":505},[492,1037,1038],{"class":509},"2.77",[492,1040,513],{"class":505},[15,1042,1043,1044,1046,1047,1050],{},"With the ",[19,1045,130],{}," decorator present each of these functions continues chugging\nalong even if it were to hit an errant line of code such as ",[19,1048,1049],{},"b = 1 \u002F 0",". Neat!",[24,1052,1054],{"id":1053},"remarks-on-claudeai","Remarks on Claude.ai",[15,1056,1057,1058,1062],{},"I found ",[32,1059,1061],{"href":48,"rel":1060},[36],"Claude"," to be incredibly useful when working on this project and\nwas honestly shocked at how well it handled a somewhat advanced use case of Python. However,\nthere were a few quirks I noticed when working with Claude on this project.",[15,1064,1065],{},"Chief among them were as follows:",[140,1067,1068,1071,1108],{},[143,1069,1070],{},"Claude consistently tried to reword my decorator and internal code to refer to\n\"steamrolling errors\" as \"swallowing errors\". This makes sense as well, but it was oddly\ninsistent on using its own naming scheme (I liked mine better :P).",[143,1072,1073,1074,1077,1078,1081,1082,1084,1085,1087,1088,1090,1091,203,1094,203,1097,270,1100,1103,1104,1107],{},"Claude initially wrote code to steamroll anything inheriting from ",[19,1075,1076],{},"BaseException",". I\ndefinitely did ",[59,1079,1080],{},"not"," want this behavior and re-wrote the code to only steamroll types\nwhich subclass ",[19,1083,154],{},". Again, Claude was oddly insistent that this be ",[19,1086,1076],{},"\ndespite my explicit remarks against it. As you may well know ",[19,1089,1076],{}," here would\ncause ",[19,1092,1093],{},"KeyboardInterrupt",[19,1095,1096],{},"SystemExit",[19,1098,1099],{},"GeneratorExit",[19,1101,1102],{},"BaseExceptionGroup"," to all\nbe steamrolled as well. This is ",[59,1105,1106],{},"HIGHLY"," undesirable behavior as it breaks many of Python's\ncore functions including explicit operator instructions to halt the program. Perhaps\nClaude is trying to spare its software brethren...",[143,1109,1110,1111,1114,1115,270,1117,1119],{},"I had initially instructed Claude to deliberately wrap all lines of code within a\n",[19,1112,1113],{},"try-except-else-finally"," structure (including the ",[19,1116,279],{},[19,1118,309],{}," blocks). However,\nI later realized that this would break the original function's attempts at proper error\nhandling even if they were likely to succeed. This is definitely undesirable behavior\nin the context of this decorator. Nevertheless, Claude went through with the edit and\ngave no pushback or recommendations to the contrary. I'm a bit surprised that it can\nso thoroughly understand what needs to be done to implement what I had requested yet\nfails to catch, or at least fails to flag, such a major oversight in the design.",[24,1121,1123],{"id":1122},"closing-thoughts","Closing Thoughts",[15,1125,1126,1127,1130],{},"I had a lot of fun working on this project. Both during my initial attempt (not\ndocumented here) and this latest attempt with Claude assisting. It was very insightful\nas far as learning ",[59,1128,1129],{},"just how advanced"," these AI tools have come since I last checked\nthem out in college (2022-ish).",[15,1132,1133],{},"I hope this Python decorator is as useful to others as it has been to myself. Just\nbe mindful of where you go slapping it!",[15,1135,1136,1137],{},"Until next ",[1138,1139,1140],"r-end-mark",{},"time!",[1142,1143,1144],"style",{},"html pre.shiki code .sOLPB, html code.shiki .sOLPB{--shiki-default:#6A9955}html pre.shiki code .sTNBD, html code.shiki .sTNBD{--shiki-default:#D4D4D4}html pre.shiki code .sDGX8, html code.shiki .sDGX8{--shiki-default:#B5CEA8}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .sCudf, html code.shiki .sCudf{--shiki-default:#DCDCAA}html pre.shiki code .scz_3, html code.shiki .scz_3{--shiki-default:#569CD6}html pre.shiki code .s3F5K, html code.shiki .s3F5K{--shiki-default:#9CDCFE}html pre.shiki code .sNl3T, html code.shiki .sNl3T{--shiki-default:#4EC9B0}html pre.shiki code .sKc5r, html code.shiki .sKc5r{--shiki-default:#CE9178}html pre.shiki code .sYbnZ, html code.shiki .sYbnZ{--shiki-default:#C586C0}",{"title":94,"searchDepth":502,"depth":502,"links":1146},[],"2026-12-03","While it is not common, there have been a few instances in the past years where I need my Python function to finish running whether it encounters errors or not. Rather than wrap every single line of code with a try-except-pass block I've decided to create a more programmatic solution to this problem in the form of an easy-to-use decorator!","md",{"excerpt":1151},{"type":7,"value":1152},[1153,1155],[10,1154],{"alt":12,"src":13},[15,1156,17,1157,22],{},[19,1158,21],{},"\u002Fprojects\u002Fclaude-and-python-steamroll-errors",{"title":5,"description":94},"projects\u002Fclaude-and-python-steamroll-errors","These errors can't stop me because I can't read!","0vFZMk5Wr0SaAuJIVOuNXjTscc7uyifwOis3pIq-C9A",1796366482849,"342cc952-a69d-44e3-b83e-3fc955855a73"]