Best way to indicate debugging code in python? #7648
-
Hi all, First off, let me say that I'm only ~50% sure that I've found the right place to post this query. If it's in the wrong place, please accept my apologies. Any hints as to a better place to post would be much appreciated. Try as I might, I can't find an obvious community forum for lgtm.com. I've seen references to discuss.lgtm.com, but that doesn't appear to exist anymore. I'm one of the Ghostscript/MuPDF developers, and we've been using lgtm for a while to improve our codebase. It's been a largely worthwhile experience (certainly more so than some other static analysis tools we've tried), but I'm hitting a snag with some python code. Let me say right off the bat that I am NOT a python coder, so it's entirely possible that there is something obvious I'm missing here. So enough preamble, the problem... Often, we have some debug code that needs to be disabled most of the time, but that we might want to switch on manually as part of hunting for particular bugs. In C we'd do this by doing something like: /* Enable the following to help debug group blending. */ at the top of the file, and then various #ifdef DUMP_GROUP_BLENDS sections throughout the following code. lgtm seems perfectly happy with this. In python, no #ifdef's of course. Accordingly, we end up with various: "if 0:" sections. lgtm shows these up as 'Unreachable code'. We could do: dump_group_blends = 0 at the start and then do "if dump_group_blends:" for each block, but I believe lgtm complains about that too. Is there a "best practice" formulation we should be using for such code to keep lgtm happy? Ideally, I'd really rather avoid having loads of annotating comments scattered through the code, but if we have to have one per such block, I guess we could... Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This is an interesting question (and you've definitely asked it in the right venue!). I don't know of any particular "best practice" when it comes to this issue, but I do have a few thoughts. First of all, you're right in thinking that doing For a more principled approach (and assuming these scripts are run manually from the command line, say) I would suggest using something like the built-in |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply. I am not running this from the command line - we're purely using lgtm.com to handle the testing/displaying of the code. It works so well, it seems silly to take on maintaining an installation ourselves. You say that:
will be seen through, but that:
will not. Can I do:
or am I being too hopeful? Failing that, is there scope for the code accepting some sort of annotation to tell it to ignore that certain variables are constants?
or something? Then I could have a list of #define equivalents at the top of each file all marked that way, and then just use if foo: blocks safely below. That's as neat a way as I can think of to work, but nothing is ever as simple as you'd hope... Thanks again. |
Beta Was this translation helpful? Give feedback.
This is an interesting question (and you've definitely asked it in the right venue!). I don't know of any particular "best practice" when it comes to this issue, but I do have a few thoughts.
First of all, you're right in thinking that doing
debug = 0
followed byif debug: ...
probably won't do the trick. In this case, I'm fairly certain that our analysis will be able to see through the deceit and figure out thatdebug
is actually always false. However, more complicated expressions should be able to fool our analysis. For example, something likeif 0*0: ...
is enough to fool our analysis into not pruning the body of theif
-statement (which makes the statements therein unreachable).For a …